Repository navigation
terminal: restore default Ctrl+C handling before spawning a ConPTY child - #18
Merged
Merged
Conversation
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.
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Makes Ctrl+C work through
Bun.Terminalon 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 owndetached: true, and processes launched from a service or an SSH session. ConPTY turns\x03input intoCTRL_C_EVENT, but aBun.Terminalchild 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 afterCreatePseudoConsole(src/win/conpty.cc, "Restore default ctrl+c handler in conpty support").create_pty_windowsnow does the same before the child is spawned.The fixing line is the
SetConsoleCtrlHandler(None, FALSE)call insrc/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 ownSIGINThandling 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 aroundCreateProcessthat races other spawning threads, so this keeps node-pty's behavior.terminal-platform-gaps.test.tsno 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 adetached: trueBun 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.zipmatched the run'sSHA256SUMS, and the workflow's own Windows smoke test passed. The comparison is against stock Bun 1.4.2.Bun.spawn(["cmd.exe"], { terminal }), runping -n 300 127.0.0.1, write\x03detached: trueBun process creates the terminal child and writes\x03SIGINTreceived, 2/2SetConsoleCtrlHandler(NULL, FALSE)called throughbun:ffibefore spawning (isolates the cause)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
spawnTerminalPtyon this build, withcmd.exeover ConPTY and no Node onPATH, matched node-pty on the same host:mode conafter about 350 ms)onExit.cmdshim command linesOn stock 1.4.2 the same run failed only on Ctrl-C.
bun run rust:check-all x86_64-pc-windows-msvc: ok.