Skip to content

externalTerminal: restore Ctrl+C in Windows external terminals - #350408

Draft
Alexandru Dima (alexdima) wants to merge 1 commit into
mainfrom
agents/vscode-issue-205417-resolution
Draft

Alexandru Dima (alexdima) wants to merge 1 commit into
mainfrom
agents/vscode-issue-205417-resolution

Conversation

@alexdima

@alexdima Alexandru Dima (alexdima) commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #205417

On Windows, programs started in an external terminal (Open New External Terminal, Ctrl+Shift+C) ignore Ctrl+C, so you can't stop a running ping -t, Python script or dev server. Ctrl+C at the prompt and Ctrl+Break still work (more details in the duplicate #262468). This regressed in 1.86, and this PR is a one-line fix.

Root cause

WindowsExternalTerminalService.spawnTerminal opens the terminal with cmd.exe /c start /wait <terminal>. The .NET debugging Ctrl+C fix (#201020) also made that spawn detached: true:

  • On Windows, detached: true makes libuv pass DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP to CreateProcess (libuv). Node's documentation of detached doesn't mention this.
  • For CREATE_NEW_PROCESS_GROUP, the Win32 documentation says "CTRL+C signals will be disabled for all processes within the new process group", and that the group "includes all processes that are descendants of this root process" (Process Creation Flags).
  • Ignoring Ctrl+C "is inherited by child processes" (SetConsoleCtrlHandler), including through the new console that start opens. So the terminal and every program started in it ignore Ctrl+C.

Windows Terminal turns Ctrl+C handling back on when it starts (main.cpp), which is why running wt from an affected terminal works around the bug. node-pty does the same for the integrated terminal (conpty.cc).

Fix

Spawn the terminal without detached, as before #201020 (1.85 and earlier). The debugger's external console (runInTerminal) is a separate code path that never used detached, so the part of #201020 that addressed #81322 (launching through wt.exe) is unchanged.

detached isn't needed to keep the terminal open. Without it, Node ties only the cmd.exe /c start /wait helper to VS Code's lifetime. libuv's job object lets that helper's children break away, so the terminal that start opens stays open when VS Code exits (checked below). Electron creates the helper's console without a window (kHideConsoleWindows), so no extra window should appear; step 2 of How to test checks that.

Evidence

I ran WindowsExternalTerminalService.openTerminal from this branch's compiled output, and from the same module with this change reverted (equivalent to main), with Python as the terminal program (windowsExec). At startup, Python sends its console the event that pressing Ctrl+C sends (GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)) and reports whether KeyboardInterrupt arrived.

"VS Code" was a Node.js 24 process, started in one of two ways. One was with Ctrl+C handling on and no console, like VS Code started from the Start menu or taskbar (I checked that Explorer doesn't ignore Ctrl+C). The other was detached, the way the code CLI starts VS Code. Each combination ran 3 times on Windows 11 (build 26300). The last row covers all 6 runs in each column.

main This PR
VS Code started from the Start menu or taskbar ❌ Ctrl+C ignored (0/3) ✅ Ctrl+C reaches the program (3/3)
VS Code started by the code CLI ❌ (0/3) ❌ (0/3), unchanged, see below
Terminal still open after VS Code exits ✅ (6/6) ✅ (6/6)

The runs used an invisible window station, so no windows were shown and window visibility isn't covered.

Repro script

Run it from the root of a built checkout with node repro-205417.mjs. It needs Python on the PATH and opens a console window for up to two seconds.

// Repro for https://github.com/microsoft/vscode/issues/205417 (Windows only).
// Run from the root of a built vscode checkout: node repro-205417.mjs
// It calls WindowsExternalTerminalService.openTerminal with Python as the terminal program. At startup, Python sends its
// console the event that pressing Ctrl+C sends, and reports whether KeyboardInterrupt arrived. A console window opens
// for up to two seconds.
import { execFileSync } from 'node:child_process';
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { setTimeout } from 'node:timers/promises';
import { pathToFileURL } from 'node:url';

const servicePath = resolve(process.argv[2] ?? 'out/vs/platform/externalTerminal/node/externalTerminalService.js');
const { WindowsExternalTerminalService } = await import(pathToFileURL(servicePath).href);

const python = execFileSync('python', ['-c', 'import sys; print(sys.executable)'], { encoding: 'utf8' }).trim();
const dir = mkdtempSync(join(tmpdir(), 'vscode-205417-'));
const probe = join(dir, 'probe.py');
writeFileSync(probe, `
import ctypes, json, os, time
received = False
try:
    ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, 0)  # CTRL_C_EVENT to every process on this console
    time.sleep(2)
except KeyboardInterrupt:
    received = True
out = os.environ['PROBE_OUT']
with open(out + '.tmp', 'w') as f:
    json.dump({'ctrlCReceived': received, 'pid': os.getpid()}, f)
os.replace(out + '.tmp', out)
time.sleep(float(os.environ.get('PROBE_LINGER', '0')))
os._exit(0)
`);
const out = process.env.PROBE_OUT ??= join(dir, 'result.json');
process.env.PYTHONSTARTUP = probe;

new WindowsExternalTerminalService().openTerminal({ windowsExec: python }, tmpdir());
for (let i = 0; i < 100 && !existsSync(out); i++) {
	await setTimeout(200);
}
const result = existsSync(out) ? JSON.parse(readFileSync(out, 'utf8')) : undefined;
console.log(result ? `Ctrl+C reached the program: ${result.ctrlCReceived ? 'yes' : 'NO'}` : 'No result: the terminal did not start');
try {
	rmSync(dir, { recursive: true, force: true });
} catch {
	// Python may still be using the folder
}
process.exit(result ? 0 : 1);

On main it prints Ctrl+C reached the program: NO, and with this PR Ctrl+C reached the program: yes.

Not changed: VS Code started by the code CLI

The code CLI has started VS Code detached since 2016 (cli.ts). A VS Code started that way ignores Ctrl+C itself, and so does everything it starts, including external terminals. That was already true before 1.86. The same probably happens after Restart to Update, which starts the installer detached, but I haven't checked. Fixing that would mean turning Ctrl+C handling back on in the main process at startup, as Windows Terminal and node-pty do. That needs a native call, so I left it for a follow-up.

How to test

  1. Start VS Code from the Start menu or taskbar, not with code from a terminal.
  2. Press Ctrl+Shift+C. Only the terminal window opens (Command Prompt by default).
  3. Run ping -t 127.0.0.1 and press Ctrl+C. Before this change, ping keeps running and only Ctrl+Break stops it. With this change, ping stops.
  4. Close VS Code. The external terminal stays open.

Validation

  • node node_modules/mocha/bin/mocha.js test/unit/node/index.ts --delay --ui=tdd --exit --run src/vs/platform/externalTerminal/test/node/externalTerminalService.test.ts (the test-node script, on Node.js 24.22.1): 13 passing. The new test fails when detached: true is put back.
  • The formatting check from build/hygiene.ts (Formatter.verifyFormatting) on both changed files: passed.
  • Not run locally: oxlint and the full type check, because my local dependencies are behind main. CI runs both.

On Windows, `detached: true` starts the child in a new process group, and
Windows disables Ctrl+C for every process in that group. Since #201020 the
external terminal was spawned that way, so programs started in it ignored
Ctrl+C. The terminal still stays open after VS Code exits without it.

Fixes #205417

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:52

This branch has not been deployed

No deployments
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.

Launching External Terminal Changes Ctrl+C Handling Of Programs

1 participant