-
Notifications
You must be signed in to change notification settings - Fork 15
Improve output capture for short-lived processes in System module #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,23 +157,38 @@ def system(cmd, *args, sudo: false, env: ENV.to_h, stdin: nil, **kwargs, &block) | |
| } | ||
| end | ||
|
|
||
| process_reaped = false | ||
| previous_trailing = Hash.new('') | ||
| loop do | ||
| break if Process.wait(pid, Process::WNOHANG) | ||
| ios = [err_r, out_r] | ||
|
|
||
| ios = [err_r, out_r].reject(&:closed?) | ||
| next if ios.empty? | ||
| loop do | ||
| # Break only when child exited AND there are no more open pipes | ||
| break if ios.empty? && process_reaped | ||
|
|
||
| readers, = IO.select(ios, [], [], 1) | ||
| next if readers.nil? # If IO.select times out we iterate again so we can check if the process has exited | ||
|
|
||
| readers.each do |io| | ||
| readers&.each do |io| | ||
| data, trailing = split_partial_characters(io.readpartial(4096)) | ||
| handlers[io].call(previous_trailing[io] + data) | ||
| previous_trailing[io] = trailing | ||
| rescue IOError | ||
| # Pipe closed - this is expected when process exits | ||
| io.close | ||
| ios.delete(io) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the process has exited, the size of this array controls the loop exit. If IO.select continues to timeout, this code won't be reached. It looks like there's a possibility of:
I guess this depends on what I'm not convinced that this could happen. If it did occur, then the Since this condition is logically possible, the new implementation should also handle it. Preferably explicitly. |
||
| end | ||
|
|
||
| # Non-blocking reap of child; only do it once | ||
| process_reaped ||= !Process.wait(pid, Process::WNOHANG).nil? | ||
| end | ||
|
|
||
| # Ensure we've fully reaped the child (if WNOHANG never caught it) | ||
| Process.wait(pid) unless process_reaped | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop isn't exited until If the loop somehow did exit with a live process that needs waiting on, there's the potential for data to appear on the pipes. Outside the loop, nothing is calling |
||
|
|
||
| # Flush any remaining trailing fragments so nothing is lost | ||
| previous_trailing.each do |io, trailing| | ||
| next if trailing.empty? | ||
|
|
||
| handlers[io].call(trailing) | ||
| end | ||
|
|
||
| $CHILD_STATUS | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redo this comment in context with the new implementation so that readers understand why the readers.each loop needs to be attempted.