You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mutable default argument in _SharedMemoryTracker.__init__ causes shared state between instances #153141
Because the default list is created only once, multiple _SharedMemoryTracker instances created without explicitly passing segment_names unexpectedly share the same list.
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.
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."
When 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 _SharedMemoryTracker instance is created in the same interpreter. I saw this as a latent bug because if multiple instances were created, they would end up sharing segment_names by 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 to None to 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.
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.
Activity
picnixz commentedon Jul 5, 2026
When is it the case? the class is private so you're not meant to use it directly.
gpshead commentedon 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.
whyvineet commentedon Jul 5, 2026
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.picnixz commentedon Jul 5, 2026
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.
gpshead commentedon 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.
gh-153141: Fix mutable default argument in _SharedMemoryTracker.__ini…
[3.14] gh-153141: Fix mutable default argument in _SharedMemoryTracke…
[3.13] gh-153141: Fix mutable default argument in _SharedMemoryTracke…
[3.15] gh-153141: Fix mutable default argument in _SharedMemoryTracke…