Skip to content

Commit

Permalink
Catch sub process crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCastro committed Oct 29, 2018
1 parent cbb9d85 commit 11e10d1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/process/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ process.on('message', function(msg) {
}
});

process.on('uncaughtException', function(err) {
if (!err.message) {
err = new Error(err);
}
process.send({
cmd: 'failed',
value: err
});
throw err;
});

function wrapJob(job) {
job.progress = function(progress) {
process.send({
Expand Down
3 changes: 3 additions & 0 deletions lib/process/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = function(processFile, childPool) {
}

child.on('message', handler);
child.on('exit', function(exitCode) {
reject(new Error('Unexpected exit code: ' + exitCode));
});
});

return done.finally(function() {
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/fixture_processor_crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* A processor file to be used in tests.
*
*/

var Promise = require('bluebird');

module.exports = function(job) {
setTimeout(function() {
if (typeof job.data.exitCode !== 'number') {
throw new Error('boom!');
}
process.exit(job.data.exitCode);
}, 100);

return new Promise(function() {});
};
46 changes: 46 additions & 0 deletions test/test_sandboxed_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,52 @@ describe('sandboxed process', function() {
queue.add({ foo: 'bar' });
});

it('should fail if the process crashes', function() {
queue.process(__dirname + '/fixtures/fixture_processor_crash.js');

return queue
.add({})
.then(function(job) {
return job.finished().reflect();
})
.then(function(inspection) {
expect(inspection.isRejected()).to.be.eql(true);
expect(inspection.reason().message).to.be.eql('boom!');
});
});

it('should fail if the process exits 0', function() {
queue.process(__dirname + '/fixtures/fixture_processor_crash.js');

return queue
.add({ exitCode: 0 })
.then(function(job) {
return job.finished().reflect();
})
.then(function(inspection) {
expect(inspection.isRejected()).to.be.eql(true);
expect(inspection.reason().message).to.be.eql(
'Unexpected exit code: 0'
);
});
});

it('should fail if the process exits non-0', function() {
queue.process(__dirname + '/fixtures/fixture_processor_crash.js');

return queue
.add({ exitCode: 1 })
.then(function(job) {
return job.finished().reflect();
})
.then(function(inspection) {
expect(inspection.isRejected()).to.be.eql(true);
expect(inspection.reason().message).to.be.eql(
'Unexpected exit code: 1'
);
});
});

it('should remove exited process', function(done) {
queue.process(__dirname + '/fixtures/fixture_processor_exit.js');

Expand Down

0 comments on commit 11e10d1

Please sign in to comment.