system_command: improve EOF handling

This commit is contained in:
Bo Anderson 2021-04-01 15:42:16 +01:00
parent c7e183ef9f
commit e30f2af987
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

@ -197,10 +197,10 @@ class SystemCommand
sig { params(sources: T::Array[IO], _block: T.proc.params(type: Symbol, line: String).void).void }
def each_line_from(sources, &_block)
loop do
readable_sources, = IO.select(sources)
readable_sources = T.must(readable_sources).reject(&:eof?)
sources_remaining = sources.dup
while sources_remaining.present?
readable_sources, = IO.select(sources_remaining)
readable_sources = T.must(readable_sources)
break if readable_sources.empty?
@ -208,12 +208,15 @@ class SystemCommand
line = source.readline_nonblock || ""
type = (source == sources[0]) ? :stdout : :stderr
yield(type, line)
rescue IO::WaitReadable, EOFError
rescue EOFError
source.close_read
sources_remaining.delete(source)
rescue IO::WaitReadable
next
end
end
sources.each(&:close_read)
sources_remaining.each(&:close_read)
end
# Result containing the output and exit status of a finished sub-process.