python / python/cpython

multiprocessing.shared_memory: failed attach unlinks another process's block

Open
#153,279 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-multiprocessing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description

SharedMemory.__init__ calls self.unlink() from its OSError cleanup handler:

try:
    if create and size:
        os.ftruncate(self._fd, size)
    stats = os.fstat(self._fd)
    size = stats.st_size
    self._mmap = mmap.mmap(self._fd, size)
except OSError:
    self.unlink()
    raise

This runs unconditionally, including on the attach path (create=False).
unlink() calls shm_unlink(), which permanently destroys the named block.
So if mmap() fails while attaching to a block created by another process
(e.g. ENOMEM under memory pressure), the failing attach destroys another
owner's live shared memory block
.

Reproduction (simulating an mmap failure during attach):

from unittest import mock
from multiprocessing import shared_memory

owner = shared_memory.SharedMemory(create=True, size=1024)
try:
    with mock.patch("multiprocessing.shared_memory.mmap.mmap",
                    side_effect=OSError("ENOMEM")):
        shared_memory.SharedMemory(owner.name)   # attach
except OSError:
    pass

shared_memory.SharedMemory(owner.name)   # FileNotFoundError: block destroyed

Secondary issue: resource_tracker.register() runs after this try/except, so
on the create=True path the block is not yet registered. unlink() still
calls resource_tracker.unregister(), so the resource_tracker does
cache[rtype].remove(name) on an unknown name → KeyError printed as a
traceback, with the tracker's exit code set to 3.

Expected behavior

A failed attach must not unlink a block it does not own. On error the fd should
just be closed; the block should be unlinked only when it was created in this
call, and without a spurious unregister for a never-registered block.

Your environment

  • CPython main (3.16.0a0); also affects earlier versions
  • Linux / POSIX shared memory (_USE_POSIX)
Linked PRs
  • gh-153280

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at multiprocessing.shared_memory.SharedMemory.init and reproduce the attach failure with the mock shown in the issue. Check the cleanup and resource-tracker behavior for both attach and create paths. Done means a failed attach leaves the existing block usable and does not produce a spurious tracker error; the issue includes linked PR gh-153280.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.