Skip to content

Mutable default argument in _SharedMemoryTracker.__init__ causes shared state between instances #153141

Description

@whyvineet

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:

['seg1']
True

Expected behavior:

[]
False

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

Activity

  1. picnixz commented on Jul 5, 2026

    @picnixz
    Member

    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.

  2. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jul 5, 2026
  3. gpshead commented on Jul 5, 2026

    @gpshead
    Member

    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.

  4. self-assigned this
    on Jul 5, 2026
  5. whyvineet commented on Jul 5, 2026

    @whyvineet
    ContributorAuthor

    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.

  6. picnixz commented on Jul 5, 2026

    @picnixz
    Member

    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.

  7. added
    type-refactorCode refactoring (with no changes in behavior)
    and removed
    type-featureA feature request or enhancement
    on Jul 5, 2026
  8. gpshead commented on Jul 5, 2026

    @gpshead
    Member

    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.

  9. added 4 commits that reference this issue on Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-multiprocessingtype-refactorCode refactoring (with no changes in behavior)

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions