Skip to content

Commit

Permalink
Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Apr 28, 2024
1 parent 50d33ed commit 1646ed6
Show file tree
Hide file tree
Showing 117 changed files with 30 additions and 13,626 deletions.
14 changes: 9 additions & 5 deletions lib/resolve/wait-subprocess.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {once} from 'node:events';
import {once, on} from 'node:events';
import {isStream as isNodeStream} from 'is-stream';
import {errorSignal} from '../terminate/kill.js';
import {errorSignal, isRepeatKillError} from '../terminate/kill.js';
import {throwOnTimeout} from '../terminate/timeout.js';
import {isStandardStream} from '../utils/standard-stream.js';
import {TRANSFORM_TYPES} from '../stdio/type.js';
Expand Down Expand Up @@ -62,8 +62,8 @@ export const waitForSubprocessResult = async ({
...originalPromises,
...customStreamsEndPromises,
]),
throwOnSubprocessError(subprocess, controller),
throwOnInternalError(subprocess, controller),
throwOnSubprocessError(subprocess, controller),
...throwOnTimeout(subprocess, timeout, context, controller),
]);
} catch (error) {
Expand Down Expand Up @@ -97,8 +97,12 @@ const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors

// Fails when the subprocess emits an `error` event
const throwOnSubprocessError = async (subprocess, {signal}) => {
const [error] = await once(subprocess, 'error', {signal});
throw error;
for await (const [error] of on(subprocess, 'error', {signal})) {
console.log('EVENT', {error});
if (!isRepeatKillError(error)) {
throw error;
}
}
};

// Fails right away when calling `subprocess.kill(error)`.
Expand Down
18 changes: 17 additions & 1 deletion lib/terminate/kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const subprocessKill = (
) => {
const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);
emitKillError(subprocess, error);
const killResult = kill(signal);
const killResult = safeKill(kill, signal);
setKillTimeout({
kill,
signal,
Expand All @@ -41,6 +41,15 @@ export const subprocessKill = (
return killResult;
};

const safeKill = (kill, signal) => {
try {
return kill(signal);
} catch (error) {
console.log('THROWING', {error});
throw error;
}
};

const parseKillArguments = (signalOrError, errorArgument, killSignal) => {
const [signal = killSignal, error] = isErrorInstance(signalOrError)
? [undefined, signalOrError]
Expand Down Expand Up @@ -86,3 +95,10 @@ const shouldForceKill = (signal, forceKillAfterDelay, killSignal, killResult) =>
const normalizeSignal = signal => typeof signal === 'string'
? os.constants.signals[signal.toUpperCase()]
: signal;

// Calling `subprocess.kill()` on a process that has already exited emits an `error` event on Windows.
// However, it does not do it on Unix. This ignores the error to make it cross-platform.
// `subprocess.kill()` returns `false` in both cases.
export const isRepeatKillError = error => error instanceof Error
&& error.syscall === 'kill'
&& error.code === 'EPERM';
104 changes: 0 additions & 104 deletions test/arguments/cwd.js

This file was deleted.

39 changes: 0 additions & 39 deletions test/arguments/encoding-option.js

This file was deleted.

32 changes: 0 additions & 32 deletions test/arguments/env.js

This file was deleted.

100 changes: 0 additions & 100 deletions test/arguments/escape.js

This file was deleted.

0 comments on commit 1646ed6

Please sign in to comment.