Skip to content
Open
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
16 changes: 9 additions & 7 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Sandbox(options) {
self._message_queue = [];

self.options = {
timeout: 500,
timeout: options && options.timeout !== undefined ? options.timeout : 500,
node: 'node',
shovel: path.join(__dirname, 'shovel.js')
};
Expand Down Expand Up @@ -96,12 +96,14 @@ Sandbox.prototype.run = function(code, hollaback) {
self.child.stdin.write(code);
self.child.stdin.end();

self.child.timer = setTimeout(function() {
this.parent.stdout.removeListener('output', output);
stdout = JSON.stringify({ result: 'TimeoutError', console: [] });
this.parent.kill('SIGKILL');
}, self.options.timeout);
self.child.timer.parent = self.child;
if(self.options.timeout){
self.child.timer = setTimeout(function() {
this.parent.stdout.removeListener('output', output);
stdout = JSON.stringify({ result: 'TimeoutError', console: [] });
this.parent.kill('SIGKILL');
}, self.options.timeout);
self.child.timer.parent = self.child;
}

};

Expand Down
22 changes: 21 additions & 1 deletion test/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,24 @@ describe('Sandbox', function() {
});
});

});
it('should keep the process alive when timeout is explicitly disabled', function(done){
sb = new Sandbox({timeout : null}); //disable timeout
var messageHandler = sinon.spy();
var messageHandler2 = sinon.spy();
var num_messages_sent = 0;
var interval = setInterval(function(){
sb.postMessage(++num_messages_sent);
}, 100);
sb.on('message', messageHandler);
sb.run('onmessage = function (msg) { postMessage(msg); };', messageHandler2);
setTimeout(function(){
messageHandler.callCount.should.eql(num_messages_sent);
num_messages_sent.should.be.greaterThan(0);

messageHandler2.callCount.should.eql(0); //messageHandler2 should never be called
clearInterval(interval);
done();
},1000);
});

});