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 2c76cba commit d48c495
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
11 changes: 7 additions & 4 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ class BlenoBindings extends EventEmitter {
}

init () {
this.onSigIntBinded = this.onSigInt.bind(this);
this._sigIntHandler = this.onSigInt.bind(this);
this._exitHandler = this.onExit.bind(this);

process.on('SIGINT', this.onSigIntBinded);
process.on('exit', this.onExit.bind(this));
process.on('SIGINT', this._sigIntHandler);
process.on('exit', this._exitHandler);

this._gap.on('advertisingStart', this.onAdvertisingStart.bind(this));
this._gap.on('advertisingStop', this.onAdvertisingStop.bind(this));
Expand All @@ -102,6 +103,8 @@ class BlenoBindings extends EventEmitter {
}

stop () {
process.removeListener('SIGINT', this._sigIntHandler);
process.removeListener('exit', this._exitHandler);
this.stopAdvertising();
this.disconnect();
this._hci.stop();
Expand Down Expand Up @@ -208,7 +211,7 @@ class BlenoBindings extends EventEmitter {
onSigInt () {
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);
Expand Down
3 changes: 2 additions & 1 deletion lib/hci-socket/hci.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Hci extends EventEmitter {
}
}

stop() {
stop () {
this._socket.removeAllListeners();
this._socket.stop();
}
Expand Down Expand Up @@ -589,6 +589,7 @@ class Hci extends EventEmitter {
debug('\t\talready closed');
continue;
}

if (pkts > this._handleAclsInProgress[handle_]) {
// Linux kernel may send acl packets by itself, so be ready for underflow
this._handleAclsInProgress[handle_] = 0;
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"node-gyp-build": "^4.8.1"
},
"optionalDependencies": {
"@stoprocent/bluetooth-hci-socket": "^1.4.2",
"@stoprocent/bluetooth-hci-socket": "^1.4.5",
"bplist-parser": "0.3.2",
"xpc-connect": "^3.0.0"
},
Expand Down

0 comments on commit d48c495

Please sign in to comment.