Repository navigation
Mutable default argument in _SharedMemoryTracker.__init__ causes shared state between instances #153141
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 5, 2026 But if you create more than one tracker in the same interpreter
When is it the case? the class is private so you're not meant to use it directly.
- addedtype-featureA feature request or enhancementA feature request or enhancementstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directoryand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 5, 2026 it's a code smell regardless of if it happens in practice via public API surfaces as noted in the PR description's "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."
I'm happy to merge the fix.
Reacted by Vineet KumarWhen is it the case? the class is private so you're not meant to use it directly.
AFAIK, there is no current execution path in CPython where more than one
_SharedMemoryTrackerinstance is created in the same interpreter. I saw this as a latent bug because if multiple instances were created, they would end up sharingsegment_namesby accident due to the mutable default. This does not seem to cause problems right now, but I noticed the mutable default while reading the code and suggested changing it toNoneto make things more correct.I'm ok with it being fixed, but I would prefer that we don't call it a "bug". It's something that can be improved, but it's not a bug per-se (at least not to me). If Greg wants to backport the change though, feel free to do it.
Reacted by Vineet Kumar- addedtype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)and removedtype-featureA feature request or enhancementA feature request or enhancement
on Jul 5, 2026 the way we structure our "New Issue" button forces people to pick a category up front with Bug being the first choice. i wouldn't worry about what we call this. :) its an internal cleanup in my book.
- added 4 commits that reference this issue
on Jul 5, 2026
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