Skip to content
Merged
Prev Previous commit
Next Next commit
fix(shell): make Unix PTY reads interruptible during cleanup
  • Loading branch information
SivanCola committed Nov 13, 2026
commit d35b1c3644d35426a851779791beb0d4fff41259
21 changes: 21 additions & 0 deletions internal/persistentshell/pty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package persistentshell

import (
"fmt"
"os"
"os/exec"
"sync"
"syscall"

"github.com/creack/pty"
"golang.org/x/sys/unix"

"deepzora/internal/proc"
)
Expand All @@ -30,6 +32,25 @@ func startPTY(argv []string, dir string, env []string) (ptyConn, error) {
if err != nil {
return nil, err
}
// PTY setup obtains Fd(), which can leave the master in blocking mode.
// Register a fresh nonblocking descriptor with Go's poller so Close can
// interrupt Read even when another process still holds the slave open.
fd, err := unix.FcntlInt(file.Fd(), unix.F_DUPFD_CLOEXEC, 0)
if err == nil {
err = unix.SetNonblock(fd, true)
if err != nil {
_ = unix.Close(fd)
}
}
if err != nil {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
_ = file.Close()
_ = cmd.Wait()
return nil, fmt.Errorf("persistent shell PTY polling: %w", err)
}
pollable := os.NewFile(uintptr(fd), file.Name())
_ = file.Close()
file = pollable
// Reap the shell without blocking: its exit surfaces to callers as a read
// error on the master side, so the wait status itself carries no decision.
go func() { _ = cmd.Wait() }()
Expand Down
54 changes: 54 additions & 0 deletions internal/persistentshell/pty_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build !windows

package persistentshell

import (
"errors"
"os"
"testing"
"time"
)

func TestUnixPTYReadDeadlineAndClose(t *testing.T) {
conn, err := startPTY([]string{"/bin/sh", "-c", "sleep 60"}, t.TempDir(), os.Environ())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
file := conn.(*unixPTY).file
if err := file.SetReadDeadline(time.Now().Add(50 * time.Millisecond)); err != nil {
t.Fatalf("PTY reads must support cancellation: %v", err)
}
read := func() <-chan error {
done := make(chan error, 1)
go func() {
var buf [1]byte
_, err := conn.Read(buf[:])
done <- err
}()
return done
}
await := func(done <-chan error) error {
t.Helper()
select {
case err := <-done:
return err
case <-time.After(2 * time.Second):
t.Fatal("PTY reader remained blocked")
return nil
}
}
if err := await(read()); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("idle read did not honor its deadline: %v", err)
}
if err := file.SetReadDeadline(time.Time{}); err != nil {
t.Fatal(err)
}
done := read()
if err := conn.Close(); err != nil {
t.Fatal(err)
}
if err := await(done); err == nil {
t.Fatal("closed PTY read reported success")
}
}
11 changes: 10 additions & 1 deletion release-notes/releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@
]
}
],
"fixed": []
"fixed": [
{
"targets": ["desktop", "cli"],
"title": { "en": "Interrupt idle Unix terminal readers during session cleanup", "zh": "会话清理时中断 Unix 终端的空闲读取" },
"body": {
"en": "Linux and macOS persistent shells register their PTY master for cancellable I/O, so closing a session can interrupt an idle reader even if another process keeps a terminal writer open. Command output, Unicode transport and persistent working-directory state remain covered by regression checks.",
"zh": "Linux、macOS 持久化终端将 PTY 主端注册为可取消的 I/O,即使其他进程仍持有终端写入端,关闭会话也能中断空闲读取。命令输出、Unicode 传输及工作目录保持均继续通过回归检查。"
}
}
]
},
"upgrade": [
{
Expand Down
Loading