Skip to content

fix: don't reconnect a websocket after close() - #120

Merged
EnriqueL8 merged 5 commits into
hyperledger-firefly:mainfrom
kaleido-io:websocket-reconnect
Sep 14, 2026
Merged

EnriqueL8 merged 5 commits into
hyperledger-firefly:mainfrom
kaleido-io:websocket-reconnect

Conversation

@annamcallister

Copy link
Copy Markdown
Contributor

Problem

FireFlyWebSocket.close() isn't terminal. A closed socket can come back two ways:

  1. close() never cleared reconnectTimer — only connect() did. Closing during the reconnect backoff leaves the timer running; it later fires and calls connect().
  2. reconnect() had no notion of "already closed" — the unexpected-response handler calls it from a deferred stream flush, so a rejection still being drained when close() ran schedules a fresh retry afterwards.

Either way the socket reconnects after the application has closed it and dropped its reference, so nothing is left that can stop it, and it retries every reconnectDelay indefinitely.

Since socketOptions headers are fixed at construction, if the connection was authenticated with a credential that later expires, every retry fails — an endless stream of rejected connections from a socket the application believes is gone.

Fix

A closing flag:

  • close() sets it, and clears reconnectTimer
  • reconnect() returns early when set
  • connect() clears it, so reopening still works

Both halves of close() are load-bearing: the flag stops a future reconnect() call, while the clearTimeout stops an already-scheduled retry — a pending timer calls connect() directly and never goes through reconnect().

connect()'s own reconnectTimer teardown is removed, since close() (called on the line above) now owns it.

Signed-off-by: Anna McAllister <anna.mcallister@kaleido.io>
Signed-off-by: Anna McAllister <anna.mcallister@kaleido.io>
@annamcallister
annamcallister requested a review from a team as a code owner September 14, 2026 12:29
Signed-off-by: Anna McAllister <anna.mcallister@kaleido.io>
Signed-off-by: Anna McAllister <anna.mcallister@kaleido.io>
Signed-off-by: Anna McAllister <anna.mcallister@kaleido.io>

@EnriqueL8 EnriqueL8 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @annamcallister looks good

node-version: '24.x'
registry-url: 'https://registry.npmjs.org'
# Ensure npm 11.5.1 or later required by trusted publishing
- name: Update npm

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the node-version 24 includes 11.5.1?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes- 24.x should resolve to node 24.21.0/ npm 11.19.0 right now, and obviously only ever go up

@EnriqueL8
EnriqueL8 merged commit dce7f0a into hyperledger-firefly:main Sep 14, 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