Skip to content

Commit d469cd4

Browse files
authored
Merge pull request #34 from tmcw/allow-esrch
Allow process.kill to fail with ESRCH. Fixes #32.
2 parents 973d67e + 177e4f2 commit d469cd4

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/DenoWorker.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('DenoWorker', () => {
2222
const failScript = readFileSync(failFile, { encoding: 'utf-8' });
2323
const envFile = path.resolve(__dirname, './test/env.js');
2424
const envScript = readFileSync(envFile, { encoding: 'utf-8' });
25+
const memoryCrashFile = path.resolve(__dirname, './test/memory.js');
2526

2627
afterEach(() => {
2728
if (worker) {
@@ -1118,6 +1119,33 @@ describe('DenoWorker', () => {
11181119
expect(exitSignal).toBe('SIGKILL');
11191120
}
11201121
});
1122+
1123+
it('should gracefully handle Deno out-of-memory', async () => {
1124+
let denoProcesses = await getDenoProcesses();
1125+
expect(denoProcesses).toEqual([]);
1126+
worker = new DenoWorker(memoryCrashFile, {
1127+
denoV8Flags: ['--max-heap-size=10'],
1128+
logStdout: true,
1129+
});
1130+
worker.postMessage({});
1131+
1132+
const exitValues = await new Promise<
1133+
[number | null, string | null]
1134+
>((resolve) => {
1135+
worker.onexit = (...args) => resolve(args);
1136+
});
1137+
1138+
const isWindows = /^win/.test(process.platform);
1139+
1140+
if (isWindows) {
1141+
expect(typeof exitValues[0]).toEqual('number');
1142+
expect(exitValues[1]).toEqual(null);
1143+
} else {
1144+
expect(exitValues).toEqual([null, 'SIGTRAP']);
1145+
}
1146+
1147+
worker.terminate();
1148+
});
11211149
});
11221150
});
11231151

src/DenoWorker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export class DenoWorker {
420420
}
421421

422422
/**
423-
* Terminiates the worker and cleans up unused resources.
423+
* Terminates the worker and cleans up unused resources.
424424
*/
425425
terminate() {
426426
this._terminated = true;

src/Utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ function killWindows(pid: number) {
2929
}
3030

3131
function killUnix(pid: number) {
32-
const signal = 'SIGKILL';
33-
process.kill(pid, signal);
32+
try {
33+
const signal = 'SIGKILL';
34+
process.kill(pid, signal);
35+
} catch (e) {
36+
// Allow this call to fail with
37+
// ESRCH, which meant that the process
38+
// to be killed was already dead.
39+
// But re-throw on other codes.
40+
if (e.code !== 'ESRCH') {
41+
throw e;
42+
}
43+
}
3444
}
3545

3646
export interface ExecResult {

src/test/memory.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
self.onmessage = (event) => {
2+
let arrays = [];
3+
for (;;) {
4+
arrays.push(new Uint32Array(128));
5+
console.log(arrays.length);
6+
}
7+
};

0 commit comments

Comments
 (0)