Bug 204812

Summary: pty read blocks after select, if ^S is written in between
Product: Base System Reporter: cgull
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: Open ---    
Severity: Affects Only Me CC: ed, jilles
Priority: ---    
Version: 10.2-RELEASE   
Hardware: amd64   
OS: Any   

Description cgull 2015-11-25 17:34:27 UTC
The pts driver has an issue with breaking the promise made by select() that a read will not block, if there is a write of ^S to the pty between the select() and the read().

The reproduction for this is very simple:

  script /dev/null yes

Type ^S.

Type ^Q.  Nothing happens, because script is hung on a blocking read.  The read will never complete, because script is hung and your ^Q will never be delivered.

I originally discovered this while working on mosh (net/mosh), but script blocks in the same way.

The sequence of events that causes this:

* The pty slave has software flow control enabled.

* select() returns read status for the pty master fd.

* Something write()s ^S to the pty master.

* A read() is started on the pty master, with the expectation that it will not block.  But it does block, and since the tty is stopped, it will never unblock without outside intervention.

I hesitate to call this a bug, because the state of the pty *is* changed by that write, and the fix for this issue is not clear.  But it violates POLA, other kernels (Linux, OpenBSD) do not behave this way, and the consequences are serious.

I see this issue on FreeBSD 10.2, 9.1, and 7.2.  An old 4.10 box doesn't have this issue.

I originally discovered this bug while working on Mosh.  It's not a new bug there.  I've now implemented pty packet mode, and the problem exists there too, even though packet mode should always be able to avoid breaking this promise.  See

https://github.com/mobile-shell/mosh/issues/692
https://github.com/cgull/mosh/tree/pty-packet

I don't understand why nobody has run into this issue before though, either on the FreeBSD or on the Mosh side.  (Mosh has this problem on FreeBSD and OS X.)
Comment 1 Jilles Tjoelker freebsd_committer freebsd_triage 2015-11-28 22:26:12 UTC
I can reproduce this using script on stable/10 and head (all amd64).
Comment 2 cgull 2019-01-16 19:28:25 UTC
Still present on 12.0p2.