Skip to content
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

websocket closes unexpectedly, causing Node Red to crash #231

Open
mduchain opened this issue Apr 19, 2024 · 0 comments
Open

websocket closes unexpectedly, causing Node Red to crash #231

mduchain opened this issue Apr 19, 2024 · 0 comments

Comments

@mduchain
Copy link

Hello all,
Today I noticed my NR was restarting almost every 3 minutes.
The crash/restart was caused by an uncaught exception on timeout of the ping sent every 30s.

19 Apr 18:03:21 - [red] Uncaught Exception:
19 Apr 18:03:21 - [error] Error: Can't send data because WebSocket is not opened.
 at exports.throwIf (/x/.node-red/node_modules/websocket-as-promised/src/utils.js:4:11)
 at WebSocketAsPromised.send (/x/.node-red/node_modules/websocket-as-promised/src/index.js:248:5)
 at Timeout._onTimeout (/x/.node-red/node_modules/ewelink-api/src/mixins/openWebSocket.js:39:17)
 at listOnTimeout (node:internal/timers:559:17)
 at processTimers (node:internal/timers:502:7)

After digging a bit, noticed the connection was getting closed on the server side.
Fiddling with the heartbeat timer didn't help (tried with 10s).

So, i fixed/patched it by catching the exception and restarting the connection.
Offending original code, ewelink-api/src/mixins/openWebSocket.js

...
await wsp.open();
await wsp.send(payloadLogin);
setInterval(async () => {
      await wsp.send('ping');
}, heartbeat);

return wsp;

Patched code:

    ...
    await wsp.open();
    await wsp.send(payloadLogin);

    setInterval(async () => {
      try {
        await wsp.send('ping');
      } catch (error) {
        console.error('websocket-as-promised - heartbeat failed');
        await wsp.open();
        await wsp.send(payloadLogin);
      }
    }, heartbeat);

    return wsp;

This seems to have fixed it for me. Every now and then the connection drops,
and at HB timeout the connection is re-established.
Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant