New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection stuck in readyState 0 (CONNECTING) #1948
Comments
It does not seem to be a |
Sorry I do not mention that ws client is connecting to simple ws:// not wss:// |
What is the default value of Lines 717 to 721 in 055949f
Is that true, or am I missing something? I also can't find where that |
Correct. There is no default. The client will wait indefinitely unless there are some other OS settings to close inactive sockets. See https://nodejs.org/api/http.html#http_http_request_url_options_callback. |
I'm closing this. Discussion can continue if needed. |
So is code example bellow correct to avoid this issue?
How can I please simulate it works? In piece of code shared by @besworks Could you please define some default handshakeTimeout to avoid this CONNECTING stuck/hangs issue by default? Im asking because readyState 2 (CLOSING) has default timeout... Line 36 in 055949f
Line 602 in 055949f
opts.timeout ? Thanks.
|
The last question.... when I define handshakeTimeout will be close event fired in case of timeout? Because in close event only I have reconnection mechanism. |
I have simulated handshakeTimeout by setting low value e.g. 10 ms and it works and both error and close events are fired so my reconnect in close callback works. I think you should set default handshakeTimeout e.g. to 30 sec such as close timeout to avoid this issue ;) |
For me, it still doesn't work. It's stuck on ws connection code const ws = (this.connection = WebSocket.create(gateway, wsQuery, { handshakeTimeout : 30000 }));
// Set it to 10ms if you want to test the timeout, it does work but not when it hits 4009 randomly interval to check if ready state ever changes from if (closeCode === 4009) {
setInterval(() => {
this.debug(`WS State [Interval 5s]: ${CONNECTION_STATE[this.connection?.readyState]}`);
}, 5000);
} |
@legendhimslef I think that your issue does not belong to this my issue. This issue is about CONNECTING state stuck because of as found out it unfortunately has no default value which is bad I think and I suggested to define some default but no answer from maintainer. But you have problem with CLOSING state. I think you are working something wrong because I have never had problem with stuck in CLOSING state and as mentioned above it have 30 sec timeout by default so close event should be fired and this state should not take longer then 30 sec. I have implemented terminating of zombie connections and reconnect mechanism is based on close event in many different projects and it works well long years - never happens that reconnect does not work. Maybe provide some minimum repo example and we will find some problem in your code. |
I found this issue from CLOSING state issue, you mentioned this issues there soo yeah.. WebSocket close does not work only on a specific closeCode [4009] ie session timeout and that too its rarely with others and frequent with mine. |
Is the close code received in the close frame? If so it does not make sense because the only way to read it is from a listener of the const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 0 }, function () {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
ws.on('close', function (code) {
console.log(code);
wss.close();
});
});
wss.on('connection', function (ws) {
ws.close(4009);
}); |
If I'm not wrong the In our case (@legendhimslef ) the here's our We've tried both |
@lpinca ws._socket.destroy(); its not the best solution but we couldn't find any working solution So we tried this and it works. We only do it when the |
|
If you check https://github.com/discordjs/discord.js/blob/988a51b7641f8b33cc9387664605ddc02134859d/src/client/websocket/WebSocketShard.js#L567 we aren't removing any listeners when getting 4009 code. We are removing listeners only in and this method isn't getting used in |
If you write a reproducible test case using only |
I have no idea, but doesn't this https://github.com/discordjs/discord.js/blob/988a51b7641f8b33cc9387664605ddc02134859d/src/client/websocket/WebSocketShard.js#L725 removes the listeners before the the Again, please try to reproduce the issue using only |
Yes but that is in the |
Description
I have never seen this and I use "ws" module for years but always in server mode (not client mode). But now there is a difference that I use it not on regular server/hosting as server with high speed internet connection but I use it on raspberry PI with NB-IoT hat modem (there is a limited internet speed; pings in about 400ms are common) as client and there are often disconnections (sometimes I have uptime 5 hours without disconnect but sometimes I have in about 10 disconnects per hour). I already have implemented heartbeat and reconnection (after 2 secs after close event). I send message (max. 20 bytes - values from sensor) each 5 minutes but now happens that it could not be send for more then 30 hours because of readyState 0 (CONNECTING). See log below. Of course on websocket server side this stuck client is 30 hours as offline. I know it seems to be rare what happened but question is - is it a bug? How long can be ws client in readyState 0 (CONNECTING)? I read here #1125 that in readyState 2 (CLOSING) it can be max. 30 second. But what about readyState 0 (CONNECTING)? Or should I implement some checking of readyState and if it is several minutes in readyState 0 (CONNECTING) I need to terminate connection which calls close event and fires my reconnect mechanism? Thanks.
ws version
7.5.1
Node.js Version
10.24.1
System
Expected result
Connection can not be in readyState 0 (CONNECTING) so long (several days).
Actual result
Connection is in readyState 0 (CONNECTING) too long (for 30 hours and still continues, it seems I will have to stop nodejs script)
Attachments
2021-09-15 14:39:54 WS Init
2021-09-15 14:40:09 WS Connected
2021-09-15 14:42:12 WS Terminated (zombie)
2021-09-15 14:42:12 WS Message Sending Error: WebSocket is not open: readyState 3 (CLOSED)
2021-09-15 14:42:14 WS Init
2021-09-15 14:42:15 WS Connected
2021-09-15 14:42:52 WS Terminated (zombie)
2021-09-15 14:42:54 WS Init
2021-09-15 14:42:56 WS Connected
2021-09-15 14:47:12 WS Message Sent
2021-09-15 14:48:32 WS Terminated (zombie)
2021-09-15 14:48:34 WS Init
2021-09-15 14:48:34 WS Connected
2021-09-15 14:51:32 WS Terminated (zombie)
2021-09-15 14:51:34 WS Init
2021-09-15 14:52:12 WS Message Sending Error: WebSocket is not open: readyState 0 (CONNECTING)
2021-09-15 14:52:38 WS Connected
2021-09-15 14:54:52 WS Terminated (zombie)
2021-09-15 14:54:54 WS Init
2021-09-15 14:57:12 WS Message Sending Error: WebSocket is not open: readyState 0 (CONNECTING)
2021-09-15 15:02:12 WS Message Sending Error: WebSocket is not open: readyState 0 (CONNECTING)
...
and the same each 5 minutes;
btw internet via NB-IoT works well (I have ping.log);
I switched system from NB-IoT to LAN cable but still in readyState 0 (CONNECTING), so this stuck/hangs seems to be permanent
...
2021-09-16 10:42:27 WS Message Sending Error: WebSocket is not open: readyState 0 (CONNECTING)
2021-09-16 10:47:27 WS Message Sending Error: WebSocket is not open: readyState 0 (CONNECTING)
...
etc.
UPDATE: I restarted nodejs script and ws error event was fired now and logged...
2021-09-16 11:48:29 Received signal SIGTERM
2021-09-16 11:48:29 WS Error WebSocket was closed before the connection was established
2021-09-16 11:48:33 WS Init
2021-09-16 11:48:33 WS Connected
The text was updated successfully, but these errors were encountered: