Skip to content

Commit

Permalink
fix(sandbox): wait for result of sending start command
Browse files Browse the repository at this point in the history
  • Loading branch information
manast authored Feb 20, 2022
1 parent 2d6c8af commit 232ed85
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
16 changes: 5 additions & 11 deletions lib/process/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ let processor;
let currentJobPromise;

const { promisify } = require('util');

// same as process.send but waits until the send is complete

// the async version is used below because otherwise
// the termination handler may exit before the parent
// process has recived the messages it requires
const processSendAsync = promisify(process.send.bind(process));
const { asyncSend } = require('./utils');

// https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify
if (!('toJSON' in Error.prototype)) {
Expand Down Expand Up @@ -95,7 +89,7 @@ process.on('message', msg => {
currentJobPromise = (async () => {
try {
const result = (await processor(wrapJob(msg.job))) || {};
await processSendAsync({
await asyncSend(process, {
cmd: 'completed',
value: result
});
Expand All @@ -104,7 +98,7 @@ process.on('message', msg => {
// eslint-disable-next-line no-ex-assign
err = new Error(err);
}
await processSendAsync({
await asyncSend(process, {
cmd: 'failed',
value: err
});
Expand Down Expand Up @@ -156,7 +150,7 @@ function wrapJob(job) {
// so that we can return it from this process synchronously.
progressValue = progress;
// Send message to update job progress.
return processSendAsync({
return asyncSend(process, {
cmd: 'progress',
value: progress
});
Expand All @@ -178,7 +172,7 @@ function wrapJob(job) {
* Emulate the real job `log` function.
*/
job.log = function(row) {
return processSendAsync({
return asyncSend(process, {
cmd: 'log',
value: row
});
Expand Down
6 changes: 4 additions & 2 deletions lib/process/sandbox.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

const { asyncSend } = require('./utils');

module.exports = function(processFile, childPool) {
return function process(job) {
return childPool.retain(processFile).then(child => {
return childPool.retain(processFile).then(async child => {
let msgHandler;
let exitHandler;

child.send({
await asyncSend(child, {
cmd: 'start',
job: job
});
Expand Down
23 changes: 22 additions & 1 deletion lib/process/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ function killAsync(child, signal, timeoutMs) {
return onExit;
}

/*
asyncSend
Same as process.send but waits until the send is complete
the async version is used below because otherwise
the termination handler may exit before the parent
process has recived the messages it requires
*/

const asyncSend = (proc, msg) => {
return new Promise((resolve, reject) => {
proc.send(msg, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};

module.exports = {
killAsync
killAsync,
asyncSend
};

0 comments on commit 232ed85

Please sign in to comment.