Skip to content

multiprocessing manager not threadsafe #137233

@stmario-ax

Description

@stmario-ax

Bug report

Bug description:

Expected: 0, 1, 2 or 0, 10, 11 or 0, 10, 100, 2, 1000, 101 or 0, 1, 2, 20 etc...
Actual: 0, 10, 200

Python version 3.12

Code for minimal reproduction:

import multiprocessing
from threading import Thread
import time

class MyThread(Thread):
    def __init__(self, test_dict, **kwargs):
        super().__init__(**kwargs)
        self.test_dict = test_dict

    def run(self):
        time.sleep(1)
        while True:
            self.test_dict["test"] += 1
            time.sleep(1)

class AProcess(multiprocessing.Process):
    def __init__(self, test_dict, **kwargs):
        super().__init__(**kwargs)
        self.test_dict = test_dict

    def run(self):
        time.sleep(1)
        while True:
            self.test_dict["test"] *= 10
            time.sleep(1)

class PrintThread(Thread):
    def __init__(self, test_dict, **kwargs):
        super().__init__(**kwargs)
        self.test_dict = test_dict

    def run(self):
        while True:
            time.sleep(0.1)
            print(self.test_dict["test"])

if __name__ == "__main__":
    manager = multiprocessing.Manager()
    test_dict = manager.dict({"test": 0})

    ts = [
        MyThread(test_dict=test_dict, daemon=True),
        PrintThread(test_dict=test_dict, daemon=True),
    ]

    for t in ts:
        t.start()
    p = AProcess(test_dict)
    p.start()
    time.sleep(2)
    test_dict["test"] = 20
    for t in ts:
        t.join()
    p.join()

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions