Skip to content

Commit 4aeae98

Browse files
committed
prevent duplicate event emissions on flaky connections with multiple sequential reconnect attempts
1 parent 3442918 commit 4aeae98

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/xrpl/HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
55
## Unreleased
66

77
### Fixed
8-
* Fix event listener accumulation bug where `'connected'` event handlers would fire multiple times after each reconnection. Changed `.on()` to `.once()` in the Client constructor's reconnect handler to prevent listener accumulation.
8+
* Fix event listener accumulation bug where `'connected'` event handlers would fire multiple times after each reconnection. The fix cleans up stale listeners from previous reconnect attempts to prevent duplicate event emissions on flaky connections with multiple sequential reconnect attempts.
99

1010
## 4.6.0 (2026-02-12)
1111

packages/xrpl/src/client/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,19 @@ class Client extends EventEmitter<EventTypes> {
262262
this.emit('error', errorCode, errorMessage, data)
263263
})
264264

265+
let connectedListener: (() => void) | undefined
265266
this.connection.on('reconnect', () => {
266-
this.connection.once('connected', () => this.emit('connected'))
267+
// Clean up any stale listener from previous reconnect attempt
268+
// This prevents duplicate event emissions when multiple reconnect attempts
269+
// occur before a successful connection (e.g., on flaky networks)
270+
if (connectedListener !== undefined) {
271+
this.connection.off('connected', connectedListener)
272+
}
273+
274+
connectedListener = (): boolean => this.emit('connected')
275+
// Use .once() so the listener auto-removes after firing
276+
// This prevents listener accumulation across reconnections
277+
this.connection.once('connected', connectedListener)
267278
})
268279

269280
this.connection.on('disconnected', (code: number) => {

0 commit comments

Comments
 (0)