When closing Streamable HTTP connection and session termination fails it leaves the http connection open.
The current close method has a clear problem: if terminate_session raises (e.g., the server returns a 500), the exception propagates and neither cleanup_sse_resources nor cleanup_connection ever runs, leaving HTTP connections and SSE threads leaked.
RubyLLM::MCP::Errors::TransportError (Failed to terminate session: 500)
|
def close |
|
terminate_session |
|
cleanup_sse_resources |
|
cleanup_connection |
|
end |
Maybe change:
def close
terminate_session
cleanup_sse_resources
cleanup_connection
end
to something like
def close
terminate_session
ensure
begin
cleanup_sse_resources
ensure
cleanup_connection
end
end
This guarantees:
- cleanup_sse_resources always runs — even if terminate_session raises (500, connection refused, timeout, etc.)
- cleanup_connection always runs — even if cleanup_sse_resources itself raises
- The original exception still propagates — if no subsequent exception occurs in the ensure blocks, the caller still sees the TransportError from terminate_session
When closing Streamable HTTP connection and session termination fails it leaves the http connection open.
The current close method has a clear problem: if terminate_session raises (e.g., the server returns a 500), the exception propagates and neither cleanup_sse_resources nor cleanup_connection ever runs, leaving HTTP connections and SSE threads leaked.
ruby_llm-mcp/lib/ruby_llm/mcp/native/transports/streamable_http.rb
Lines 136 to 140 in 4d10851
Maybe change:
to something like
This guarantees: