diff --git a/lib/hbc/extend/io.rb b/lib/hbc/extend/io.rb new file mode 100644 index 0000000000000..1357293cdcc84 --- /dev/null +++ b/lib/hbc/extend/io.rb @@ -0,0 +1,10 @@ +class IO + def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR) + buffer = "" + buffer.concat(read_nonblock(1)) while buffer[-1] != sep + buffer + rescue IO::WaitReadable, EOFError => e + raise e if buffer.empty? + buffer + end +end diff --git a/lib/hbc/system_command.rb b/lib/hbc/system_command.rb index 1b915f033dfa4..46b22f8f4bf12 100644 --- a/lib/hbc/system_command.rb +++ b/lib/hbc/system_command.rb @@ -1,3 +1,4 @@ +require "hbc/extend/io" require "open3" require "shellwords" @@ -89,7 +90,11 @@ def each_line_from(sources) readable_sources = IO.select(sources)[0] readable_sources.delete_if(&:eof?).first(1).each do |source| type = (source == sources[0] ? :stdout : :stderr) - yield(type, source.gets || "") + begin + yield(type, source.readline_nonblock || "") + rescue IO::WaitReadable, EOFError + next + end end break if readable_sources.empty? end