Skip to content

Commit f9f3ecc

Browse files
ryoppippidevinsays
andcommitted
fix(cli): forward repeated platform signals
Forward every catchable launcher signal delivery without suppressing repeated deliveries. Use SIGINT, SIGBREAK, and SIGHUP on Windows while retaining the Unix signal set, and cover both behaviors in focused tests. Co-authored-by: devinsays <[email protected]>
1 parent e697018 commit f9f3ecc

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

‎apps/ccusage/src/cli.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function isMainModule({ argvEntry = process.argv[1], moduleUrl, realpathPath = r
165165
*/
166166
function getForwardedSignals(platform = process.platform) {
167167
if (platform === 'win32') {
168-
return ['SIGINT', 'SIGTERM'];
168+
return ['SIGINT', 'SIGBREAK', 'SIGHUP'];
169169
}
170170

171171
return ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'];
@@ -186,7 +186,6 @@ function createNativeSpawner({
186186
new Promise((resolve) => {
187187
const child = spawnProcess(command, args, { stdio: 'inherit' });
188188
const signalHandlers = new Map();
189-
const forwardedSignalSet = new Set();
190189
const cleanup = () => {
191190
for (const [signal, handler] of signalHandlers) {
192191
signalSource.off(signal, handler);
@@ -197,10 +196,6 @@ function createNativeSpawner({
197196
* @param {NodeJS.Signals} signal
198197
*/
199198
const handleSignal = (signal) => {
200-
if (forwardedSignalSet.has(signal)) {
201-
return;
202-
}
203-
forwardedSignalSet.add(signal);
204199
child.kill(signal);
205200
};
206201

‎apps/ccusage/src/cli.test.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void describe(resolveCliRuntime.name, () => {
127127
});
128128

129129
void describe(createNativeSpawner.name, () => {
130-
void it('forwards each supported launcher signal once', async () => {
130+
void it('forwards every supported launcher signal delivery', async () => {
131131
const signalSource = new EventEmitter();
132132
const kill = mock.fn(() => true);
133133
const child = /** @type {import('node:child_process').ChildProcess} */ (
@@ -150,7 +150,16 @@ void describe(createNativeSpawner.name, () => {
150150
assert.deepEqual(await resultPromise, { signal: null, status: 0 });
151151
assert.deepEqual(
152152
kill.mock.calls.map((call) => call.arguments),
153-
[['SIGINT'], ['SIGTERM'], ['SIGHUP'], ['SIGQUIT']],
153+
[
154+
['SIGINT'],
155+
['SIGINT'],
156+
['SIGTERM'],
157+
['SIGTERM'],
158+
['SIGHUP'],
159+
['SIGHUP'],
160+
['SIGQUIT'],
161+
['SIGQUIT'],
162+
],
154163
);
155164
const spawnCall = spawnProcess.mock.calls[0];
156165
assert.ok(spawnCall);
@@ -161,6 +170,41 @@ void describe(createNativeSpawner.name, () => {
161170
]);
162171
});
163172

173+
void it('forwards the supported Windows console signals', async () => {
174+
const signalSource = new EventEmitter();
175+
const kill = mock.fn(() => true);
176+
const child = /** @type {import('node:child_process').ChildProcess} */ (
177+
/** @type {unknown} */ (Object.assign(new EventEmitter(), { kill }))
178+
);
179+
const spawnNative = createNativeSpawner({
180+
platform: 'win32',
181+
signalSource,
182+
spawnProcess: () => child,
183+
});
184+
185+
const resultPromise = spawnNative('/native/bin/ccusage.exe', []);
186+
assert.deepEqual(
187+
['SIGINT', 'SIGBREAK', 'SIGHUP'].map((signal) => signalSource.listenerCount(signal)),
188+
[1, 1, 1],
189+
);
190+
signalSource.emit('SIGINT');
191+
signalSource.emit('SIGBREAK');
192+
signalSource.emit('SIGHUP');
193+
signalSource.emit('SIGTERM');
194+
assert.equal(signalSource.listenerCount('SIGTERM'), 0);
195+
assert.deepEqual(
196+
kill.mock.calls.map((call) => call.arguments),
197+
[['SIGINT'], ['SIGBREAK'], ['SIGHUP']],
198+
);
199+
child.emit('exit', 0, null);
200+
201+
assert.deepEqual(await resultPromise, { signal: null, status: 0 });
202+
assert.deepEqual(
203+
['SIGINT', 'SIGBREAK', 'SIGHUP'].map((signal) => signalSource.listenerCount(signal)),
204+
[0, 0, 0],
205+
);
206+
});
207+
164208
void it('removes signal listeners after the child exits', async () => {
165209
const signalSource = new EventEmitter();
166210
const kill = mock.fn(() => true);

0 commit comments

Comments
 (0)