Sitelet https://github.com/python/cpython/issues/91577
Skip to content

SharedMemory.unlink() can fail if called during interpreter shutdown #91577

Description

@samtygier

SharedMemory.unlink() contains a import which can fail if called during interpreter shutdown.

For example when working with SharedMemory as the buffer for a numpy array it is useful to create a wrapper object to ensure that clean up is done with the object is deleted.

#import numpy as np
from multiprocessing import shared_memory

class ShArray:
    def __init__(self):
        self.shared_memory = shared_memory.SharedMemory(create=True, size=400)
        #self.array = np.ndarray((10,10), dtype="i4", buffer=self.shared_memory.buf)

    def __del__(self):
        self.shared_memory.close()
        self.shared_memory.unlink()

a1 = ShArray()

(Commented lines show my typical usage, but are not need to demonstrate the issue)

Exception ignored in: <function ShArray.__del__ at 0x7f36eee510d0>
Traceback (most recent call last):
  File "/home/sam/git/cpython/test_shared_mem2.py", line 13, in __del__
  File "/home/sam/git/cpython/Lib/multiprocessing/shared_memory.py", line 240, in unlink
ImportError: sys.meta_path is None, Python is likely shutting down
/home/sam/git/cpython/Lib/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked shared_memory objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

This happens on 3.8 and current main, both on Linux x86-64.

Patch to follow

Activity

  1. added a commit that references this issue on Jun 16, 2022
  2. added a commit that references this issue on Jun 16, 2022
  3. added 2 commits that reference this issue on Jun 16, 2022
  4. vstinner commented on Jun 16, 2022

    @vstinner
    Member

    I'm not a supporter of multiprocessing programs relying on the GC to call __del__() finalizers. You should close resources explicitly, using with: statement if possible. But well, the change seems acceptable to me, so I merged it.

    cc @pitrou

  5. added 2 commits that reference this issue on Jun 16, 2022
  6. pitrou commented on Jun 16, 2022

    @pitrou
    Member

    Seems fine to me. with statements are not always usable.

  7. vstinner commented on Jun 16, 2022

    @vstinner
    Member

    with statements are not always usable.

    So we should make them usable in more cases :-)

    Even without with:, the idea is to explicitly release resources, not rely on the GC for that. The objects are not finalized in a specific order by the GC. The exact order is really complicated and depends on too many things. So it's likely that your code will fall into many corner cases. ImportError: sys.meta_path is None, Python is likely shutting down is just one example of such issue that you can get if you rely on the GC.

    In PyPy, it expect that it will be even worse.

  8. pitrou commented on Jun 16, 2022

    @pitrou
    Member

    So we should make them usable in more cases :-)

    I mean that with statements only work if your lifetime is lexically-defined. It's not the case for more sophisticated use cases unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions