Skip to content

devpts: do not hang up master before its replica is opened - #17244

Merged
milantracy merged 1 commit into
gvisor:masterfrom
faukah:devpts-fix
Nov 24, 2026
Merged

milantracy merged 1 commit into
gvisor:masterfrom
faukah:devpts-fix

Conversation

@faukah

@faukah faukah commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

hello 👍

gVisor treats a PTY master as hung up whenever it has no open replica file descriptors, including before any replica has been opened. In that state, poll() on the master reports POLLHUP and read() fails with EIO. A program that opens a PTY, forks a child that opens the replica, and reads the master at once sees a hang up if the read runs before the child opens the replica.

Local Nix builds fail at random under runsc because of this (NixOS/nix#15181). Nix reads the builder's startup line from a PTY master while the forked builder opens the replica. When the read runs first, Nix fails with
"unexpected EOF reading a line".

A minimal reproduction example for this:

import os, fcntl, struct, select
m = os.open("/dev/ptmx", os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
fcntl.ioctl(m, 0x40045431, struct.pack("i", 0))  # TIOCSPTLCK
p = select.poll(); p.register(m, select.POLLIN | select.POLLOUT)
print("revents:", p.poll(0))
try:
    os.read(m, 1)
except OSError as e:
    print("read:", e.strerror)
                     poll               read
Linux 7.3.1          POLLOUT            EAGAIN
runsc, without fix   POLLOUT|POLLHUP    EIO
runsc, with fix      POLLOUT            EAGAIN

There's been some related work on this as well: PR #14505 closed #13738, but it fixed a different devpts bug (ESPIPE on positional I/O); this change actually fixes the bug the issue reports.
The POLLHUP for a master without replicas came from the fix for #9951 and #9333, which still work because they close a replica that was opened.

Fixes #13738.
(I think it makes sense to re-open that one, since it's still valid, and have it closed by this pr?)

ayushr2
ayushr2 previously approved these changes Nov 9, 2026

@ayushr2 ayushr2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

ayushr2
ayushr2 previously approved these changes Nov 23, 2026
@ayushr2

ayushr2 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

can you rebase and push again

Comment thread test/syscalls/linux/pty.cc Resolved
gVisor treats a PTY master as hung up whenever it has no open replica
file descriptors, including before any replica has been opened. In
that state, poll() on the master reports POLLHUP and read() fails with
EIO. A program that opens a PTY, forks a child that opens the replica,
and reads the master at once sees a hang up if the read runs before
the child opens the replica.

On Linux, the master is only hung up once a replica has been opened and
then closed again. Before the first open, poll() on the master does not
report POLLHUP, and read() blocks or, if the master is nonblocking,
fails with EAGAIN.

Track whether a replica was ever opened, and only treat the master as
hung up if one was and none are left.

Local Nix builds fail at random under runsc because of this
(NixOS/nix#15181). Nix reads the builder's startup line from a PTY
master while the forked builder opens the replica. When the read runs
first, Nix fails with "unexpected EOF reading a line". Tools like
expect and script that poll the master of a new session have the same
race.

Fixes gvisor#13738

Signed-off-by: faukah <[email protected]>
Change-Id: If185581843f5f6b7b2a510f6f54a4e4a6a6a6964
@milantracy
milantracy enabled auto-merge (squash) November 24, 2026 05:06
@milantracy
milantracy disabled auto-merge November 24, 2026 05:06
@milantracy
milantracy enabled auto-merge (squash) November 24, 2026 05:07
@milantracy
milantracy merged commit 5cc6d03 into gvisor:master Nov 24, 2026
9 checks passed
@faukah
faukah deleted the devpts-fix branch November 24, 2026 07:10
EtiennePerot pushed a commit that referenced this pull request Nov 26, 2026
gVisor treats a PTY master as hung up whenever it has no open replica
file descriptors, including before any replica has been opened. In that
state, poll() on the master reports POLLHUP and read() fails with EIO. A
program that opens a PTY, forks a child that opens the replica, and
reads the master at once sees a hang up if the read runs before the
child opens the replica.

Local Nix builds fail at random under runsc because of this
([NixOS/nix#15181](NixOS/nix#15181)). Nix
reads the builder's startup line from a PTY master while the forked
builder opens the replica. When the read runs first, Nix fails with
"unexpected EOF reading a line".

A minimal reproduction example for this:

```python
import os, fcntl, struct, select
m = os.open("/dev/ptmx", os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
fcntl.ioctl(m, 0x40045431, struct.pack("i", 0))  # TIOCSPTLCK
p = select.poll(); p.register(m, select.POLLIN | select.POLLOUT)
print("revents:", p.poll(0))
try:
    os.read(m, 1)
except OSError as e:
    print("read:", e.strerror)
```

```
                     poll               read
Linux 7.3.1          POLLOUT            EAGAIN
runsc, without fix   POLLOUT|POLLHUP    EIO
runsc, with fix      POLLOUT            EAGAIN
```

There's been some related work on this as well: PR #14505 closed #13738,
but it fixed a different devpts bug (ESPIPE on positional I/O); this
change _actually_ fixes the bug the issue reports.
The POLLHUP for a master without replicas came from the fix for #9951
and #9333, which still work because they close a replica that was
opened.

Fixes #13738.
(I think it makes sense to re-open that one, since it's still valid, and
have it closed by this pr?)

Signed-off-by: faukah <[email protected]>
(cherry picked from commit 5cc6d03)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

devpts: read() on PTY master returns EIO when no replica has ever been opened (Linux blocks)

3 participants