Skip to content

Conversation

@Ankitsinghsisodya
Copy link
Contributor

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 FileNotFoundError for base_library.zip during shutdown. This happens because
ProcessPoolExecutor worker processes attempt to import modules after PyInstaller has
begun cleanup of the frozen environment.

The fix adds a check for sys.frozen in reformat_many() to limit workers to 1 in
frozen builds, which forces use of ThreadPoolExecutor instead of
ProcessPoolExecutor. This avoids the shutdown race condition while still allowing
Black 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 ...

  • Implement any code style changes under the --preview style, following the
    stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

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_environment verifies the mono-process fallback logic
works correctly.

Ankitsinghsisodya and others added 2 commits December 30, 2025 08:04
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 psf#4823
@github-actions
Copy link

github-actions bot commented Dec 31, 2025

diff-shades results comparing this PR (c676452) to main (14713ac):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@cobaltt7
Copy link
Collaborator

cobaltt7 commented Jan 1, 2026

Hey @FrankPortman, are you able to test this fix? I've built the mac binary already (build logs)

(25.12.0 binary for comparison)

Copilot AI review requested due to automatic review settings January 23, 2026 02:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a shutdown-time import race in PyInstaller-frozen Black binaries (notably on macOS) by preventing ProcessPoolExecutor usage in frozen environments.

Changes:

  • In reformat_many(), detect frozen builds via sys.frozen and force workers = 1 to avoid multiprocessing workers.
  • Add a packaging changelog entry describing the PyInstaller shutdown fix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/black/concurrency.py Forces single-worker execution in frozen builds to avoid ProcessPoolExecutor shutdown/import issues.
CHANGES.md Documents the PyInstaller/frozen-build shutdown fix under Packaging.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cobaltt7
Copy link
Collaborator

Fuzz error is unrelated to this PR

@cobaltt7 cobaltt7 merged commit 5cdb4b6 into psf:main Jan 23, 2026
53 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PyInstaller related issue when using the prebuilt Black binary on Mac OSX

2 participants