Fix unhandled rejection on disconnect (#582)

If a link is closed while the prepareDisconnect action is in progress
the link will still try to signal the disconnect ready over the socket
when the prepareDisconnect action completes, which throws due to the
socket no longer being present.  This error is not caught due to the
catch happening before the finally on the promise and triggers an
unhandled rejection.

Fix by rearranging the finally to come before the catch on the promise
so that errors are always handled, and checking that the link still has
a session before attempting to send the disconnect ready signal.
This commit is contained in:
Hornwitser authored and GitHub committed 2024-02-05 23:36:43 +01:00
1 parent 520007c8d1
commit 48c31b4825
3 files changed
+10 -9

No files matched your search

+1
View File
@@ -5,6 +5,7 @@
## Changes
- Fixed an unhandled error in Subspace Storage crashing the host.
- Fixed an unhandled rejection in disconnect logic crashing the host.
## Version 2.0.0-alpha.16
+8 -9
View File
@@ -126,16 +126,15 @@ export class Link {
});
connector.on("disconnectPrepare", () => {
this.prepareDisconnect().catch(
err => {
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
this.prepareDisconnect().finally(() => {
// Only WebSocket connectors issue disconnection events.
const webSocketConnector = this.connector as WebSocketBaseConnector;
if (webSocketConnector.hasSession) {
webSocketConnector.sendDisconnectReady();
}
).finally(
() => {
// Only WebSocket connectors issue disconnection events.
(this.connector as WebSocketBaseConnector).sendDisconnectReady();
},
);
}).catch(err => {
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
});
});
connector.on("invalidate", () => {
+1
View File
@@ -73,6 +73,7 @@ class MockConnector extends lib.BaseConnector {
super(src, dst);
this.connected = true;
this.hasSession = true;
this.sentMessages = [];
}