Skip to content
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

(PUP-11897) Handle EOF in order to avoid busy-loop #9074

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/puppet/util/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ def self.execute(command, options = NoOptionsSpecified)
# Use non-blocking read to check for data. After each attempt,
# check whether the child is done. This is done in case the child
# forks and inherits stdout, as happens in `foo &`.

until results = Process.waitpid2(child_pid, Process::WNOHANG) #rubocop:disable Lint/AssignmentInCondition
# If we encounter EOF, though, then switch to a blocking wait for
# the child; after EOF, IO.select will never block and the loop
# below will use maximum CPU available.

wait_flags = Process::WNOHANG
until results = Process.waitpid2(child_pid, wait_flags) #rubocop:disable Lint/AssignmentInCondition

# If not done, wait for data to read with a timeout
# This timeout is selected to keep activity low while waiting on
Expand All @@ -235,6 +239,7 @@ def self.execute(command, options = NoOptionsSpecified)
output << reader.read_nonblock(4096) if ready
rescue Errno::EAGAIN
rescue EOFError
wait_flags &= ~Process::WNOHANG
end
end

Expand Down