queue up signal handlers so they do not get overwritten.

This commit is contained in:
Christopher Jeffrey 2014-09-30 15:14:53 -07:00
parent ad5ae36a2f
commit 057ab4da03
2 changed files with 25 additions and 2 deletions

View File

@ -46,11 +46,25 @@ function Bitcoin(options) {
Bitcoin.prototype.__proto__ = EventEmitter.prototype;
// Make sure signal handlers are not overwritten
Bitcoin._signalQueue = [];
Bitcoin._processOn = process.on;
process.addListener =
process.on = function(name, listener) {
if (~['SIGINT', 'SIGHUP', 'SIGQUIT'].indexOf(name.toUpperCase())) {
if (!Bitcoin.global || !Bitcoin.global._started) {
Bitcoin._signalQueue.push([name, listener]);
return;
}
}
return on.apply(this, arguments);
};
Bitcoin.prototype.start = function(callback) {
var self = this;
if (this._started) return;
this._started = true;
if (this._startCalled) return;
this._startCalled = true;
var none = {};
var isSignal = {};
@ -61,6 +75,8 @@ Bitcoin.prototype.start = function(callback) {
var errorCaught = none;
this.log_pipe = bitcoindjs.start(function(err, status) {
self._started = true;
[sigint, sighup, sigquit].forEach(function(signal) {
process.on(signal.name, signal.listener = function() {
if (process.listeners(signal.name).length > 1) {
@ -75,6 +91,12 @@ Bitcoin.prototype.start = function(callback) {
});
});
// Finally set signal handlers
process.on = process.addListener = Bitcoind._processOn;
Bitcoin._signalQueue.forEach(function(event) {
process.on(event[0], event[1]);
});
var exit = process.exit;
self._exit = function() {
return exit.apply(process, arguments);

View File

@ -478,6 +478,7 @@ start_node(void) {
// drop the bitcoind signal handlers - we want our own
signal(SIGINT, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
return 0;
}