2021-05-05 18:44:01 +01:00
|
|
|
# frozen_string_literal: false
|
2021-05-06 13:22:54 +01:00
|
|
|
|
|
|
|
require 'fcntl'
|
2021-05-05 18:44:01 +01:00
|
|
|
|
|
|
|
module Terrapin
|
|
|
|
module MultiPipeExtensions
|
2021-05-06 13:22:54 +01:00
|
|
|
def initialize
|
|
|
|
@stdout_in, @stdout_out = IO.pipe
|
|
|
|
@stderr_in, @stderr_out = IO.pipe
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
clear_nonblocking_flags!
|
2021-05-05 18:44:01 +01:00
|
|
|
end
|
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
def pipe_options
|
|
|
|
# Add some flags to explicitly close the other end of the pipes
|
2023-05-04 04:54:26 +01:00
|
|
|
{ :out => @stdout_out, :err => @stderr_out, @stdout_in => :close, @stderr_in => :close }
|
2021-05-06 13:22:54 +01:00
|
|
|
end
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
def read
|
|
|
|
# While we are patching Terrapin, fix child process potentially getting stuck on writing
|
|
|
|
# to stderr.
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
@stdout_output = +''
|
|
|
|
@stderr_output = +''
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
fds_to_read = [@stdout_in, @stderr_in]
|
|
|
|
until fds_to_read.empty?
|
|
|
|
rs, = IO.select(fds_to_read)
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
read_nonblocking!(@stdout_in, @stdout_output, fds_to_read) if rs.include?(@stdout_in)
|
|
|
|
read_nonblocking!(@stderr_in, @stderr_output, fds_to_read) if rs.include?(@stderr_in)
|
2021-05-05 18:44:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
# @param [IO] io IO Stream to read until there is nothing to read
|
|
|
|
# @param [String] result Mutable string to which read values will be appended to
|
|
|
|
# @param [Array<IO>] fds_to_read Mutable array from which `io` should be removed on EOF
|
|
|
|
def read_nonblocking!(io, result, fds_to_read)
|
|
|
|
while (partial_result = io.read_nonblock(8192))
|
|
|
|
result << partial_result
|
2021-05-05 18:44:01 +01:00
|
|
|
end
|
2021-05-06 13:22:54 +01:00
|
|
|
rescue IO::WaitReadable
|
|
|
|
# Do nothing
|
|
|
|
rescue EOFError
|
|
|
|
fds_to_read.delete(io)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_nonblocking_flags!
|
|
|
|
# Ruby 3.0 sets pipes to non-blocking mode, and resets the flags as
|
|
|
|
# needed when calling fork/exec-related syscalls, but posix-spawn does
|
|
|
|
# not currently do that, so we need to do it manually for the time being
|
|
|
|
# so that the child process do not error out when the buffers are full.
|
|
|
|
stdout_flags = @stdout_out.fcntl(Fcntl::F_GETFL)
|
|
|
|
@stdout_out.fcntl(Fcntl::F_SETFL, stdout_flags & ~Fcntl::O_NONBLOCK) if stdout_flags & Fcntl::O_NONBLOCK
|
2021-05-05 18:44:01 +01:00
|
|
|
|
2021-05-06 13:22:54 +01:00
|
|
|
stderr_flags = @stderr_out.fcntl(Fcntl::F_GETFL)
|
|
|
|
@stderr_out.fcntl(Fcntl::F_SETFL, stderr_flags & ~Fcntl::O_NONBLOCK) if stderr_flags & Fcntl::O_NONBLOCK
|
|
|
|
rescue NameError, NotImplementedError, Errno::EINVAL
|
|
|
|
# Probably on windows, where pipes are blocking by default
|
2021-05-05 18:44:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Terrapin::CommandLine::MultiPipe.prepend(Terrapin::MultiPipeExtensions)
|