Repository navigation
Fix websocket client leaking a connection on every deploy - #5971
Merged
knolleary merged 1 commit intoOct 6, 2026
Merged
Conversation
|
|
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
force-pushed
the
fix-websocket-client-connection-leak
branch
from
September 29, 2026 03:32
26f2f86 to
baa553f
Compare
knolleary
approved these changes
Oct 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/errorlisteners, 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 outcallsstartconn()when_inputNodes.length === 0websocket incallsstartconn()again fromregisterInputNodewhen_inputNodes.length === 1Config 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 doesnew ws()and thennode.server = socket. The second call drops the first socket fromnode.serverand never closes it. Deploy only closesnode.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(). Theircloseanderrorhandlers schedulenode.startconn()after 3 seconds whenevernode.closingis false. They are not cleared whennode.serveris 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 onnode.serveris pinged. A server that requires ping traffic closes the others. Each of thosecloseevents callsstartconn()again, which creates another socket and abandons the previousnode.serversocket (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 length0and callsstartconn().Fix
Keep the #5506 behaviour (do not connect unless some node needs the client) and make the connection singular:
registerUser/deregisterUser).startconn()runs only when the first user appears.CONNECTINGorOPEN,startconn()returns. A dead socket has its listeners removed and is terminated before a replacement is created, so an oldclosehandler cannot open another connection.close()on a missing socket.Test plan
node node_modules/mocha/bin/mocha.js test/nodes/core/network/22-websocket_spec.js— 33 passing