Skip to content

gh-82300: Add track parameter to shared memory #110778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3be08b4
add track parameter to shared memory
pan324 Oct 12, 2023
f50812a
phrasing
pan324 Oct 12, 2023
cbc8431
📜🤖 Added by blurb_it.
blurb-it[bot] Oct 12, 2023
7138447
phrasing
pan324 Oct 12, 2023
6bbfca6
Merge branch 'main' of https://github.com/python/cpython into shmemun…
pan324 Oct 12, 2023
db79426
Merge branch 'python:main' into shmemuntrack
pan324 Oct 12, 2023
db11894
Merge branch 'shmemuntrack' of github.com:pan324/cpython into shmemun…
pan324 Oct 12, 2023
d44cb82
Delete Doc/library/result.html
pan324 Oct 12, 2023
c62cff0
Merge branch 'main' into shmemuntrack
ambv Oct 13, 2023
dcda10f
Update Misc/NEWS.d/next/Library/2023-10-12-18-19-47.gh-issue-82300.P8…
pan324 Oct 15, 2023
63d21d7
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 15, 2023
e990e41
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 15, 2023
9c593ba
Removed TypeError. Clarified documentation.
pan324 Oct 17, 2023
9ef1ff3
untracking shmem can unlink now
pan324 Oct 17, 2023
e5fe674
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 19, 2023
66acf90
removed unneeded try-except
pan324 Oct 19, 2023
3fdf625
phrasing of track parameter
pan324 Oct 19, 2023
d65e3f8
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 24, 2023
a97c6d3
untracking test
pan324 Oct 24, 2023
17c07f5
untrack tests both track=True and track=False
pan324 Oct 25, 2023
13f3fb6
untrack tests both track=True and track=False
pan324 Oct 25, 2023
7c7f0e7
untrack tests both track=True and track=False
pan324 Oct 25, 2023
765afb7
reliable test cleanup
pan324 Nov 30, 2023
a5848c1
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Dec 1, 2023
8255e01
split tests
pan324 Dec 1, 2023
5d89117
split tests
pan324 Dec 1, 2023
66db5b8
style: add a missing blank line
gpshead Dec 2, 2023
52053f8
Merge branch 'main' into shmemuntrack
gpshead Dec 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions 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)
.. 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 Expand Up @@ -64,26 +64,45 @@ copying of data.
memory block may be larger or equal to the size requested. When attaching
to an existing shared memory block, the ``size`` parameter is ignored.

*track*, when enabled, registers the shared memory block with a resource
tracker process on platforms where the OS does not do this automatically.
The resource tracker ensures proper cleanup of the shared memory even
if all other processes with access to the memory exit without doing so.
Python processes created from a common ancestor using :mod:`multiprocessing`
facilities share a single resource tracker process, and the lifetime of
shared memory segments is handled automatically among these processes.
Python processes created in any other way will receive their own
resource tracker when accessing shared memory with *track* enabled.
This will cause the shared memory to be deleted by the resource tracker
of the first process that terminates.
To avoid this issue, users of :mod:`subprocess` or standalone Python
processes should set *track* to ``False`` when there is already another
process in place that does the bookkeeping.
*track* is ignored on Windows, which has its own tracking and
automatically deletes shared memory when all handles to it have been closed.

.. versionchanged:: 3.13 Added *track* parameter.

.. method:: close()

Closes access to the shared memory from this instance. In order to
ensure proper cleanup of resources, all instances should call
``close()`` once the instance is no longer needed. Note that calling
``close()`` does not cause the shared memory block itself to be
destroyed.
Closes the file descriptor/handle to the shared memory from this
instance. :meth:`close()` should be called once access to the shared
memory block from this instance is no longer needed. Depending
on operating system, the underlying memory may or may not be freed
even if all handles to it have been closed. To ensure proper cleanup,
use the :meth:`unlink()` method.

.. method:: unlink()

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 need
for the shared memory block. After requesting its destruction, a
shared memory block may or may not be immediately destroyed and
this behavior may differ across platforms. Attempts to access data
inside the shared memory block after ``unlink()`` has been called may
result in memory access errors. Note: the last process relinquishing
its hold on a shared memory block may call ``unlink()`` and
:meth:`close()` in either order.
Deletes the underlying shared memory block. This should be called only
once per shared memory block regardless of the number of handles to it,
even in other processes.
:meth:`unlink()` and :meth:`close()` can be called in any order, but
trying to access data inside a shared memory block after :meth:`unlink()`
may result in memory access 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.

.. attribute:: buf

Expand Down
24 changes: 17 additions & 7 deletions Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class SharedMemory:
_flags = os.O_RDWR
_mode = 0o600
_prepend_leading_slash = True if _USE_POSIX else False
_track = True

def __init__(self, name=None, create=False, size=0):
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 All @@ -82,6 +83,7 @@ def __init__(self, name=None, create=False, size=0):
if name is None and not self._flags & os.O_EXCL:
raise ValueError("'name' can only be None if create=True")

self._track = track
if _USE_POSIX:

# POSIX Shared Memory
Expand Down Expand Up @@ -116,8 +118,8 @@ def __init__(self, name=None, create=False, size=0):
except OSError:
self.unlink()
raise

resource_tracker.register(self._name, "shared_memory")
if self._track:
resource_tracker.register(self._name, "shared_memory")

else:

Expand Down Expand Up @@ -236,12 +238,20 @@ 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)
resource_tracker.unregister(self._name, "shared_memory")
if self._track:
resource_tracker.unregister(self._name, "shared_memory")


_encoding = "utf8"
Expand Down
53 changes: 53 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4455,6 +4455,59 @@ def test_shared_memory_cleaned_after_process_termination(self):
"resource_tracker: There appear to be 1 leaked "
"shared_memory objects to clean up at shutdown", err)

@unittest.skipIf(os.name != "posix", "resource_tracker is posix only")
def test_shared_memory_untracking(self):
# gh-82300: When a separate Python process accesses shared memory
# with track=False, it must not cause the memory to be deleted
# when terminating.
cmd = '''if 1:
import sys
from multiprocessing.shared_memory import SharedMemory
mem = SharedMemory(create=False, name=sys.argv[1], track=False)
mem.close()
'''
mem = shared_memory.SharedMemory(create=True, size=10)
# 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.assertNotIn(b"resource_tracker", err)
self.assertEqual(rc, 0)
mem2 = shared_memory.SharedMemory(create=False, name=mem.name)
mem2.close()
finally:
try:
mem.unlink()
except OSError:
pass
mem.close()

@unittest.skipIf(os.name != "posix", "resource_tracker is posix only")
def test_shared_memory_tracking(self):
# gh-82300: When a separate Python process accesses shared memory
# with track=True, it must cause the memory to be deleted when
# terminating.
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)
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:
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
@@ -0,0 +1 @@
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.