fix: clean up socket.acks on broadcastWithAck timeout#5452
Open
veeceey wants to merge 1 commit intosocketio:mainfrom
Open
fix: clean up socket.acks on broadcastWithAck timeout#5452veeceey wants to merge 1 commit intosocketio:mainfrom
veeceey wants to merge 1 commit intosocketio:mainfrom
Conversation
When using emitWithAck with a timeout on broadcast operations, the ack callbacks stored in each socket's acks map were never cleaned up if the client didn't respond before the timeout fired. Over time this causes significant memory leaks, especially with many sockets and frequent broadcasts. The fix tracks which sockets received the broadcast ack and schedules cleanup of their acks map entries after the timeout period, matching the behavior already present in individual socket registerAckCallback. Fixes socketio#4984
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4984
When using
emitWithAckwith a timeout on broadcast operations (e.g.io.timeout(2000).to("room").emitWithAck("event", data)), the ack callbacks stored in each socket'sacksmap were never removed if the client didn't respond before the timeout.This causes a memory leak because
socket.acks.set(packet.id, ack)is called inbroadcastWithAckfor every target socket, but when the timeout fires inbroadcast-operator.ts, only the caller-side ack gets invoked - the per-socket entries insocket.acksare left behind indefinitely.The fix tracks which sockets received the broadcast ack and schedules cleanup of their
acksentries after the timeout period. This matches the existing pattern inregisterAckCallbackon individual sockets, which already cleans up on timeout (line 324 of socket.ts).The issue is straightforward to reproduce: broadcast
emitWithAckto a room where clients never call the callback, and observe thatsocket.acksgrows unboundedly. The reporter measured 1.5 GB after 3 hours with 500 clients.Includes unit tests verifying: