Skip to content

Commit 0b6a7d8

Browse files
authored
Cancel pending transactions when calling destroy (#521)
* add jsdocs * allow to pass more opts to socket * allow to pass AbortSignal to socket * add abort signal example * lint the code * remove vscode/ from gitignore * drop node v14 (reached EOL April 2023) * cancel pending transactions on destroy * remove comments
1 parent 9a74d61 commit 0b6a7d8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,20 @@ class ModbusRTU extends EventEmitter {
637637
return false;
638638
}
639639

640+
/**
641+
* Clears the timeout for all pending transactions.
642+
* This essentially cancels all pending requests.
643+
*/
644+
_cancelPendingTransactions() {
645+
if (Object.keys(this._transactions).length > 0) {
646+
Object.values(this._transactions).forEach((transaction) => {
647+
if (transaction._timeoutHandle) {
648+
_cancelTimeout(transaction._timeoutHandle);
649+
}
650+
});
651+
}
652+
}
653+
640654
/**
641655
* Close the serial port
642656
*
@@ -661,6 +675,9 @@ class ModbusRTU extends EventEmitter {
661675
* or failure.
662676
*/
663677
destroy(callback) {
678+
// cancel all pending requests as we're closing the port
679+
this._cancelPendingTransactions();
680+
664681
// close the serial port if exist and it has a destroy function
665682
if (this._port && this._port.destroy) {
666683
this._port.removeAllListeners("data");

0 commit comments

Comments
 (0)