Open titanism opened 5 months ago
You can see the code here from ws
that causes this uncaught exception:
close(code, data) {
if (this.readyState === WebSocket.CLOSED) return;
if (this.readyState === WebSocket.CONNECTING) {
const msg = 'WebSocket was closed before the connection was established';
abortHandshake(this, this._req, msg); // <----------- RIGHT HERE EXCEPTION IS THROWN
return;
}
abortHandshake
will throw the error on the process.nextTick
here https://github.com/websockets/ws/blob/0d1b5e6c4acad16a6b1a1904426eb266a5ba2f72/lib/websocket.js#L1096
How do you call _disconnect? this error seems to happen when you manually invoke reconnect
method? could you check the readyState
before invoke?
@bytemain This gets invoked when you call wsp.close()
or when a timeout occurs, e.g. _handleError
is called which calls this._disconnect
, which then triggers this.
I see it, seems a bug here, might we should check this._ws.readyState === this.CLOSING
first?
Actually what is probably better is to check if readyState !== CONNECTING
but when ws
abort connection, it set readyState to CLOSING first: https://github.com/websockets/ws/blob/0d1b5e6c4acad16a6b1a1904426eb266a5ba2f72/lib/websocket.js#L1077
The logic that throws this error results from checking if it is equal to CONNECTING as mentioned already.
Please see the code here https://github.com/websockets/ws/blob/0d1b5e6c4acad16a6b1a1904426eb266a5ba2f72/lib/websocket.js#L290-L294.
Therefore, it is is NOT connecting, !== CONNECTING
then don't call close
.
@bytemain
why? we actually need abort connection that is connecting, because here we are reconnecting, if dont abort it, we will have orphen conection
It seems to throw an uncaught exception if you try to call close
in an instance of ws
that is in a CONNECTING
state (even if you wrap it with a try/catch block).
@bytemain just making sure you saw my comment here https://github.com/opensumi/reconnecting-websocket/pull/10#discussion_r1656500138
Thank you! 🙏 🙇 😄
There is a bug in the codebase right now in that
_disconnect
is invoked which subsequently callsthis._ws.close
. Despite there being a try/catch wrapper, if you use thews
library, an exception is thrown and your process will exit.cc @bytemain @abdelmagied94 @Zerounary can you please merge this fix into your library at https://github.com/opensumi/reconnecting-websocket for the npm package
@opensumi/reconnecting-websocket
and patch version bump and publish to npm afterwards to v4.4.1 (it currently on v4.4.0)