Opened 15 years ago

Last modified 15 years ago

#3148 closed bug

select() returns too early when reading from a tty with line editing enabled, in blocking mode — at Version 3

Reported by: bhaible Owned by: bonefish
Priority: normal Milestone: R1
Component: Drivers/TTY Version: R1/pre-alpha1
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description (last modified by korli)

The select() function, when used to detect whether a tty is ready for reading, returns too early.

According to POSIX http://www.opengroup.org/susv3/functions/select.html, "the ... select() function shall modify the objects pointed to by the readfds, writefds, and errorfds arguments to indicate which file descriptors are ready for reading, ready for writing". And: "A descriptor shall be considered ready for reading when a call to an input function with O_NONBLOCK clear would not block"

Run the attached test program in a terminal. It reads from stdin. After 3 seconds, type an 'x'. After a few more seconds, type 'y', 'z' and 'Return'. The output should be something like this;

Applying select() from standard input. Press Ctrl-C to abort.
......x........y...z..
Input available! Trying to read 1 byte...
Input available! Trying to read 1 byte...
Input available! Trying to read 1 byte...
Input available! Trying to read 1 byte...
.......

The actual output is like this:

Applying select() from standard input. Press Ctrl-C to abort.
......x.Input available! Trying to read 1 byte...
[Here no progressing dots any more, the program is blocked]yz
Input available! Trying to read 1 byte...
Input available! Trying to read 1 byte...
Input available! Trying to read 1 byte...

This means, select() pretends that the stdin descriptor is ready for reading as soon as the user types the first character in the line. But the tty has line editing enabled, and read() therefore blocks until the user has pressed 'Return'.

The fix should be that when the tty has line editing enabled, select() waits until the 'Return' at the end of the line has been pressed (or the timeout occurs).

Change History (5)

by bhaible, 15 years ago

Attachment: select-stdin.c added

test program, to be run in a terminal

by bhaible, 15 years ago

Attachment: haiku-select-tty-bug.png added

screenshot of execution of the test program in a terminal

comment:1 by anevilyak, 15 years ago

Could this be at all related to ticket #2963?

in reply to:  1 comment:2 by bhaible, 15 years ago

Replying to anevilyak:

Could this be at all related to ticket #2963?

I think it's unrelated. select() is typically implemented by dispatching according to the type of file descriptor: regular file, pipe, tty, socket are all different cases. #2963 is about sockets, #3148 about ttys.

comment:3 by korli, 15 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.