Class: Rubyagents::Sandbox::ThreadExecutor

Inherits:
Executor
  • Object
show all
Defined in:
lib/rubyagents/sandbox.rb

Overview

JRuby >= 10.0.3.0: fork is unavailable; use a thread with a join-based timeout. STDOUT_MUTEX serializes $stdout redirection so concurrent sandbox calls (e.g. managed agents) don't interleave captured output.

Constant Summary collapse

STDOUT_MUTEX =
Mutex.new

Instance Method Summary collapse

Methods inherited from Executor

for_platform

Instance Method Details

#call(timeout, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubyagents/sandbox.rb', line 67

def call(timeout, &block)
  result = nil
  error  = nil

  thread = Thread.new do
    STDOUT_MUTEX.synchronize { result = block.call }
  rescue => e
    error = "#{e.class}: #{e.message}"
  end

  unless thread.join(timeout)
    thread.kill
    return { error: "Execution timed out after #{timeout}s" }
  end

  error ? { error: error } : result
end