Module: RubyLLM::MCP::Native::Transports::Support::Timeout

Included in:
RubyLLM::MCP::Native::Transports::SSE, RubyLLM::MCP::Native::Transports::Stdio, RubyLLM::MCP::Native::Transports::StreamableHTTP
Defined in:
lib/ruby_llm/mcp/native/transports/support/timeout.rb

Instance Method Summary collapse

Instance Method Details

#with_timeout(seconds, request_id: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_llm/mcp/native/transports/support/timeout.rb', line 9

def with_timeout(seconds, request_id: nil)
  result = nil
  exception = nil

  worker = Thread.new do
    result = yield
  rescue StandardError => e
    exception = e
  end

  if worker.join(seconds)
    raise exception if exception

    result
  else
    worker.kill # stop the thread (can still have some risk if shared resources)
    worker.join(0.1)
    raise RubyLLM::MCP::Errors::TimeoutError.new(
      message: "Request timed out after #{seconds} seconds",
      request_id: request_id
    )
  end
end