Skip to content

Continuous increase of 'connect' event listeners when manually reconnecting #12

@goosy

Description

@goosy

Description

When using S7Endpoint with autoReconnect disabled and manually managing reconnection, the number of 'connect' event listeners keeps increasing. This behavior doesn't occur when using the built-in autoReconnect feature.

Steps to Reproduce

  1. Create an S7Endpoint instance with autoReconnect set to 0.
  2. Add a 'newListener' event handler to log new listeners.
  3. Implement a manual reconnection logic using setInterval.

Expected Behavior

The number of 'connect' event listeners should remain constant, regardless of reconnection attempts.

Actual Behavior

The number of 'connect' event listeners increases with each reconnection attempt, potentially leading to memory leaks or performance issues.

Code to Reproduce

import { S7Endpoint } from '@st-one-io/nodes7';

let plc = new S7Endpoint({ host: '192.168.201.210:102', rack: 0, slot: 1, autoReconnect: 0 });

plc.on('newListener', (eventName, listener) => {
    console.log(`New listener added to ${eventName} event`);
    console.log(`Listener is:`, listener.toString());
    console.log(`Listener count:`, plc.eventNames().map(name => name + ': ' + plc.listenerCount(name)).join(', '));
});

setInterval(async () => {
    if (!plc.isConnected) plc.connect().catch(err => console.log(err));
}, 1000)

Environment

Node.js version: 22.4.0
@st-one-io/nodes7 version: 1.1.0

Additional Observations

  • When autoReconnect is enabled (by setting it to a positive number), this issue does not occur. This suggests that the library's built-in reconnection logic handles listener management more effectively.
  • The increasing listeners appear to be native code functions, as listener.toString() outputs function () { [native code] }.

Questions

  • Is this behavior intended?
  • If not, is there a recommended way to manually manage reconnections without causing listener proliferation?
  • Are there any best practices for managing connections and listeners when using S7Endpoint with manual reconnection logic?

Impact

This issue could potentially lead to memory leaks or degraded performance in long-running applications that require frequent reconnections.

Thank you for your attention to this matter. I appreciate any insights or guidance you can provide.

Metadata

Metadata

Assignees

No one assigned

    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