Skip to content

Add documentation warning for Interlocked.Read write access requirement on 32-bit platforms #11659

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 2 commits into from
Aug 5, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 5, 2025

This PR adds critical documentation to the Interlocked.Read methods to warn developers about a non-intuitive behavior that can cause crashes.

Problem

The Interlocked.Read method takes a readonly reference parameter (in parameter), which suggests it only needs read access to the memory location. However, on 32-bit platforms, the method internally uses CompareExchange to ensure atomicity, which requires write access to the memory location.

This causes AccessViolationException when developers attempt to use Interlocked.Read with read-only memory, such as memory-mapped files opened with read-only access:

// This crashes on 32-bit platforms despite appearing safe
using var memoryMappedFile = MemoryMappedFile.CreateOrOpen("TestFile", 100, MemoryMappedFileAccess.Read);
using var accessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);

unsafe ref readonly long firstLong = ref GetReadOnlyReference(accessor);
long value = Interlocked.Read(in firstLong); // AccessViolationException!

Solution

Added important documentation warnings to both Interlocked.Read(ref long) and Interlocked.Read(ref ulong) methods that clearly explain:

  1. Despite taking a readonly reference parameter, the method requires write access on 32-bit platforms
  2. This is due to internal use of CompareExchange for atomicity
  3. Using it with read-only memory can result in AccessViolationException
  4. The issue specifically affects 32-bit platforms only

The documentation uses a prominent [!IMPORTANT] callout to ensure this critical limitation is highly visible to developers.

Impact

This change helps prevent runtime crashes by making developers aware of this limitation upfront, allowing them to choose alternative approaches when working with read-only memory on 32-bit platforms.

Fixes #11658.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Interlocked.Read takes readonly ref despite requiring write access which causes crashes Add documentation warning for Interlocked.Read write access requirement on 32-bit platforms Aug 5, 2025
Copilot finished work on behalf of jkotas August 5, 2025 21:35
@Copilot Copilot AI requested a review from jkotas August 5, 2025 21:35
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM

@jkotas jkotas requested a review from gewarren August 5, 2025 21:56
@jkotas jkotas marked this pull request as ready for review August 5, 2025 21:57
@jkotas jkotas merged commit b314392 into main Aug 5, 2025
8 checks passed
@jkotas jkotas deleted the copilot/fix-11658 branch August 5, 2025 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interlocked.Read takes readonly ref despite requiring write access which causes crashes
3 participants