From 057ab4da037187a5d0f7a823820d8eaff74268c3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 30 Sep 2014 15:14:53 -0700 Subject: [PATCH] queue up signal handlers so they do not get overwritten. --- lib/bitcoind.js | 26 ++++++++++++++++++++++++-- src/bitcoindjs.cc | 1 + 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index d77e1336..2b088f16 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -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); diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 0a79f22d..2def4cc3 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -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; }