Skip to content

Roll back named mutex shared memory header when pthread initialization fails - #137218

Merged
jkoritzinsky merged 1 commit into
mainfrom
dev/jkoritzinsky/named-mutex-init-failure-rollback
Nov 23, 2026
Merged

jkoritzinsky merged 1 commit into
mainfrom
dev/jkoritzinsky/named-mutex-init-failure-rollback

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

NamedMutexProcessDataBase.CreateOrOpen publishes the new SharedMemoryProcessDataHeader into the process-local cache (with its initial reference) before InitializeSharedData runs. If LowLevelCrossProcessMutex_Init fails, the exception propagates but the header stays cached and mapped.

Later same-process new Mutex(name) / Mutex.TryOpenExisting calls then hit that cache entry, report createdNew == false, skip initialization, and return handles over a zero-filled pthread_mutex_t. glibc treats that as a valid, process-private, non-robust mutex, so there is no crash. Instead:

  • cross-process wake-ups are lost (a waiting process in another process is never woken),
  • robustness (EOWNERDEAD recovery) is lost,
  • the mapping and shared file are leaked until process exit,
  • other processes opening the same name receive the uninitialized object too.

This change drops the header's initial reference when initialization throws, so the header is removed from the cache, unmapped, and the shared file unlinked (if no other process holds it). A retry then re-initializes from scratch.

Validation

pthread_mutex_init cannot fail for these attributes on glibc/musl Linux, so I validated with a labeled fault injection (an LD_PRELOAD shim that fails pthread_mutex_init for process-shared robust attributes; no test is added since the trigger is synthetic). Against a local build:

Scenario Before After
First init fails (ENOMEM) retries return createdNew=False over a zero-filled mutex; a child process WaitOne(4000) times out after 4004 ms; file and mapping leaked after dispose first retry re-initializes and returns createdNew=True; child acquires in ~1012 ms; file deleted and unmapped after dispose
Every init fails all retries "succeed" with a bad object every retry throws IOException; TryOpenExisting returns false; nothing cached
No injection child acquires ~1022 ms child acquires ~1014 ms (unchanged)

Existing tests: System.Threading.Tests filtered to MutexTests — 119 total, 0 failed, 1 skipped (Linux x64, glibc 2.43, pthread backend). The non-pthread backend never throws from InitializeSharedData, so it is unaffected.

Note (not addressed here): on init failure the exception message currently reads Success : 'Failed to initialize pthread mutex' because SystemNative_LowLevelCrossProcessMutex_Init returns a converted error code without setting errno.

Note

This PR description and the change were AI/Copilot-generated.

…n fails

NamedMutexProcessDataBase.CreateOrOpen publishes the SharedMemoryProcessDataHeader
into the process-local cache before InitializeSharedData runs. If
LowLevelCrossProcessMutex_Init failed, the exception propagated but the cached
header kept its initial reference. Later same-process opens hit the cache,
reported createdNew == false, skipped initialization and handed out handles over
a zero-filled pthread mutex (process-private and non-robust), losing
cross-process wake-ups and leaking the mapping and file until process exit.

Drop the header's initial reference when initialization throws so the header is
unpublished, unmapped and the shared file deleted, and a retry re-initializes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@jkoritzinsky

Copy link
Copy Markdown
Member Author

/ba-g eventpipe failure

@jkoritzinsky
jkoritzinsky merged commit d97728e into main Nov 23, 2026
134 of 136 checks passed
@jkoritzinsky
jkoritzinsky deleted the dev/jkoritzinsky/named-mutex-init-failure-rollback branch November 23, 2026 21:43
@github-project-automation github-project-automation Bot moved this to Done in AppModel Nov 23, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants