Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-runtime-worker-thread): close child process on parent disconnect #1349

Merged
merged 1 commit into from
Sep 21, 2022
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'path';
import { ChildProcess, fork } from 'child_process';
import { ChildProcess, fork, spawn } from 'child_process';
import { Caller, cancel, createCaller } from './rpc';
import { expect } from 'chai';
import { WorkerRuntime } from './worker-runtime';
import { once } from 'events';
import { promisify } from 'util';

const childProcessModulePath = path.resolve(
__dirname,
Expand All @@ -22,7 +24,7 @@ describe('child process worker proxy', () => {
}

if (childProcess) {
childProcess.kill('SIGTERM');
childProcess.disconnect();
childProcess = null;
}
});
Expand All @@ -34,4 +36,38 @@ describe('child process worker proxy', () => {
const result = await caller.evaluate('1 + 1');
expect(result.printable).to.equal(2);
});

it('should exit on its own when the parent process disconnects', async() => {
const intermediateProcess = spawn(process.execPath,
['-e', `require("child_process")
.fork(${JSON.stringify(childProcessModulePath)})
.on("message", function(m) { console.log("message " + m + " from " + this.pid) })`],
{ stdio: ['pipe', 'pipe', 'inherit'] });

// Make sure the outer child process runs and has created the inner child process
const [message] = await once(intermediateProcess.stdout.setEncoding('utf8'), 'data');
const match = message.trim().match(/^message ready from (?<pid>\d+)$/);
expect(match).to.not.equal(null);

// Make sure the inner child process runs
const childPid = +match.groups.pid;
process.kill(childPid, 0);

// Kill the intermediate process and wait for the inner child process to also close
intermediateProcess.kill('SIGTERM');
let innerChildHasStoppedRunning = false;
for (let i = 0; i < 200; i++) {
try {
process.kill(childPid, 0);
} catch (err) {
if (err.code === 'ESRCH') {
innerChildHasStoppedRunning = true;
break;
}
throw err;
}
await promisify(setTimeout)(10);
}
expect(innerChildHasStoppedRunning).to.equal(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const messageBus = createCaller(['emit', 'on'], process);

exposeAll(messageBus, workerProcess);

process.once('disconnect', () => process.exit());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, TIL!

process.nextTick(() => {
// eslint-disable-next-line chai-friendly/no-unused-expressions
process.send?.('ready');
Expand Down