Bug report
Bug description:
_SharedMemoryTracker.__init__ uses a mutable default argument for segment_names:
|
def __init__(self, name, segment_names=[]): |
Because the default list is created only once, multiple _SharedMemoryTracker instances created without explicitly passing segment_names unexpectedly share the same list.
Minimal reproducer:
from multiprocessing.managers import _SharedMemoryTracker
t1 = _SharedMemoryTracker("ctx1")
t1.register_segment("seg1")
t2 = _SharedMemoryTracker("ctx2")
print(t2.segment_names)
print(t1.segment_names is t2.segment_names)
Output:
Expected behavior:
Right now, this bug is hidden because _SharedMemoryTracker is usually created just once for each SharedMemoryServer process, so the shared state does not usually show up. But if you create more than one tracker in the same interpreter, they end up sharing bookkeeping state by accident because of the mutable default list.
To fix this, we can make the constructor safer by using None as the default value and creating a new list for each instance if one is not provided.
CPython versions tested on:
3.15
Operating systems tested on:
Windows
Linked PRs
Bug report
Bug description:
_SharedMemoryTracker.__init__uses a mutable default argument forsegment_names:cpython/Lib/multiprocessing/managers.py
Line 1297 in 28a8c18
Because the default list is created only once, multiple
_SharedMemoryTrackerinstances created without explicitly passingsegment_namesunexpectedly share the same list.Minimal reproducer:
Output:
Expected behavior:
Right now, this bug is hidden because
_SharedMemoryTrackeris usually created just once for eachSharedMemoryServerprocess, so the shared state does not usually show up. But if you create more than one tracker in the same interpreter, they end up sharing bookkeeping state by accident because of the mutable default list.To fix this, we can make the constructor safer by using
Noneas the default value and creating a new list for each instance if one is not provided.CPython versions tested on:
3.15
Operating systems tested on:
Windows
Linked PRs