Skip to content

Commit 9a458be

Browse files
authored
gh-91577: SharedMemory move imports out of methods (#91579)
SharedMemory.unlink() uses the unregister() function from resource_tracker. Previously it was imported in the method, but this can fail if the method is called during interpreter shutdown, for example when unlink is part of a __del__() method. Moving the import to the top of the file, means that the unregister() method is available during interpreter shutdown. The register call in SharedMemory.__init__() can also use this imported resource_tracker.
1 parent a38c2a6 commit 9a458be

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Lib/multiprocessing/shared_memory.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import _posixshmem
2424
_USE_POSIX = True
2525

26+
from . import resource_tracker
2627

2728
_O_CREX = os.O_CREAT | os.O_EXCL
2829

@@ -116,8 +117,7 @@ def __init__(self, name=None, create=False, size=0):
116117
self.unlink()
117118
raise
118119

119-
from .resource_tracker import register
120-
register(self._name, "shared_memory")
120+
resource_tracker.register(self._name, "shared_memory")
121121

122122
else:
123123

@@ -237,9 +237,8 @@ def unlink(self):
237237
called once (and only once) across all processes which have access
238238
to the shared memory block."""
239239
if _USE_POSIX and self._name:
240-
from .resource_tracker import unregister
241240
_posixshmem.shm_unlink(self._name)
242-
unregister(self._name, "shared_memory")
241+
resource_tracker.unregister(self._name, "shared_memory")
243242

244243

245244
_encoding = "utf8"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move imports in :class:`~multiprocessing.SharedMemory` methods to module level so that they can be executed late in python finalization.

0 commit comments

Comments
 (0)