Skip to content

Commit

Permalink
fix(sandbox): handle broken processor files
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed May 5, 2021
1 parent 7679ada commit 2326983
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/classes/child-pool.ts
Expand Up @@ -32,15 +32,39 @@ const convertExecArgv = async (execArgv: string[]): Promise<string[]> => {
return standard.concat(convertedArgs);
};

const exitCodesErrors: { [index: number]: string } = {
1: 'Uncaught Fatal Exception',
2: 'Unused',
3: 'Internal JavaScript Parse Error',
4: 'Internal JavaScript Evaluation Failure',
5: 'Fatal Error',
6: 'Non-function Internal Exception Handler',
7: 'Internal Exception Handler Run-Time Failure',
8: 'Unused',
9: 'Invalid Argument',
10: 'Internal JavaScript Run-Time Failure',
12: 'Invalid Debug Argument',
13: 'Unfinished Top-Level Await',
};

async function initChild(child: ChildProcess, processFile: string) {
const onComplete = new Promise<void>(resolve => {
const onComplete = new Promise<void>((resolve, reject) => {
const onMessageHandler = (msg: any) => {
if (msg.cmd === 'init-complete') {
resolve();
child.off('message', onMessageHandler);
}
};
child.on('message', onMessageHandler);
child.on('close', (code, signal) => {
if (code > 128) {
code -= 128;
}
const msg = exitCodesErrors[code] || `Unknown exit code ${code}`;
reject(
new Error(`Error initializing child: ${msg} and signal ${signal}`),
);
});
});
await new Promise(resolve =>
child.send({ cmd: 'init', value: processFile }, resolve),
Expand All @@ -52,8 +76,6 @@ export class ChildPool {
retained: { [key: number]: ChildProcessExt } = {};
free: { [key: string]: ChildProcessExt[] } = {};

constructor() {}

async retain(processFile: string): Promise<ChildProcessExt> {
const _this = this;
let child = _this.getFree(processFile).pop();
Expand All @@ -69,11 +91,8 @@ export class ChildPool {
try {
await stat(masterFile); // would throw if file not exists
} catch (_) {
try {
masterFile = path.join(process.cwd(), 'dist/classes/master.js');
await stat(masterFile);
} finally {
}
masterFile = path.join(process.cwd(), 'dist/classes/master.js');
await stat(masterFile);
}

child = fork(masterFile, [], { execArgv });
Expand Down
1 change: 1 addition & 0 deletions src/test/fixtures/fixture_processor_broken.js
@@ -0,0 +1 @@
throw new Error('Broken file processor');
14 changes: 14 additions & 0 deletions src/test/test_sandboxed_process.ts
Expand Up @@ -298,6 +298,20 @@ describe('sandboxed process', () => {
);
});

it('should fail if the process file is broken', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_broken.js';

new Worker(queueName, processFile, {
drainDelay: 1,
});

const job = await queue.add('test', { exitCode: 1 });

await expect(job.waitUntilFinished(queueEvents)).to.be.rejectedWith(
'Error initializing child: Internal Exception Handler Run-Time Failure',
);
});

it('should remove exited process', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_exit.js';

Expand Down

0 comments on commit 2326983

Please sign in to comment.