Class: Gemlings::Sandbox::ThreadExecutor

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

Overview

JRuby: 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

available?, inherited

Instance Method Details

#call(timeout, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gemlings/sandbox.rb', line 94

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