Finalizers of multiprocessing's Pool are abruptly stopped (causing possible deadlock)

Hi.

I was surprised to discover that multiprocessing.util.Finalize did not seem to work for processes started by multiprocessing.Pool. The finalizer is called when the pool’s context manager exits, but it isn’t given enough time to complete.

Here is a minimal reproducible example (tested with Python 3.13.5):

import multiprocessing
from multiprocessing.util import Finalize
import time


class Worker:

    def setup(self, event):
        self._event = event
        self._finalizer = Finalize(self, self._finalize, exitpriority=0)

    def work(self, _):
        time.sleep(0.01)

    def _finalize(self):
        print("Finalizing...")
        time.sleep(0.1)
        print("Finalized.")
        self._event.set()


if __name__ == "__main__":
    context = multiprocessing.get_context("spawn")

    worker = Worker()
    event = context.Event()

    # It works fine with a explicit Process instance.
    process = context.Process(target=worker.setup, args=(event,))
    process.start()
    time.sleep(0.5)
    assert event.is_set()  # OK

    event.clear()

    # It does not work when using a Pool.
    with context.Pool(1, initializer=worker.setup, initargs=(event,)) as pool:
        pool.map(worker.work, [None])
    time.sleep(0.5)
    assert event.is_set()  # NOK

The “Finalizing…” message is printed but “Finalized.” isn’t, and the Event is never set.

Worse: if the objects inherited by the pool’s process use a multiprocessing.Lock, it may never be released, potentially causing deadlocks. This is an issue I am encountering in one of my libraries that implements an object similar to multiprocessing.Queue.

I would therefore please like to know if this was a known issue, and if there was a solution to allow Finalize to run entirely.