Sha256: 7ac1ad8c153433f81cdec03d60f740b59048f137f2f7e8d87d727f3f7f70a4a2

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module SolargraphTestCoverage
  # https://stackoverflow.com/questions/1076257/returning-data-from-forked-processes
  # When called with a block, runs the content of said block in a new (forked) process
  # the return value of the process/block can be captured and used in parent process
  class ForkProcess
    def self.call(&block)
      new.run(&block)
    end

    def initialize
      @read, @write = IO.pipe
    end

    def run(&block)
      pid = fork do
        @read.close
        Marshal.dump(run_block_with_timeout(&block), @write)
        exit!(0) # skips exit handlers.
      end

      @write.close
      result = @read.read

      Process.wait(pid)
      raise ChildFailedError, "Couldn't read pipe" if result.nil?

      Marshal.load(result).tap do |r|
        raise ChildFailedError, "Marshal.load(result) returned nil" if r.nil?
        raise ChildFailedError, r.message if r.is_a? Exception
      end
    end

    private

    def run_block_with_timeout(&block)
      Timeout.timeout(30000, &block)
    rescue StandardError => e
      e
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solargraph_test_coverage-0.3.1.1 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.3.1 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.3.0 lib/solargraph_test_coverage/fork_process.rb