Skip to content

Commit 6b4e208

Browse files
Do not block when writing nothing to PTY master input queue
A zero-length write(2) to a PTY master blocked forever (or returned EAGAIN with O_NONBLOCK). inputQueueWrite returned ErrWouldBlock whenever no bytes were copied, which is indistinguishable from a full input queue when the source is empty. Linux returns 0, even if the input queue is full. This is the master-side counterpart of #9955, which fixed the same problem for writes to the replica's output queue. Assisted-by: Claude Code
1 parent 3cd7128 commit 6b4e208

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎pkg/sentry/fsimpl/devpts/line_discipline.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ func (l *lineDiscipline) inputQueueRead(ctx context.Context, dst usermem.IOSeque
354354
// +checklocksexclude:l.masterWaiter.mu
355355
// +checklocksexclude:l.replicaWaiter.mu
356356
func (l *lineDiscipline) inputQueueWrite(ctx context.Context, src usermem.IOSequence) (int64, error) {
357+
// Writing nothing always copies 0 bytes, which below is
358+
// indistinguishable from a full input queue and would block forever.
359+
if src.NumBytes() == 0 {
360+
return 0, nil
361+
}
357362
l.termiosMu.RLock()
358363
n, notifyEcho, err := l.inQueue.write(ctx, src, l)
359364
l.termiosMu.RUnlock()

‎test/syscalls/linux/pty.cc‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,26 @@ TEST_F(PtyTest, ReplicaWriteNothingNonCanonical) {
17461746
ExpectFinished(master_);
17471747
}
17481748

1749+
// Test that writing nothing to the PTY master's input queue does not block or
1750+
// return an error.
1751+
TEST_F(PtyTest, MasterWriteNothingCanonical) {
1752+
constexpr char kInput[] = "";
1753+
EXPECT_THAT(WriteFd(master_.get(), kInput, strlen(kInput)),
1754+
SyscallSucceedsWithValue(strlen(kInput)));
1755+
1756+
ExpectFinished(replica_);
1757+
}
1758+
1759+
TEST_F(PtyTest, MasterWriteNothingNonCanonical) {
1760+
DisableCanonical();
1761+
1762+
constexpr char kInput[] = "";
1763+
EXPECT_THAT(WriteFd(master_.get(), kInput, strlen(kInput)),
1764+
SyscallSucceedsWithValue(strlen(kInput)));
1765+
1766+
ExpectFinished(replica_);
1767+
}
1768+
17491769
TEST_F(PtyTest, SimpleEcho) {
17501770
constexpr char kInput[] = "Mr. Eko";
17511771
EXPECT_THAT(WriteFd(master_.get(), kInput, strlen(kInput)),

0 commit comments

Comments
 (0)