Skip to content

terminal: restore default Ctrl+C handling before spawning a ConPTY child - #18

Merged
steipete merged 4 commits into
mainfrom
claude/conpty-ctrl-c
Sep 28, 2026
Merged

steipete merged 4 commits into
mainfrom
claude/conpty-ctrl-c

Conversation

@steipete

@steipete steipete commented Sep 27, 2026 •

Copy link
Copy Markdown

What does this PR do?

Makes Ctrl+C work through Bun.Terminal on Windows.

A Windows process started with Ctrl+C ignored passes that ignore flag to every child it creates. Common sources are CREATE_NEW_PROCESS_GROUP, Bun's own detached: true, and processes launched from a service or an SSH session. ConPTY turns \x03 input into CTRL_C_EVENT, but a Bun.Terminal child inherited Bun's ignore flag, so it never reacted and Ctrl+C could not stop a foreground program.

node-pty handles this by calling SetConsoleCtrlHandler(NULL, FALSE) right after CreatePseudoConsole (src/win/conpty.cc, "Restore default ctrl+c handler in conpty support"). create_pty_windows now does the same before the child is spawned.

The fixing line is the SetConsoleCtrlHandler(None, FALSE) call in src/runtime/api/bun/Terminal.rs. Windows has no per-child form of this attribute, so, exactly as with node-pty (which makes the same call in the Node process), it also restores default Ctrl+C handling in the Bun process itself: a Bun that was started with Ctrl+C ignored and shares a console will handle console Ctrl+C after it creates a terminal, and children it spawns later inherit that. Bun processes that install their own SIGINT handling keep it, because registered handlers still run. The code comment and changelog entry state this. A child-only variant would need an undocumented read of the process's current Ctrl+C attribute and a toggle around CreateProcess that races other spawning threads, so this keeps node-pty's behavior.

terminal-platform-gaps.test.ts no longer marks "Ctrl+C input interrupts the child" as a Windows todo. The old comment blamed the system conhost, but node-pty works on the same conhost. A new Windows-only regression starts the terminal from a detached: true Bun process, which ignores Ctrl+C, so the test fails without this change on any runner.

How did you verify your code works?

Windows Server 2022 (build 20348) on an AWS Crabbox lease. Bun was started over SSH, which is how the parent ends up ignoring Ctrl+C. The Windows x64 build of this branch comes from the release workflow's tagless dry run 36341508968; its bun-windows-x64.zip matched the run's SHA256SUMS, and the workflow's own Windows smoke test passed. The comparison is against stock Bun 1.4.2.

Check Bun 1.4.2 This branch
Bun.spawn(["cmd.exe"], { terminal }), run ping -n 300 127.0.0.1, write \x03 ping still running 6 s later ping stopped in 451 ms and 455 ms
Regression scenario: a detached: true Bun process creates the terminal child and writes \x03 child never sees SIGINT (10 s watchdog) SIGINT received, 2/2
Same stock binary with SetConsoleCtrlHandler(NULL, FALSE) called through bun:ffi before spawning (isolates the cause) ping stopped in 528 ms and 530 ms n/a

The regression scenario was run as a standalone script mirroring the new test, since this repository's test harness was not set up on the Windows box. The bun:test file itself has not run on Windows.

An end-to-end run through OpenClaw's spawnTerminalPty on this build, with cmd.exe over ConPTY and no Node on PATH, matched node-pty on the same host:

  • Ctrl-C and resize (the new size visible to mode con after about 350 ms)
  • exit codes and onExit
  • a 20,000-line burst delivered intact
  • .cmd shim command lines
  • kill-tree cleanup

On stock 1.4.2 the same run failed only on Ctrl-C.

  • bun run rust:check-all x86_64-pc-windows-msvc: ok.

A process started with Ctrl+C ignored (CREATE_NEW_PROCESS_GROUP, or from a service or SSH session) passes that flag to every child it creates. ConPTY raises CTRL_C_EVENT for \x03 input, but a Bun.Terminal child inherited Bun's ignore flag and never reacted, so Ctrl+C could not interrupt a foreground program. node-pty calls SetConsoleCtrlHandler(NULL, FALSE) after CreatePseudoConsole for this reason; do the same.

On Windows Server 2022 with Bun 1.4.2 launched over SSH, \x03 left a foreground `ping` running 6 s later in 2/2 runs; calling SetConsoleCtrlHandler(NULL, FALSE) through bun:ffi before spawning made the same \x03 stop it in about 530 ms in 2/2 runs. The platform-gaps test no longer marks Ctrl+C as a Windows todo, and a Windows-only regression runs the terminal from a detached (Ctrl+C-ignoring) process so it fails without the fix on any runner. Windows x64 type-checks; runtime verification needs a Windows build.
@steipete
steipete merged commit 8282146 into main Sep 28, 2026
9 checks passed
@steipete
steipete deleted the claude/conpty-ctrl-c branch September 28, 2026 01:15
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.

1 participant