Fix #4984: Clean up socket.acks on emitWithAck timeout to prevent memory leak#5461
Open
cobyfrombrooklyn-bot wants to merge 1 commit intosocketio:mainfrom
Open
Fix #4984: Clean up socket.acks on emitWithAck timeout to prevent memory leak#5461cobyfrombrooklyn-bot wants to merge 1 commit intosocketio:mainfrom
cobyfrombrooklyn-bot wants to merge 1 commit intosocketio:mainfrom
Conversation
When using emitWithAck with a timeout, if clients don't respond before the timeout fires, the ack callbacks stored in socket.acks are never cleaned up. Over time this causes unbounded memory growth proportional to the number of timed-out broadcasts. This adds a removeAcks() method to the in-memory adapter that iterates matching sockets and deletes the orphaned ack entry. The broadcast operator calls it when the timeout fires, before invoking the error callback. 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.
Problem
When using
emitWithAckwith a timeout, if clients don't respond before the timeout fires, the ack callbacks stored insocket.acksare never cleaned up. Each timed-out broadcast leaves orphaned entries in the acks Map, causing unbounded memory growth.As reported in #4984, with 500 clients and frequent broadcasts where clients don't acknowledge, memory usage grew to 1.5GB in 3 hours.
Root Cause
broadcastWithAckin the adapter stores an ack callback insocket.acks.set(packet.id, ack)for each matching socket. When a client responds,socket.onack()deletes the entry. But when the timeout fires, only the caller's ack callback is invoked with an error; the per-socket entries insocket.acksare never removed.Fix
removeAcks(packetId, opts)to the in-memory adapter. It iterates the same set of matching sockets (usingapply()) and deletes the ack entry for the given packet ID.BroadcastOperator.emit()to callremoveAcks()before invoking the error callback. Uses a feature check (typeof this.adapter.removeAcks === 'function') for backward compatibility with custom adapters.Test
Added
should clean up socket.acks after emitWithAck timeout (memory leak fix)intest/messaging-many.ts. The test:emitWithAckwith a 200ms timeoutacksmaps are emptyThe test fails without the fix (acks map retains the orphaned entry) and passes with it.
Tested locally on macOS ARM (Apple Silicon). All existing ack-related tests continue to pass.