Files
magpie/internal/claudetui/pty_darwin_test.go
T
yetone b2509b9a81 claudetui: the Mac's ptmx is read blocking, outside Go's poller (#4834, ifccod7391564 on X)
On macOS 14.8 (x86_64 in #4834, arm64 for ifccod7391564, Claude Code
2.1.318, Claude Team) the helper's first read of /dev/ptmx answered
EAGAIN. read() took any error as the terminal's end, so nothing read the
master again: Claude Code filled the terminal's 1024 bytes and hung in
write, and 45s later the run said "showed no prompt" and went to -p.

openPTY opened the master with os.OpenFile, which hands a character
device to Go's kqueue poller in non-blocking mode. Go returns a raw
EAGAIN only for a non-blocking descriptor the poller doesn't hold, so on
those Macs the poller refused the ptmx and the descriptor was left
non-blocking. The master is now opened with syscall.Open (blocking,
O_CLOEXEC) and wrapped with os.NewFile, which keeps a blocking descriptor
out of the poller: each read waits in its own thread, as the reporter's
rawfd reproduction does. read() also takes EAGAIN as "nothing yet" and
reads again after 10ms rather than ending.

Tests:
- TestPTYMasterBlocking (darwin): the master isn't O_NONBLOCK, and a
  child's 4000 bytes (the reporter's repro) are all read.
- TestReadPastEAGAIN: a terminal that answers EAGAIN three times, then
  draws, has its drawing read.

Run: go test ./internal/proc ./internal/claudetui on mote (macOS 27,
arm64) ok; go vet for darwin, linux and windows on the two packages ok.
Broken by hand: with the EAGAIN branch off, TestReadPastEAGAIN is red
(wsl-run, screen ""). With os.OpenFile put back, TestPTYMasterBlocking
still passes on mote: on macOS 27 the poller refuses the ptmx and Go puts
the descriptor back to blocking, so the hang doesn't happen there.
Not tried: macOS 14 itself (no such machine here); the fix is the
reporter's rawfd variant, which read all 4000 bytes on their macOS 14.8.
2026-11-10 18:33:35 +08:00

75 lines
1.7 KiB
Go

package claudetui
import (
"os"
"os/exec"
"syscall"
"testing"
"time"
"golang.org/x/sys/unix"
)
// TestPTYMasterBlocking: the master is a blocking descriptor outside Go's
// poller, so a read waits for Claude Code's output instead of answering
// EAGAIN (#4834), and a child's 4000 bytes, more than the terminal holds,
// are all read (the reporter's reproduction).
func TestPTYMasterBlocking(t *testing.T) {
m, name, err := openPTY()
if err != nil {
t.Fatal(err)
}
defer m.Close()
fl, err := unix.FcntlInt(m.Fd(), unix.F_GETFL, 0)
if err != nil {
t.Fatal(err)
}
if fl&unix.O_NONBLOCK != 0 {
t.Fatalf("the ptmx master is non-blocking (flags %#x): a read can answer EAGAIN", fl)
}
tty, err := os.OpenFile(name, os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("/bin/sh", "-c", "sleep 0.2; head -c 4000 /dev/zero | tr '\\0' x")
cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
if err := cmd.Start(); err != nil {
tty.Close()
t.Fatal(err)
}
tty.Close()
got := make(chan int, 1)
go func() {
total, buf := 0, make([]byte, 64<<10)
for {
n, err := m.Read(buf)
total += n
if err != nil {
got <- total
return
}
}
}()
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(10 * time.Second):
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
<-done
t.Fatal("the child hung writing to a terminal no one read")
}
select {
case n := <-got:
if n < 4000 {
t.Fatalf("read %d bytes, want 4000", n)
}
case <-time.After(10 * time.Second):
t.Fatal("the read never ended after the child did")
}
}