Skip to content

Conversation

sophiebits
Copy link

The stop method cancels the timeout stored in nextTimer if it's set but we were only initializing it the first time we set the timeout, not for any subsequent ones. The stopped flag means that callback was not called, but this can still cause a memory leak and delay shutdown of the Node process.

Assigning each timeout to the nextTimer variable ensures that the clearTimeout call works correctly to invalidate the timeout that has been set.

Tested locally in a larger project that getInstance().stopPolling() allows a Node server to shut down successfully, versus before it would wait up to 30 seconds until the next scheduled poll.


labels: mergeable, bugfix

… one

The `stop` method cancels the timeout stored in `nextTimer` if it's set but we were only initializing it the first time we set the timeout, not for any subsequent ones. The `stopped` flag means that `callback` was not called, but this can still cause a memory leak and delay shutdown of the Node process.

Assigning each timeout to the nextTimer variable ensures that the clearTimeout call works correctly to invalidate the timeout that has been set.
@isaacanthony
Copy link

I think there is an additional race condition bug where if you call stop() while the poll() function is running (after the stopped check but before the setTimeout() call), it can reset the timer at the bottom of the poll() function even though stopped = true. I believe you have to add logic like the following:

  async function poll() {
    if (stopped) {
      if (nextTimer) {
        clearTimeout(nextTimer);
      }
      return;
    }
    ...

@sameerank
Copy link
Contributor

Hi, thanks for opening this PR!

I opened up a separate one so I could include the changes suggested in the above comment: #292

And you can see it has now been released and I cut a new node-server-sdk version to use the update

In short, please upgrade your node-server-sdk version to 3.12.1 to use the changes that you’ve proposed here

@sameerank sameerank closed this Aug 8, 2025
@sophiebits
Copy link
Author

Thanks to both of you!

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.

3 participants