Class: TIMEx::Strategies::Subprocess

Inherits:
Base
  • Object
show all
Defined in:
lib/timex/strategies/subprocess.rb

Overview

Note:

The child inherits all open file descriptors; shared connections can corrupt the parent if reused in the child. Re-open resources in the child or close inherited FDs deliberately.

Runs user code in a forked child and returns the marshalled result to the parent.

See Also:

Constant Summary collapse

EMPTY_PAYLOAD =

Message used when the child exits without producing a marshalled result (segfault, OOM, exec, etc.) so the parent can distinguish from a legitimate ‘[:ok, nil]` return.

"the child exited without producing a result"
DEFAULT_MAX_PAYLOAD_BYTES =

Default ceiling on the marshalled child payload. A buggy or malicious block can otherwise drive the parent OOM by streaming arbitrary data through the pipe before the deadline fires.

8 * 1024 * 1024

Instance Method Summary collapse

Methods inherited from Base

call, #call

Methods included from NamedComponent

included

Constructor Details

#initialize(kill_after: 0.5, max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES) ⇒ Subprocess

Returns a new instance of Subprocess.

Parameters:

  • kill_after (Numeric) (defaults to: 0.5)

    seconds to wait after TERM before KILL when reaping

  • max_payload_bytes (Integer) (defaults to: DEFAULT_MAX_PAYLOAD_BYTES)

    hard cap on bytes read from the result pipe

Raises:

  • (ArgumentError)

    when parameters are out of range



27
28
29
30
31
32
33
34
# File 'lib/timex/strategies/subprocess.rb', line 27

def initialize(kill_after: 0.5, max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES)
  super()
  raise ArgumentError, "kill_after must be a non-negative Numeric" unless kill_after.is_a?(Numeric) && !kill_after.negative?
  raise ArgumentError, "max_payload_bytes must be a positive Integer" unless max_payload_bytes.is_a?(Integer) && max_payload_bytes.positive?

  @kill_after = kill_after
  @max_payload_bytes = max_payload_bytes
end