Repository navigation
fix(session): Prevent memory leak in pendingResponses on request timeout/cancellation - #287
Open
beomsampyeon wants to merge 1 commit into
Open
beomsampyeon wants to merge 1 commit into
beomsampyeon wants to merge 1 commit into
Conversation
…out/cancellation Fixes a potential memory leak in the `pendingResponses` map within both `McpClientSession` and `McpServerSession`. Problem: When a request is initiated using the `sendRequest()` method, a `MonoSink` is stored in the `pendingResponses` map, awaiting a response from the peer (server or client). If this peer does not respond and the operation times out (due to the `.timeout()` operator), or if the `Mono` returned by `sendRequest()` is cancelled for other reasons before a response is received, the original code did not explicitly remove the `MonoSink` from the `pendingResponses` map. This could lead to an accumulation of unresolved sinks, causing a memory leak over time, especially in scenarios with many unanswered or timed-out requests. Solution: This commit introduces an `onDispose()` callback within the `Mono.create()` block in the `sendRequest()` method of both `McpClientSession` and `McpServerSession`. This `onDispose()` handler ensures that `pendingResponses.remove(requestId)` is reliably invoked when the `MonoSink` is disposed for any reason, including: - Timeout triggered by the `.timeout()` operator. - Explicit cancellation by a downstream subscriber. This change ensures that resources associated with pending requests are cleaned up properly, enhancing the stability and robustness of the session management in both client and server implementations. Affected files: - McpClientSession.java - McpServerSession.java
This branch has not been deployed
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.
This PR fixes a potential memory leak in the
pendingResponsesmap within bothMcpClientSessionandMcpServerSessionwhen requests time out or are cancelled.Motivation and Context
A potential memory leak exists in the
pendingResponsesmap within bothMcpClientSessionandMcpServerSession.Problem Details:
When a request is initiated using the
sendRequest()method, aMonoSinkis stored in thependingResponsesmap, awaiting a response from the peer (server or client). If this peer does not respond and the operation times out (due to the.timeout()operator), or if theMonoreturned bysendRequest()is cancelled for other reasons before a response is received, the original code did not explicitly remove theMonoSinkfrom thependingResponsesmap.This could lead to an accumulation of unresolved sinks, causing a memory leak over time, especially in scenarios with many unanswered or timed-out requests. This change is needed to enhance the stability and robustness of the session management.
How Has This Been Tested?
onDisposehandler, which is a well-understood pattern for resource cleanup in asynchronous scenarios like timeouts or cancellations.onDisposehandler would be invoked as expected to remove entries frompendingResponses.Breaking Changes
None. This change only affects internal resource management and does not alter any public APIs or existing behavior from a user's perspective.
Types of changes
Checklist
Additional context
Solution Details:
This commit introduces an
onDispose()callback within theMono.create()block in the
sendRequest()method of bothMcpClientSessionandMcpServerSession. ThisonDispose()handler ensures thatpendingResponses.remove(requestId)is reliably invoked when theMonoSinkis disposed for any reason, including:.timeout()operator.This approach ensures that resources associated with pending requests are
cleaned up properly under various termination conditions.