Skip to content

Commit

Permalink
reliable test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pan324 committed Nov 30, 2023
1 parent 7c7f0e7 commit 765afb7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Doc/library/multiprocessing.shared_memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ or other communications requiring the serialization/deserialization and
copying of data.


.. class:: SharedMemory(name=None, create=False, size=0, track=True)
.. class:: SharedMemory(name=None, create=False, size=0, *, track=True)

Creates a new shared memory block or attaches to an existing shared
memory block. Each shared memory block is assigned a unique name.
Expand Down
14 changes: 10 additions & 4 deletions Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SharedMemory:
_prepend_leading_slash = True if _USE_POSIX else False
_track = True

def __init__(self, name=None, create=False, size=0, track=True):
def __init__(self, name=None, create=False, size=0, *, track=True):
if not size >= 0:
raise ValueError("'size' must be a positive integer")
if create:
Expand Down Expand Up @@ -238,9 +238,15 @@ def close(self):
def unlink(self):
"""Requests that the underlying shared memory block be destroyed.
In order to ensure proper cleanup of resources, unlink should be
called once (and only once) across all processes which have access
to the shared memory block."""
Unlink should be called once (and only once) across all handles
which have access to the shared memory block, even if these
handles belong to different processes. Closing and unlinking may
happen in any order, but trying to access data inside a shared
memory block after unlinking may result in memory errors,
depending on platform.
This method has no effect on Windows, where the only way to
delete a shared memory block is to close all handles."""

if _USE_POSIX and self._name:
_posixshmem.shm_unlink(self._name)
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4453,39 +4453,39 @@ def test_shared_memory_untracking(self):
mem.close()
'''
mem = shared_memory.SharedMemory(create=True, size=10)
rc, out, err = script_helper.assert_python_ok("-c", cmd, mem.name)
# The resource tracker shares pipes with the subprocess, and so
# err existing means that the tracker process has terminated now.
try:
rc, out, err = script_helper.assert_python_ok("-c", cmd, mem.name)
self.assertEqual(rc, 0)
mem2 = shared_memory.SharedMemory(create=False, name=mem.name)
mem2.close()
finally:
mem.close()
try:
mem.unlink()
except OSError:
pass
mem.close()
cmd = '''if 1:
import sys
from multiprocessing.shared_memory import SharedMemory
mem = SharedMemory(create=False, name=sys.argv[1], track=True)
mem.close()
'''
mem = shared_memory.SharedMemory(create=True, size=10)
rc, out, err = script_helper.assert_python_ok("-c", cmd, mem.name)
try:
rc, out, err = script_helper.assert_python_ok("-c", cmd, mem.name)
self.assertEqual(rc, 0)
self.assertIn(
b"resource_tracker: There appear to be 1 leaked "
b"shared_memory objects to clean up at shutdown", err)
finally:
resource_tracker.unregister(mem._name, "shared_memory")
mem.close()
try:
mem.unlink()
except OSError:
pass
resource_tracker.unregister(mem._name, "shared_memory")
mem.close()

#
# Test to verify that `Finalize` works.
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add ``track`` parameter to :class:`multiprocessing.shared_memory.SharedMemory` that allows using shared memory blocks without having to register with the resource tracker.
Add ``track`` parameter to :class:`multiprocessing.shared_memory.SharedMemory` that allows using shared memory blocks without having to register with the POSIX resource tracker that automatically releases them upon process exit.

0 comments on commit 765afb7

Please sign in to comment.