Skip to content

[Bug]: RubyLLM::MCP::Native::Transports::StreamableHTTP#close doesn't close connections #138

@ebeigarts

Description

@ebeigarts

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions