Skip to content

Fix websocket client leaking a connection on every deploy - #5971

Merged
knolleary merged 1 commit into
node-red:mainfrom
yuanzhiqing:fix-websocket-client-connection-leak
Oct 6, 2026
Merged

knolleary merged 1 commit into
node-red:mainfrom
yuanzhiqing:fix-websocket-client-connection-leak

Conversation

@yuanzhiqing

Copy link
Copy Markdown
Contributor

Summary

Node-RED 5.0 has updated the WebSocket client to only establish connections when necessary (#5533). However, this change introduces the following issue: when using both websocket in and websocket out processes on the same client, an additional connection is established each time the deployment is made (closing one connection and opening two connections). Connections that are no longer tracked still have close / error listeners, Against a server that closes sockets which are not receiving pings, this becomes a reconnect loop.
4.1.0 is unaffected: the client config called startconn() once when it was created.

Cause

#5506 / #5533 (5.0.0) correctly stopped the client from connecting when nobody is using it, for example a disabled flow. The new startup logic has two independent callers:

  • websocket out calls startconn() when _inputNodes.length === 0
  • the first websocket in calls startconn() again from registerInputNode when _inputNodes.length === 1
    Config nodes are created first, then the flow nodes in file order. websocket out is often created before websocket in, so both calls run.
    startconn() always does new ws() and then node.server = socket. The second call drops the first socket from node.server and never closes it. Deploy only closes node.server, so the remote sees one close and two opens (net +1) on every deploy. Earlier abandoned sockets stay open.
    Those abandoned sockets still run handleConnection(). Their close and error handlers schedule node.startconn() after 3 seconds whenever node.closing is false. They are not cleared when node.server is overwritten.
    Heartbeat makes this worse. There is a single node.heartbeatInterval, and it is replaced when the newest socket opens, so only the socket currently stored on node.server is pinged. A server that requires ping traffic closes the others. Each of those close events calls startconn() again, which creates another socket and abandons the previous node.server socket (that one then also stops receiving pings). The result is a reconnect loop, not a single leaked connection.
    The same double-start happens when several websocket out nodes share one client: out nodes never join _inputNodes, so every out node sees length 0 and calls startconn().

Fix

Keep the #5506 behaviour (do not connect unless some node needs the client) and make the connection singular:

  • websocket in and websocket out both register on one user set (registerUser / deregisterUser).
  • startconn() runs only when the first user appears.
  • The socket is closed when the last user leaves, and the 3s reconnect timer is cleared. Reconnect runs only while users remain and the config node is not closing.
  • If a socket is already CONNECTING or OPEN, startconn() returns. A dead socket has its listeners removed and is terminated before a replacement is created, so an old close handler cannot open another connection.
  • Closing a client that never connected (disabled flow, no users) completes the close callback instead of calling close() on a missing socket.

Test plan

  • node node_modules/mocha/bin/mocha.js test/nodes/core/network/22-websocket_spec.js — 33 passing
  • New test: one websocket in and one websocket out on the same client open exactly one connection
  • New test: a full redeploy of that flow still has exactly one connection
  • Manual: Node-RED 5.0.x, one websocket client shared by in and out, deploy repeatedly. The remote should show one close and one open, and the connection count should stay at 1
  • Manual: remote closes sockets that do not receive pings. With heartbeat enabled, the managed socket should stay up; no extra clients should appear
  • Manual: disable the flow that uses the client. It should stop connecting (the Flow disabled but websocket client config node keep connecting #5506 case)

@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: yuanzhiqing / name: yuanzhiqing (baa553f)

Node-RED 5 started the client from both websocket-out and websocket-in. The second startconn() replaced node.server without closing the previous socket, so each deploy closed one connection and opened two. Listeners on the abandoned socket still scheduled a reconnect, which loops when the remote closes connections that are not receiving pings.
@yuanzhiqing
yuanzhiqing force-pushed the fix-websocket-client-connection-leak branch from 26f2f86 to baa553f Compare September 29, 2026 03:32
@knolleary
knolleary merged commit 6c37070 into node-red:main Oct 6, 2026
3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants