Fixes rare crash when lowering number of threads #2013
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
More details, including a hopefully easy repro that works on OSX, are in #1991.
The way this fix works is that instead of detaching threads, which by bad luck might stay running and access thread pool data after the thread pool is destroyed, we stay attached and move the threads into a list of threads that we have to explicitly wait on to be finished, just like we already do for the active threads.
Tests
The crash is very hard to reproduce without adding a 1s sleep, and even then I suspect the 1s worked for me because my process runs in under 1s. Maybe you'll need to sleep for at least as long as it takes to go from lowering the number of threads to terminating the process so that we can be assured the detached threads survive to the end?
Checklist:
have previously submitted a Contributor License Agreement
(individual, and if there is any way my
employers might think my programming belongs to them, then also
corporate).
(adding new test cases if necessary).