Skip to content

gh-153279: Don't unlink another process's shared memory block on failed attach#153280

Open
uguraka wants to merge 1 commit into
python:mainfrom
uguraka:gh-153279-shm-attach-unlink
Open

gh-153279: Don't unlink another process's shared memory block on failed attach#153280
uguraka wants to merge 1 commit into
python:mainfrom
uguraka:gh-153279-shm-attach-unlink

Conversation

@uguraka

@uguraka uguraka commented Jul 7, 2026

Copy link
Copy Markdown

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

except OSError:
    self.unlink()
    raise

On the attach path (create=False) this permanently destroyed a block owned
by another process when mmap() failed (e.g. ENOMEM under memory pressure),
since unlink() calls shm_unlink().

The cleanup now only closes the file descriptor, and unlinks the block only
when it was created in the failing call. Because the block has not been
registered with the resource tracker at that point (register() runs after the
try/except), it is unlinked directly rather than through unlink(), which
avoids a spurious UNREGISTER that raised KeyError in the resource tracker
on the create=True path.

Added regression tests for both the attach and create failure paths; the attach
test fails without the fix.

…n failed attach

SharedMemory.__init__ called self.unlink() from its OSError cleanup
handler unconditionally.  On the attach path (create=False) this destroyed
a block owned by another process when mmap() failed (e.g. ENOMEM).  The
cleanup now only closes the file descriptor and unlinks the block only when
it was created in the failing call.  Because the block has not been
registered with the resource tracker yet at that point, it is unlinked
directly to avoid a spurious UNREGISTER (which raised KeyError in the
resource tracker on the create path).
@uguraka uguraka requested a review from gpshead as a code owner July 7, 2026 16:31
@python-cla-bot

python-cla-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant