From d83b8002effb909665a3912e15f56496fc1cfe48 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Tue, 11 May 2021 13:27:26 +0100 Subject: [PATCH] system_command: better handle race conditions when interrupting --- Library/Homebrew/system_command.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/system_command.rb b/Library/Homebrew/system_command.rb index ddce9d8113..a5f30806ad 100644 --- a/Library/Homebrew/system_command.rb +++ b/Library/Homebrew/system_command.rb @@ -200,23 +200,26 @@ class SystemCommand 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?