Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/acp-bridge/src/spawnChannel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ describe('createSpawnChannelFactory env policy', () => {
});

const spawnOptions = mockSpawn.mock.calls[0]?.[2] as
| { env?: NodeJS.ProcessEnv }
| { env?: NodeJS.ProcessEnv; windowsHide?: boolean }
| undefined;
expect(spawnOptions?.windowsHide).toBe(true);
expect(spawnOptions?.env).not.toHaveProperty('QWEN_CODE_SIMPLE');
expect(spawnOptions?.env).not.toHaveProperty('QWEN_SERVER_TOKEN');
expect(spawnOptions?.env).not.toHaveProperty(
Expand Down
1 change: 1 addition & 0 deletions packages/acp-bridge/src/spawnChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ export function createSpawnChannelFactory(
{
cwd: workspaceCwd,
stdio: ['pipe', 'pipe', 'pipe'],
windowsHide: true,
env: childEnv,
},
);
Expand Down
159 changes: 79 additions & 80 deletions packages/core/src/services/shellExecutionService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const mockProcessKill = vi
// to avoid PATH/CWD binary planting. Compute the expected path the same way so
// assertions stay in sync across platforms (SystemRoot is unset off Windows).
const TASKKILL = `${process.env['SystemRoot'] || 'C:\\Windows'}\\System32\\taskkill.exe`;
const HIDDEN_WINDOW = { windowsHide: true };
const CHCP = `${process.env['SystemRoot'] || 'C:\\Windows'}\\System32\\chcp.com`;

const shellExecutionConfig = {
Expand Down Expand Up @@ -1411,29 +1412,27 @@ describe('ShellExecutionService', () => {
expect(result.aborted).toBe(true);
// Cancel tree-kills (/t) SYNCHRONOUSLY (spawnSync) so taskkill enumerates
// the tree before ptyProcess.kill() fires ClosePseudoConsole. See #5873.
expect(mockSpawnSync).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockSpawnSync).toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
// The finalizer reap (async cpSpawn via windowsKillPid) must ALSO
// tree-kill on cancel — positively asserted so removing the reap or
// forcing cancelKillDispatched=false fails here, not just trivially via
// the negative check below. See #5873.
expect(mockCpSpawn).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
// ...and it must never downgrade to a shell-only (/f without /t) kill
// that could leave the abandoned descendant tree behind. See #5873.
expect(mockCpSpawn).not.toHaveBeenCalledWith(TASKKILL, [
'/f',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
['/f', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
// ...and still calls ptyProcess.kill() so that if taskkill cannot launch,
// the ConPTY host is torn down and onExit fires (the cancel can't hang).
// See #5873.
Expand All @@ -1457,17 +1456,16 @@ describe('ShellExecutionService', () => {
expect(result.exitCode).toBe(0);
// Reap kills only the shell pid — no /t — so a child the command
// intentionally detached (e.g. Start-Process) survives. See #5873.
expect(mockCpSpawn).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).not.toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
TASKKILL,
['/f', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
});

it('normal completion on win32 swallows a taskkill launch error (no crash)', async () => {
Expand Down Expand Up @@ -1514,6 +1512,7 @@ describe('ShellExecutionService', () => {
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
expect.anything(),
HIDDEN_WINDOW,
);
} finally {
// clearAllMocks() resets calls but not implementations — restore the
Expand All @@ -1529,7 +1528,11 @@ describe('ShellExecutionService', () => {
});

expect(result.exitCode).toBe(0);
expect(mockCpSpawn).not.toHaveBeenCalledWith(TASKKILL, expect.anything());
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
expect.anything(),
HIDDEN_WINDOW,
);
});

it('exit cleanup on win32 tree-kills via taskkill and tears down the host', () => {
Expand All @@ -1546,12 +1549,11 @@ describe('ShellExecutionService', () => {
ShellExecutionService.cleanup();
ShellExecutionService['activePtys'].delete(pid);

expect(mockSpawnSync).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(pid),
]);
expect(mockSpawnSync).toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(pid)],
HIDDEN_WINDOW,
);
// The ConPTY host is torn down unconditionally, alongside the tree-kill.
expect(mockPtyProcess.kill).toHaveBeenCalled();
});
Expand Down Expand Up @@ -1622,17 +1624,17 @@ describe('ShellExecutionService', () => {

expect(result.aborted).toBe(true);
// performCancelKill's sync tree-kill still runs...
expect(mockSpawnSync).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockSpawnSync).toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
// ...but the finalizer reap is skipped (isPtyActive false via ESRCH),
// so no async taskkill fires there.
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
expect.anything(),
HIDDEN_WINDOW,
);
} finally {
mockProcessKill.mockImplementation(() => true);
Expand Down Expand Up @@ -1670,12 +1672,11 @@ describe('ShellExecutionService', () => {

// Pins killChildProcesses on the absolute System32 path — a regression to
// the bare 'taskkill' name reopens the binary-planting hole. See #5873.
expect(mockSpawnSync).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(childPid),
]);
expect(mockSpawnSync).toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(childPid)],
HIDDEN_WINDOW,
);
});

it('win32 taskkill is invoked by absolute System32 path, not the bare name', async () => {
Expand Down Expand Up @@ -1713,17 +1714,16 @@ describe('ShellExecutionService', () => {
);

expect(result.exitCode).toBe(0);
expect(mockCpSpawn).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).not.toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
TASKKILL,
['/f', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
});

it('win32 promoted shell reaps its lingering pwsh on natural exit (shell-pid-only)', async () => {
Expand Down Expand Up @@ -1754,17 +1754,16 @@ describe('ShellExecutionService', () => {
postPromoteExitHandler({ exitCode: 0, signal: undefined });

expect(settleCalls).toHaveLength(1);
expect(mockCpSpawn).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).not.toHaveBeenCalledWith(TASKKILL, [
'/f',
'/t',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
TASKKILL,
['/f', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
['/f', '/t', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
});

it('win32 promoted shell reaps on natural exit even with onData only (no onSettle)', async () => {
Expand All @@ -1790,11 +1789,11 @@ describe('ShellExecutionService', () => {
onExitRegistrations[onExitRegistrations.length - 1][0];
postPromoteExitHandler({ exitCode: 0, signal: undefined });

expect(mockCpSpawn).toHaveBeenCalledWith(TASKKILL, [
'/f',
'/pid',
String(mockPtyProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
TASKKILL,
['/f', '/pid', String(mockPtyProcess.pid)],
HIDDEN_WINDOW,
);
});

it('win32 promoted shell skips the post-settle reap when the pty already exited', async () => {
Expand Down Expand Up @@ -1834,6 +1833,7 @@ describe('ShellExecutionService', () => {
expect(mockCpSpawn).not.toHaveBeenCalledWith(
TASKKILL,
expect.anything(),
HIDDEN_WINDOW,
);
} finally {
mockProcessKill.mockImplementation(() => true);
Expand Down Expand Up @@ -2632,12 +2632,11 @@ describe('ShellExecutionService child_process fallback', () => {
expectedSignal,
);
} else {
expect(mockCpSpawn).toHaveBeenCalledWith(expectedCommand, [
'/f',
'/t',
'/pid',
String(mockChildProcess.pid),
]);
expect(mockCpSpawn).toHaveBeenCalledWith(
expectedCommand,
['/f', '/t', '/pid', String(mockChildProcess.pid)],
HIDDEN_WINDOW,
);
}
});
},
Expand Down
33 changes: 18 additions & 15 deletions packages/core/src/services/shellExecutionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ interface ProcessCleanupStrategy {
// workspace or on PATH could run from these cleanup paths with the CLI's
// environment — arbitrary code execution out of a benign teardown. See #5873.
const WINDOWS_TASKKILL = `${process.env['SystemRoot'] || 'C:\\Windows'}\\System32\\taskkill.exe`;
const WINDOWS_TASKKILL_OPTIONS = { windowsHide: true } as const;

const windowsKillPid = (pid: number, tree: boolean): void => {
try {
Expand All @@ -558,7 +559,7 @@ const windowsKillPid = (pid: number, tree: boolean): void => {
const args = tree
? ['/f', '/t', '/pid', pid.toString()]
: ['/f', '/pid', pid.toString()];
const killer = cpSpawn(WINDOWS_TASKKILL, args);
const killer = cpSpawn(WINDOWS_TASKKILL, args, WINDOWS_TASKKILL_OPTIONS);
// Log (don't crash on) a failed launch: silently swallowing it would let
// the #5873 pwsh leak quietly return with no diagnostic trail under
// enterprise lockdown / EMFILE / antivirus interception.
Expand Down Expand Up @@ -597,7 +598,11 @@ const windowsStrategy: ProcessCleanupStrategy = {
// before the process dies (and a sync stderr write would be exit-time
// noise). The unconditional ptyProcess.kill() below is the mitigation;
// the runtime reap paths (windowsKillPid), which DO flush, keep logging.
spawnSync(WINDOWS_TASKKILL, ['/f', '/t', '/pid', pid.toString()]);
spawnSync(
WINDOWS_TASKKILL,
['/f', '/t', '/pid', pid.toString()],
WINDOWS_TASKKILL_OPTIONS,
);
} catch {
// ignore
}
Expand All @@ -621,7 +626,7 @@ const windowsStrategy: ProcessCleanupStrategy = {
}
// No logging — like killPty, this runs only from the 'exit' handler
// where an async debug write can't flush before the process dies.
spawnSync(WINDOWS_TASKKILL, args);
spawnSync(WINDOWS_TASKKILL, args, WINDOWS_TASKKILL_OPTIONS);
} catch {
// ignore
}
Expand Down Expand Up @@ -1332,12 +1337,11 @@ export class ShellExecutionService {
const performCancelKill = async (): Promise<void> => {
if (!child.pid || exited) return;
if (isWindows) {
const killer = cpSpawn(WINDOWS_TASKKILL, [
'/f',
'/t',
'/pid',
child.pid.toString(),
]);
const killer = cpSpawn(
WINDOWS_TASKKILL,
['/f', '/t', '/pid', child.pid.toString()],
WINDOWS_TASKKILL_OPTIONS,
);
// taskkill can fail two ways, and either would otherwise hang the
// cancel (the abort waits for a child exit that never comes), so
// fall back to killing the child directly in both:
Expand Down Expand Up @@ -2312,12 +2316,11 @@ export class ShellExecutionService {
// cancel path). ptyProcess.kill() alone doesn't tree-kill under
// ConPTY (microsoft/node-pty#333).
try {
const r = spawnSync(WINDOWS_TASKKILL, [
'/f',
'/t',
'/pid',
ptyProcess.pid.toString(),
]);
const r = spawnSync(
WINDOWS_TASKKILL,
['/f', '/t', '/pid', ptyProcess.pid.toString()],
WINDOWS_TASKKILL_OPTIONS,
);
if (r.error || (typeof r.status === 'number' && r.status !== 0)) {
debugLogger.warn(
`performCancelKill: taskkill failed for pid ${ptyProcess.pid}: ${r.error?.message ?? `exit ${r.status}`}`,
Expand Down
Loading
Loading