Files
magpie/internal/claudetui/pty_darwin.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

49 lines
1.6 KiB
Go

package claudetui
import (
"os"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
// openPTY opens a pseudo-terminal: its master, and the name of its slave.
//
// The master is opened blocking and wrapped with os.NewFile, so it stays
// out of Go's kqueue poller. os.OpenFile puts a character device in the
// poller, non-blocking, and on some Macs (macOS 14.8 on x86_64 and on
// arm64) the first read of such a ptmx answered EAGAIN instead of
// waiting: the read loop ended, Claude Code filled the terminal's 1024
// bytes and hung, and the run said "showed no prompt" after 45s (#4834).
// A blocking read in its own thread works on every Mac.
func openPTY() (*os.File, string, error) {
fd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
if err != nil {
return nil, "", &os.PathError{Op: "open", Path: "/dev/ptmx", Err: err}
}
m := os.NewFile(uintptr(fd), "/dev/ptmx")
if err := unix.IoctlSetInt(fd, unix.TIOCPTYGRANT, 0); err != nil {
m.Close()
return nil, "", err
}
if err := unix.IoctlSetInt(fd, unix.TIOCPTYUNLK, 0); err != nil {
m.Close()
return nil, "", err
}
var name [128]byte
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(unix.TIOCPTYGNAME), uintptr(unsafe.Pointer(&name[0]))); e != 0 {
m.Close()
return nil, "", e
}
n := 0
for n < len(name) && name[n] != 0 {
n++
}
return m, string(name[:n]), nil
}
// the requests that read and set a terminal's modes (the tests' fake
// Claude Code sets its own raw, as the real one does)
const ioctlGetTermios, ioctlSetTermios = unix.TIOCGETA, unix.TIOCSETA