Skip to content

Multiprocessing code succeeds in 3.13 but fails in 3.14 #128145

Closed as not planned
Closed as not planned
@nickdrozd

Description

@nickdrozd

Bug report

Bug description:

The following code runs successfully in 3.13.1 (and earlier versions) but fails in 3.14.0a3+ (5a584c8):

from __future__ import annotations

from unittest import TestCase
from multiprocessing import Manager, Pool

LETTERS: dict[str, int]

def add_letter(letter: str) -> None:
    LETTERS[letter] += 1

class TestAsdf(TestCase):
    def setUp(self):
        global LETTERS

        LETTERS = Manager().dict(
            a = 0,
            s = 0,
            d = 0,
            f = 0,
        )

    def test_asdf(self):
        with Pool() as pool:
            pool.map(
                add_letter,
                list('asdf'))

        print(LETTERS)

Run successfully, it produces this output:

$ python3.11 -m unittest asdf

{'a': 1, 's': 1, 'd': 1, 'f': 1}

In 3.14, it fails with NameError:

$ python3.14 -m unittest asdf

======================================================================
ERROR: test_asdf (asdf.TestAsdf.test_asdf)
----------------------------------------------------------------------
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.14/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
                    ~~~~^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "/home/nick/asdf.py", line 9, in add_letter
    LETTERS[letter] += 1
    ^^^^^^^
NameError: name 'LETTERS' is not defined
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nick/asdf.py", line 24, in test_asdf
    pool.map(
    ~~~~~~~~^
        add_letter,
        ^^^^^^^^^^^
        list('asdf'))
        ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/multiprocessing/pool.py", line 367, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.14/multiprocessing/pool.py", line 774, in get
    raise self._value
NameError: name 'LETTERS' is not defined

This example looks stupid, but it is based on real-world test code.

CPython versions tested on:

3.11, 3.13, 3.14, CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions