Skip to content
Draft
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
31 changes: 31 additions & 0 deletions src/conpty_console_list_agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2024, Microsoft Corporation (MIT License).
*/

import * as assert from 'assert';
import * as path from 'path';
import { fork } from 'child_process';

if (process.platform === 'win32') {
describe('conpty_console_list_agent', () => {
it('should gracefully handle AttachConsole failure for a dead PID', (done) => {
const agentPath = path.join(__dirname, 'conpty_console_list_agent');
const deadPid = 999999;

const agent = fork(agentPath, [deadPid.toString()]);

agent.on('message', (message: { consoleProcessList: number[] }) => {
// When AttachConsole fails, the agent should fall back to returning
// just the shell PID rather than crashing.
assert.deepStrictEqual(message.consoleProcessList, [deadPid]);
done();
});

agent.on('exit', (code) => {
if (code !== 0) {
done(new Error(`Agent exited with code ${code}, expected graceful fallback`));
}
});
});
});
}
11 changes: 9 additions & 2 deletions src/conpty_console_list_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { loadNativeModule } from './utils';

const getConsoleProcessList = loadNativeModule('conpty_console_list').module.getConsoleProcessList;
const shellPid = parseInt(process.argv[2], 10);
const consoleProcessList = getConsoleProcessList(shellPid);
process.send!({ consoleProcessList });
try {
const consoleProcessList = getConsoleProcessList(shellPid);
process.send!({ consoleProcessList });
} catch {
// AttachConsole can fail if the shell process has already exited.
// Fall back to returning just the shell PID, matching the timeout
// fallback behavior in windowsPtyAgent.ts.
process.send!({ consoleProcessList: [shellPid] });
}
process.exit(0);