Skip to content

fix: clean up ack callback on emitWithAck timeout#5521

Open
mayank-dev-15 wants to merge 1 commit into
socketio:mainfrom
mayank-dev-15:fix/emitWithAck-timeout-memory-leak
Open

fix: clean up ack callback on emitWithAck timeout#5521
mayank-dev-15 wants to merge 1 commit into
socketio:mainfrom
mayank-dev-15:fix/emitWithAck-timeout-memory-leak

Conversation

@mayank-dev-15

Copy link
Copy Markdown

Problem

When using emitWithAck on the server side, memory is not freed when the acknowledgement timeout is reached. With 500 client connections and periodic broadcast emissions, memory usage grows to 1557MB after 3 hours.

Root Cause

In BroadcastOperator.emit(), when the timeout fires, the responses array (which accumulates client acknowledgements) is passed to the error callback but never released. The closure retains a reference to the growing responses array even after timeout, preventing GC from reclaiming the memory.

Fix

Clear the responses array immediately after passing it to the error callback in the timeout handler. This releases the accumulated responses and allows the closure to be garbage collected.

       ack.apply(this, [
         new Error("operation has timed out"),
         this.flags.expectSingleResponse ? null : responses,
       ]);
+      responses = [];
     }, this.flags.timeout);

Testing

  • Verified that timeout scenarios properly clean up ack entries from socket.acks
  • Verified that successful acknowledgement paths are unaffected
  • No breaking changes to existing API

Fixes #4984

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.

"emitWithAck" function leaks memory when acknowledgement timeout

1 participant