Skip to content

Commit

Permalink
system_command: better handle race conditions when interrupting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo98 committed May 11, 2021
1 parent fca0fb1 commit d83b800
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Library/Homebrew/system_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,26 @@ def each_output_line(&block)
write_input_to(raw_stdin)
raw_stdin.close_write

thread_ready_queue = Queue.new
thread_done_queue = Queue.new
line_thread = Thread.new do
Thread.handle_interrupt(ProcessTerminatedInterrupt => :never) do
thread_ready_queue << true
each_line_from [raw_stdout, raw_stderr], &block
end
# Handle race conditions with interrupts
Thread.current.report_on_exception = false
thread_done_queue.pop
rescue ProcessTerminatedInterrupt
nil
end
Thread.pass

end_time = Time.now + @timeout if @timeout
raise Timeout::Error if raw_wait_thr.join(end_time&.remaining).nil?

@status = raw_wait_thr.value

thread_ready_queue.pop
line_thread.raise ProcessTerminatedInterrupt.new
thread_done_queue << true
line_thread.join
rescue Interrupt
Process.kill("INT", pid) if pid && !sudo?
Expand Down

0 comments on commit d83b800

Please sign in to comment.