Skip to content

Serialize process-wide memory barriers under Rosetta - #134384

Open
agocke wants to merge 2 commits into
dotnet:mainfrom
agocke:fix-rosetta-process-wide-barrier
Open

agocke wants to merge 2 commits into
dotnet:mainfrom
agocke:fix-rosetta-process-wide-barrier

Conversation

@agocke

@agocke agocke commented Sep 21, 2026

Copy link
Copy Markdown
Member

Summary

  • Serialize the Apple process-wide memory barrier when an x64 process is running under Rosetta.
  • Leave native arm64 and Intel macOS behavior unchanged.
  • Add an isolated regression test that invokes Interlocked.MemoryBarrierProcessWide concurrently from four threads.

Motivation

Under Rosetta, concurrent calls to thread_get_register_pointer_values can deadlock while reconstructing translated x64 thread state. A standalone stress repro hung in 5 out of 5 runs, with every worker blocked in:

thread_get_register_pointer_values
thread_get_state
Rosetta Runtime Routines

Serializing the existing Mach-based barrier avoids this deadlock while retaining the mechanism recommended for Apple platforms.

This addresses the concurrent-barrier hang reported in #133797. It intentionally does not add a pthread_atfork handler for the separate raw-fork interaction. Normal .NET process creation on macOS uses posix_spawn after #126063.

Validation

  • Clean baseline: ./build.sh clr+libs+host
  • Changed build: ./build.sh clr+libs+host
  • Rosetta runtime: ./build.sh clr+libs+host -arch x64 -cross
  • Release CoreCLR builds:
    • ./build.sh clr.native -c Release
    • ./build.sh clr.native -c Release -arch x64 -cross
  • System.Threading.Tests: 714 passed on native arm64
  • System.Threading.Tests: 714 passed on x64 under Rosetta
  • New targeted test passed on native arm64 and x64 under Rosetta
  • Negative validation: the new test consistently timed out after 20 seconds when run with an unfixed x64 libcoreclr; it completes in about 5.5 seconds with the fix

Performance

Single-threaded Interlocked.MemoryBarrierProcessWide latency was measured using a Debug x64 testhost under Rosetta, taking the median of 10 process runs with 10,000 calls per run:

Median latency
Before 35.7 us/call
After 36.4 us/call

The uncontended mutex adds approximately 2% in this configuration. Concurrent calls are intentionally serialized because issuing the Mach register-state queries concurrently is the operation that deadlocks Rosetta.

Note

This pull request description was generated with GitHub Copilot.

Concurrent Mach thread state queries can deadlock when an x64 process runs under Rosetta. Serialize the Apple process-wide barrier only in translated processes and add a concurrent-call regression test.

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

Copilot-Session: 6de45ab0-f614-494b-9fcf-b38d01823145
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The regression test must skip when multithreading is unsupported.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

This PR serializes process-wide memory barriers under Rosetta to prevent concurrent Mach-state query deadlocks and adds regression coverage.

Changes:

  • Adds Rosetta-specific mutex serialization.
  • Adds a concurrent four-thread barrier test.
File Description
src/​native/​minipal/​memorybarrierprocesswide.c Serializes barrier calls under Rosetta.
src/​libraries/​System.Threading/​tests/​InterlockedTests.cs Adds concurrent barrier regression coverage.

Comment thread src/libraries/System.Threading/tests/InterlockedTests.cs Outdated
@jkotas jkotas added the os-macos macOS aka OSX label Sep 22, 2026
@agocke
agocke requested a review from eduardo-vp September 29, 2026 10:11

@eduardo-vp eduardo-vp 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!

@agocke
agocke requested a balanced review from Copilot October 2, 2026 21:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The targeted synchronization and regression coverage address the documented Rosetta deadlock without affecting other platforms.

Review effort: Balanced
Findings: None

Resolved since last review (1)

This branch has not been deployed

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants