Skip to content

Allow Manual Control Over resource_tracker in shared_memory #129999

@koshak01

Description

@koshak01

Feature or enhancement

Proposal:

Proposal: Allow Manual Control Over resource_tracker in shared_memory

Context and Problem Statement

I am working on a system where a main module is responsible for creating and deleting shared memory segments, while sub-modules only connect to these segments and write data. However, when a sub-module stops, resource_tracker incorrectly deletes the shared memory segment, even though it should persist.

This is a critical issue because resource_tracker assumes it has full control over shared memory cleanup, while in complex architectures like mine, only the main module should be responsible for managing these resources.

Proposed Solution

I propose adding a mechanism to control whether resource_tracker tracks a shared memory segment. There are several possible approaches:

  1. Add a flag (e.g., track=False) when creating a SharedMemory object:

    shm = SharedMemory(name="my_shared_segment", create=True, track=False)

    This would allow developers to specify that they do not want resource_tracker to track this object.

  2. Allow replacing resource_tracker with a custom implementation, so developers can define their own resource management logic.

  3. Provide an API to disable resource_tracker entirely for advanced users who manage shared memory explicitly:

    from multiprocessing import resource_tracker
    resource_tracker.disable()

Why This is Needed

  • In systems with complex shared memory architectures, resource_tracker does not have enough context to decide which segments should be deleted.
  • Allowing manual control would prevent unintended resource deletion, reducing potential data loss or race conditions in multi-process applications.
  • Manually patching resource_tracker or modifying the Python standard library is not an ideal solution.

Current Workaround

For now, I have resorted to commenting out resource_tracker code in the standard library, which I acknowledge is not a proper solution.

Would the Python maintainers consider adding such an option in multiprocessing.shared_memory?

Thank you!

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions