Fix PyInstaller shutdown errors on macOS #4930
Open
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.
In frozen (PyInstaller) builds, disable multiprocessing by limiting workers to 1. This
prevents shutdown errors caused by worker processes attempting to import modules after
PyInstaller cleanup begins.
Fixes #4823
Description
When using the prebuilt Black binary on macOS (built with PyInstaller), users encounter
a
FileNotFoundErrorforbase_library.zipduring shutdown. This happens becauseProcessPoolExecutorworker processes attempt to import modules after PyInstaller hasbegun cleanup of the frozen environment.
The fix adds a check for
sys.frozeninreformat_many()to limit workers to 1 infrozen builds, which forces use of
ThreadPoolExecutorinstead ofProcessPoolExecutor. This avoids the shutdown race condition while still allowingBlack to function correctly.
This approach mirrors the existing fallback behavior for environments that don't support
multiprocessing (like AWS Lambda or Termux).
Checklist - did you ...
--previewstyle, following thestability policy?
CHANGES.mdif necessary?Note on tests: No automated tests can be added for this fix because the issue only
manifests in PyInstaller-frozen builds. The existing test
test_works_in_mono_process_only_environmentverifies the mono-process fallback logicworks correctly.