Skip to content

devpts: return ESPIPE on positional read and write - #14505

Merged
copybara-service[bot] merged 1 commit into
gvisor:masterfrom
adi-IL:fix/devpts-positional-io-espipe
Sep 28, 2026
Merged

copybara-service[bot] merged 1 commit into
gvisor:masterfrom
adi-IL:fix/devpts-positional-io-espipe

Conversation

@adi-IL

@adi-IL adi-IL commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Positional I/O (pread, pwrite, preadv, pwritev, preadv2, pwritev2 with an offset) on Linux pseudo-terminals fails with ESPIPE.

In gVisor, masterFileDescription and replicaFileDescription inherited FileDescriptionDefaultImpl, which returns EINVAL when opts.DenyPRead and opts.DenyPWrite are false.

Set DenyPRead and DenyPWrite in FileDescriptionOptions when initializing both master and replica file descriptions so positional reads and writes return ESPIPE.

Fixes #13738.

@rexren-gif rexren-gif added the ready to pull Causes the PR to be imported into Google. Will be removed soon. label Sep 28, 2026
copybara-service Bot pushed a commit that referenced this pull request Sep 28, 2026
Positional I/O (`pread`, `pwrite`, `preadv`, `pwritev`, `preadv2`, `pwritev2` with an offset) on Linux pseudo-terminals fails with `ESPIPE`.

In gVisor, `masterFileDescription` and `replicaFileDescription` inherited `FileDescriptionDefaultImpl`, which returns `EINVAL` when `opts.DenyPRead` and `opts.DenyPWrite` are false.

Set `DenyPRead` and `DenyPWrite` in `FileDescriptionOptions` when initializing both master and replica file descriptions so positional reads and writes return `ESPIPE`.

Fixes #13738.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#14505 from adi-IL:fix/devpts-positional-io-espipe 3270e65
PiperOrigin-RevId: 989705902
copybara-service Bot pushed a commit that referenced this pull request Sep 28, 2026
Positional I/O (`pread`, `pwrite`, `preadv`, `pwritev`, `preadv2`, `pwritev2` with an offset) on Linux pseudo-terminals fails with `ESPIPE`.

In gVisor, `masterFileDescription` and `replicaFileDescription` inherited `FileDescriptionDefaultImpl`, which returns `EINVAL` when `opts.DenyPRead` and `opts.DenyPWrite` are false.

Set `DenyPRead` and `DenyPWrite` in `FileDescriptionOptions` when initializing both master and replica file descriptions so positional reads and writes return `ESPIPE`.

Fixes #13738.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#14505 from adi-IL:fix/devpts-positional-io-espipe 3270e65
PiperOrigin-RevId: 989705902
copybara-service Bot pushed a commit that referenced this pull request Sep 28, 2026
Positional I/O (`pread`, `pwrite`, `preadv`, `pwritev`, `preadv2`, `pwritev2` with an offset) on Linux pseudo-terminals fails with `ESPIPE`.

In gVisor, `masterFileDescription` and `replicaFileDescription` inherited `FileDescriptionDefaultImpl`, which returns `EINVAL` when `opts.DenyPRead` and `opts.DenyPWrite` are false.

Set `DenyPRead` and `DenyPWrite` in `FileDescriptionOptions` when initializing both master and replica file descriptions so positional reads and writes return `ESPIPE`.

Fixes #13738.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#14505 from adi-IL:fix/devpts-positional-io-espipe 3270e65
PiperOrigin-RevId: 989705902
copybara-service Bot pushed a commit that referenced this pull request Sep 28, 2026
Positional I/O (`pread`, `pwrite`, `preadv`, `pwritev`, `preadv2`, `pwritev2` with an offset) on Linux pseudo-terminals fails with `ESPIPE`.

In gVisor, `masterFileDescription` and `replicaFileDescription` inherited `FileDescriptionDefaultImpl`, which returns `EINVAL` when `opts.DenyPRead` and `opts.DenyPWrite` are false.

Set `DenyPRead` and `DenyPWrite` in `FileDescriptionOptions` when initializing both master and replica file descriptions so positional reads and writes return `ESPIPE`.

Fixes #13738.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#14505 from adi-IL:fix/devpts-positional-io-espipe 3270e65
PiperOrigin-RevId: 989705902
@copybara-service
copybara-service Bot merged commit d327f2d into gvisor:master Sep 28, 2026
13 checks passed
milantracy pushed a commit that referenced this pull request Nov 24, 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]>
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

ready to pull Causes the PR to be imported into Google. Will be removed soon.

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)

2 participants