Opened 10 years ago

Closed 10 years ago

#3148 closed bug (fixed)

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

Reported by: bhaible Owned by: axeld
Priority: normal Milestone: R1
Component: Drivers/TTY Version: R1/pre-alpha1
Keywords: Cc:
Blocked By: Blocking:
Has a Patch: no 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).

Attachments (2)

select-stdin.c (2.0 KB) - added by bhaible 10 years ago.
test program, to be run in a terminal
haiku-select-tty-bug.png (5.9 KB) - added by bhaible 10 years ago.
screenshot of execution of the test program in a terminal

Download all attachments as: .zip

Change History (7)

Changed 10 years ago by bhaible

Attachment: select-stdin.c added

test program, to be run in a terminal

Changed 10 years ago by bhaible

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

screenshot of execution of the test program in a terminal

comment:1 Changed 10 years ago by anevilyak

Could this be at all related to ticket #2963?

comment:2 in reply to:  1 Changed 10 years ago by bhaible

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 Changed 10 years ago by korli

Description: modified (diff)

comment:4 Changed 10 years ago by axeld

Owner: changed from bonefish to axeld
Status: newassigned

comment:5 Changed 10 years ago by axeld

Resolution: fixed
Status: assignedclosed

Thanks for the nice test case! It's fixed in hrev28687.

Note: See TracTickets for help on using tickets.