Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: server pendingResponses leak #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
taobaorun wants to merge 3 commits into modelcontextprotocol:main
base: main
Choose a base branch
Loading
from taobaorun:fix/fix_pendingresponse_leak
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

package io.modelcontextprotocol.spec;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.json.TypeRef;
import io.modelcontextprotocol.server.McpAsyncServerExchange;
import io.modelcontextprotocol.server.McpInitRequestHandler;
import io.modelcontextprotocol.server.McpNotificationHandler;
import io.modelcontextprotocol.server.McpRequestHandler;
import io.modelcontextprotocol.json.TypeRef;
import io.modelcontextprotocol.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoSink;
import reactor.core.publisher.Sinks;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/**
* Represents a Model Context Protocol (MCP) session on the server side. It manages
* bidirectional JSON-RPC communication with the client.
Expand All @@ -36,7 +36,9 @@ public class McpServerSession implements McpLoggableSession {

private final String id;

/** Duration to wait for request responses before timing out */
/**
* Duration to wait for request responses before timing out
*/
private final Duration requestTimeout;

private final AtomicLong requestCounter = new AtomicLong(0);
Expand Down Expand Up @@ -165,6 +167,8 @@ public <T> Mono<T> sendRequest(String method, Object requestParams, TypeRef<T> t
this.pendingResponses.remove(requestId);
sink.error(error);
});
// remove pending response when sink is disposed(e.g. timeout)
sink.onDispose(() -> this.pendingResponses.remove(requestId));
}).timeout(requestTimeout).handle((jsonRpcResponse, sink) -> {
if (jsonRpcResponse.error() != null) {
sink.error(new McpError(jsonRpcResponse.error()));
Expand Down Expand Up @@ -345,13 +349,15 @@ private MethodNotFoundError getMethodNotFoundError(String method) {

@Override
public Mono<Void> closeGracefully() {
// TODO: clear pendingResponses and emit errors?
this.pendingResponses.values().forEach(sink -> sink.error(new RuntimeException("Session closed")));
this.pendingResponses.clear();
return this.transport.closeGracefully();
}

@Override
public void close() {
// TODO: clear pendingResponses and emit errors?
this.pendingResponses.values().forEach(sink -> sink.error(new RuntimeException("Session closed")));
this.pendingResponses.clear();
this.transport.close();
}

Expand Down

AltStyle によって変換されたページ (->オリジナル) /