Skip to content

fix(runtime): fence node-pty writes at PTY exit - #3016

Merged
M4n5ter merged 1 commit into
mainfrom
fix/node-pty-fd-reuse
Aug 15, 2026
Merged

M4n5ter merged 1 commit into
mainfrom
fix/node-pty-fd-reuse

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Summary

Patch the node-pty Unix writer so queued PTY input cannot outlive its raw file descriptor and corrupt a subsequently opened file after fd reuse. Writes now use the already nonblocking descriptor synchronously, verify descriptor identity before retries, and stop at every exit or close fence.

Add an isolated deterministic regression that blocks the sole libuv worker, reopens a sentinel at the exact retired PTY fd, and proves that the unpatched queued write reaches that sentinel.

Fixes #2978

Verification

  • npm ci — patch-package reapplies [email protected]
  • npm run build:test
  • npm run typecheck
  • npm --workspace @maka/runtime test — 2,836 tests, 0 failures
  • Targeted regression on Node 22.19, Node 24.5, and Node 26.3
  • npm run lint
  • npm run format:check
  • Regression confirmed to fail with the patch reversed and pass after restoration

Review focus

This is a temporary dependency patch for an upstream Unix lifecycle defect. Remove it when node-pty publishes an equivalent fix. This PR intentionally does not upgrade to beta.15 because that release does not modify the Unix write path.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck, and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — queued Unix PTY writes stop at descriptor retirement instead of reaching a reused file
  • No

AI disclosure: Codex prepared this draft with direction from M4n5ter; M4n5ter is the contributor of record and will review the result.

@M4n5ter
M4n5ter marked this pull request as ready for review August 15, 2026 03:09
@M4n5ter
M4n5ter requested a review from Astro-Han August 15, 2026 03:09

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — this fixes the race at the correct ownership boundary. Fencing queued writes inside node-pty’s CustomWriteStream is substantially cleaner than adding retries or defensive behavior in Runtime or SQLite consumers. I found no P0–P2 issues, so I’m approving.

Before merging, please rebase onto the current main, resolve the patches/README.md conflict, and confirm the new head is green. I consider this merge hygiene rather than a blocker to approval.

One non-blocking test note: after reversing the dependency patch, the regression failed consistently in one environment but survived repeated runs in another. The underlying fd-reuse race and the production fix are sound, but the fixture still appears sensitive to scheduling and fd allocation. It may be worth either forcing reuse of the exact retired fd or describing this as a stress regression rather than a deterministic one.

中文对照

感谢修复。这个改动在正确的职责边界上解决了竞态:在 node-pty 的 CustomWriteStream 内终止过期写入,比在 Runtime 或 SQLite 使用方增加重试和防御逻辑干净得多。没有发现 P0–P2 问题,因此 Approve。

合并前请 rebase 到当前 main,解决 patches/README.md 的冲突,并确认新 head 的 CI 全绿。这属于合并状态处理,不阻塞本次 Approve。

有一个非阻塞的测试建议:反向移除依赖补丁后,该回归测试在一个环境中能稳定失败,但在另一个环境中多次通过。底层 fd 重用竞态和生产修复本身是成立的,不过测试夹具仍会受到调度和 fd 分配的影响。后续可以考虑强制重用准确的已退役 fd,或者将其称为压力回归测试,而不是确定性测试。

AI-assisted review disclosure: Codex assisted with source inspection and focused validation. I reviewed the evidence and own the conclusions and approval.

node-pty can leave async raw-fd writes queued after a Unix PTY exits, allowing descriptor reuse to redirect terminal input into unrelated files.

Patch the dependency at its write-lifecycle boundary and add a deterministic exact-fd-reuse regression that fails against the unpatched package.

Generated-by: Codex
@M4n5ter
M4n5ter force-pushed the fix/node-pty-fd-reuse branch from 00ca32f to 388910c Compare August 15, 2026 08:14
@M4n5ter

M4n5ter commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

Addressed both points: the branch is rebased onto current main, and the regression now forces reuse of the exact retired PTY fd. It fails unpatched and passes patched on Node 22.19, 24.5, and 26.3.

All affected checks are green. The remaining unrelated Desktop E2E failure is tracked in #3044 and has a separate fix underway. We have decided to merge.

@M4n5ter
M4n5ter merged commit afa8006 into main Aug 15, 2026
22 of 24 checks passed
@M4n5ter
M4n5ter deleted the fix/node-pty-fd-reuse branch August 15, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flaky test: shell-run-manager secret redaction across a soft wrap

2 participants