[Python-checkins] gh-91577: SharedMemory move imports out of methods (GH-91579)

miss-islington webhook-mailer at python.org
Thu Jun 16 10:07:35 EDT 2022


https://github.com/python/cpython/commit/5c10c365fef3ca480c930cb379bf1d0ebadcd911
commit: 5c10c365fef3ca480c930cb379bf1d0ebadcd911
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-06-16T07:07:30-07:00
summary:

gh-91577: SharedMemory move imports out of methods (GH-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.
(cherry picked from commit 9a458befdd68625d088f4fea7df135a57d147deb)

Co-authored-by: samtygier <samtygier at yahoo.co.uk>

files:
A Misc/NEWS.d/next/Library/2022-04-15-17-38-55.gh-issue-91577.Ah7cLL.rst
M Lib/multiprocessing/shared_memory.py

diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py
index 122b3fcebf3fe..881f2001dd598 100644
--- a/Lib/multiprocessing/shared_memory.py
+++ b/Lib/multiprocessing/shared_memory.py
@@ -23,6 +23,7 @@
     import _posixshmem
     _USE_POSIX = True
 
+from . import resource_tracker
 
 _O_CREX = os.O_CREAT | os.O_EXCL
 
@@ -116,8 +117,7 @@ def __init__(self, name=None, create=False, size=0):
                 self.unlink()
                 raise
 
-            from .resource_tracker import register
-            register(self._name, "shared_memory")
+            resource_tracker.register(self._name, "shared_memory")
 
         else:
 
@@ -237,9 +237,8 @@ def unlink(self):
         called once (and only once) across all processes which have access
         to the shared memory block."""
         if _USE_POSIX and self._name:
-            from .resource_tracker import unregister
             _posixshmem.shm_unlink(self._name)
-            unregister(self._name, "shared_memory")
+            resource_tracker.unregister(self._name, "shared_memory")
 
 
 _encoding = "utf8"
diff --git a/Misc/NEWS.d/next/Library/2022-04-15-17-38-55.gh-issue-91577.Ah7cLL.rst b/Misc/NEWS.d/next/Library/2022-04-15-17-38-55.gh-issue-91577.Ah7cLL.rst
new file mode 100644
index 0000000000000..0f44f34011f9c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-15-17-38-55.gh-issue-91577.Ah7cLL.rst
@@ -0,0 +1 @@
+Move imports in :class:`~multiprocessing.SharedMemory` methods to module level so that they can be executed late in python finalization.



More information about the Python-checkins mailing list