Fix ThreadPool#shutdown timeout accuracy #2221
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes up
ThreadPool#shutdown
so that the providedtimeout
value (which corresponds to theforce_shutdown_after
configuration option) is more accurate, and can be set as a float instead of just an integer value.Previously, the shutdown timeout ran a loop
timeout.times
, which calledt.join 1
on each thread (one after another, not parallel), followed by asleep 1
each loop iteration, thent.join SHUTDOWN_GRACE_TIME
on each thread after raisingForceShutdown
. Instead of forcing shutdown intimeout
(+SHUTDOWN_GRACE_TIME
) seconds, this forced a shutdown intimeout * (n + 1)
(+n * SHUTDOWN_GRACE_TIME
) seconds (wheren
is the number of running threads). The updated code fixes this by joining a maximum oftimeout
seconds (followed by a maximum ofgrace
seconds) across all threads.I also added an extra step at the end that calls
kill
on any remaining threads still alive after the grace period, plus another short (1-second) join to wait for force-killed threads to (finally) exit, as an extra precaution that threads are fully cleaned up when shutdown returns. I thought this might be helpful, but could be removed as it's not strictly needed for the rest of this PR.Your checklist for this pull request
[changelog skip]
the pull request title.[ci skip]
to the title of the PR.#issue
" to the PR description or my commit messages.