Skip to content

Manager not properly shut down in concurrency module - violates multiprocessing best practices #4950

Closed

Description

@Bitshifter-9

In src/black/concurrency.py, schedule_formatting() creates a multiprocessing.Manager() when write_back is DIFF or COLOR_DIFF, but the Manager is never explicitly shut down.
code location:

async def schedule_formatting(...):
lock = None
if write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
manager = Manager()
lock = manager.Lock()

Since Manager starts a separate server process, not calling shutdown() (or using a context manager) can lead to resource leaks when Black is used as a library or repeatedly in a long-running process. This is mostly hidden in CLI usage because the process exits immediately.

It would be safer to explicitly shut down the Manager or manage it via a context manager to match Python’s multiprocessing best practices.

Activity

  1. cobaltt7 commented on Jan 10, 2026

    @cobaltt7
    Collaborator

    PRs welcome!

  2. nileshsingal commented on Jan 10, 2026

    @nileshsingal
    Contributor

    @cobaltt7 Thanks! I’ve opened a PR that addresses this by explicitly shutting down
    the multiprocessing.Manager() in schedule_formatting() for DIFF or
    COLOR_DIFF modes.

    The PR also adds a deterministic unit test to ensure the Manager is always
    shut down, including on exceptions or cancellation.

    Looking forward to your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions