Module: Mutant::Isolation::Fork

Defined in:
lib/mutant/isolation.rb

Class Method Summary collapse

Class Method Details

.call(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call block in isolation

This isolation implements the fork strategy. Future strategies will probably use a process pool that can handle multiple mutation kills, in-isolation at once.

rubocop:disable MethodLength

Returns:

  • (Object)

    returns block execution result

Raises:

  • (Error)

    if block terminates abnormal


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mutant/isolation.rb', line 42

def self.call(&block)
  reader, writer = IO.pipe.map(&:binmode)

  pid = Process.fork do
    File.open('/dev/null', 'w') do |file|
      $stderr.reopen(file)
      reader.close
      writer.write(Marshal.dump(block.call))
      writer.close
    end
  end

  writer.close
  Marshal.load(reader.read)
rescue => exception
  raise Error, exception
ensure
  Process.waitpid(pid) if pid
end