Ticket #3148 (closed bug: fixed)

Opened 8 weeks ago

Last modified 7 weeks ago

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 development
Cc: Blocked By:
Platform: All Blocking:

Description (last modified by korli) (diff)

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

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

Change History

Changed 8 weeks ago by bhaible

test program, to be run in a terminal

Changed 8 weeks ago by bhaible

screenshot of execution of the test program in a terminal

follow-up: ↓ 2   Changed 8 weeks ago by anevilyak

Could this be at all related to ticket #2963?

in reply to: ↑ 1   Changed 8 weeks 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.

  Changed 7 weeks ago by korli

  • description modified (diff)

  Changed 7 weeks ago by axeld

  • owner changed from bonefish to axeld
  • status changed from new to assigned

  Changed 7 weeks ago by axeld

  • status changed from assigned to closed
  • resolution set to fixed

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

Note: See TracTickets for help on using tickets.