Skip to content

Commit

Permalink
fix: cleanup exit and sigint listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Feb 24, 2025
1 parent ecb4333 commit 847ebe2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
19 changes: 11 additions & 8 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ NobleBindings.prototype.updateRssi = function (peripheralUuid) {
};

NobleBindings.prototype.init = function () {
this.onSigIntBinded = this.onSigInt.bind(this);
/* Add exit handlers after `init()` has completed. If no adaptor
is present it can throw an exception - in which case we don't
want to try and clear up afterwards (issue #502) */
this._sigIntHandler = this.onSigInt.bind(this);
this._exitHandler = this.stop.bind(this);
process.on('SIGINT', this._sigIntHandler);
process.on('exit', this._exitHandler);

this._gap.on('scanParametersSet', this.onScanParametersSet.bind(this));
this._gap.on('scanStart', this.onScanStart.bind(this));
Expand All @@ -122,25 +128,22 @@ NobleBindings.prototype.init = function () {
this._hci.on('aclDataPkt', this.onAclDataPkt.bind(this));

this._hci.init();

/* Add exit handlers after `init()` has completed. If no adaptor
is present it can throw an exception - in which case we don't
want to try and clear up afterwards (issue #502) */
process.on('SIGINT', this.onSigIntBinded);
process.on('exit', this.stop.bind(this));
};

NobleBindings.prototype.onSigInt = function () {
const sigIntListeners = process.listeners('SIGINT');

if (sigIntListeners[sigIntListeners.length - 1] === this.onSigIntBinded) {
if (sigIntListeners[sigIntListeners.length - 1] === this._sigIntHandler) {
// we are the last listener, so exit
// this will trigger onExit, and clean up
process.exit(1);
}
};

NobleBindings.prototype.stop = function () {
process.removeListener('exit', this._exitHandler);
process.removeListener('SIGINT', this._sigIntHandler);

this.stopScanning();
for (const handle in this._aclStreams) {
this._hci.disconnect(handle);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"node-gyp-build": "^4.8.1"
},
"optionalDependencies": {
"@stoprocent/bluetooth-hci-socket": "^1.4.4"
"@stoprocent/bluetooth-hci-socket": "^1.4.5"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
Expand Down
10 changes: 7 additions & 3 deletions test/lib/hci-socket/bindings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ describe('hci-socket bindings', () => {
describe('onSigInt', () => {
it('should exit', () => {
const sigIntListeners = process.listeners('SIGINT');
bindings.onSigIntBinded = sigIntListeners[sigIntListeners.length - 1];
bindings._sigIntHandler = sigIntListeners[sigIntListeners.length - 1];
bindings.onSigInt();
assert.calledOnceWithExactly(process.exit, 1);
});

it('should not exit', () => {
bindings.onSigIntBinded = sinon.spy();
bindings._sigIntHandler = sinon.spy();
bindings.onSigInt();
assert.notCalled(process.exit);
});
Expand Down Expand Up @@ -292,7 +292,9 @@ describe('hci-socket bindings', () => {
bindings._gap.stopScanning = fake.resolves(null);
bindings._hci.reset = fake.resolves(null);
bindings._hci.stop = fake.resolves(null);

bindings._sigIntHandler = fake.resolves(null);
bindings._exitHandler = fake.resolves(null);

bindings.stop();

assert.calledOnce(bindings._gap.stopScanning);
Expand All @@ -305,6 +307,8 @@ describe('hci-socket bindings', () => {
bindings._hci.disconnect = fake.resolves(null);
bindings._hci.reset = fake.resolves(null);
bindings._hci.stop = fake.resolves(null);
bindings._sigIntHandler = fake.resolves(null);
bindings._exitHandler = fake.resolves(null);

bindings._aclStreams = [1, 2, 3];

Expand Down

0 comments on commit 847ebe2

Please sign in to comment.