Skip to content

Client event listener accumulation on reconnection #3210

@kuan121

Description

@kuan121

Description

The Client class has a bug where event listeners accumulate on each reconnection, causing the 'connected' event to fire multiple times (N+1 pattern) after each reconnect.

This issue was originally reported on Discord by a developer building a ConnectionManager class.

Steps to Reproduce

  1. Create a Client instance and add a 'connected' event listener
  2. Connect to an XRPL server
  3. Simulate network disconnect (e.g., close WebSocket or toggle WiFi)
  4. Wait for automatic reconnection
  5. Repeat steps 3-4 multiple times

Expected behavior:
The 'connected' event should fire exactly once per connection/reconnection.

Actual behavior:

  • 1st reconnect: 'connected' fires 1 time
  • 2nd reconnect: 'connected' fires 2 times
  • 3rd reconnect: 'connected' fires 3 times
  • 4th reconnect: 'connected' fires 4 times
  • And so on (N+1 pattern)

Example Code

const { Client } = require('xrpl');

class ConnectionManager {
  constructor() {
    this.client = new Client('wss://s.altnet.rippletest.net:51233');
    
    this.client.on('connected', () => {
      console.log('Connected to XRPL');
      // This will be called multiple times after each reconnection
    });
  }
  
  async connect() {
    await this.client.connect();
  }
}

const manager = new ConnectionManager();
await manager.connect();

// Disconnect WiFi, then reconnect
// First time: "Connected to XRPL" appears once ✓
// Second time: "Connected to XRPL" appears twice ❌
// Third time: "Connected to XRPL" appears three times ❌

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions