Skip to content

System.IO.DriveInfo.GetDrives() random AccessViolationException on macOS #122634

@picrap

Description

@picrap

Description

We noticed a very random (and quite rare) System.AccessViolationException poping out of a System.IO.DriveInfo.GetDrives() call.
This happens on a mac M1 in a CI/CD environment, in tests phase (using Gitlab Runner, however my guess is a concurrency problem, not specific to this environment).

Reproduction Steps

The code is:

System.IO.DriveInfo.GetDrives();

Expected behavior

A list, not an exception 😉

Actual behavior

It throws the following:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.SpanHelpers.IndexOfNullByte(Byte*)
   at System.Runtime.InteropServices.Marshal.PtrToStringUTF8(IntPtr)
   at Interop+Sys.AddMountPoint(Void*, Byte*)
   at Interop+Sys.GetAllMountPoints()
   at System.IO.DriveInfo.GetDrives()

Regression?

We noticed the same problem with .NET 6, so my guess is that it never worked.

Known Workarounds

The only workaround known to me is to rewrite the method, since the System.AccessViolationException can’t be caught.

Configuration

  • .NET 8.0.22 (we had the same problem in .NET 6)
  • macOS Sequoia 15.5
  • ARM64 (an apple M1 actually)
  • The problem is not specific to that configuration, however we anly observed it on macOS, never on any Linux

Other information

As far as my thoughts go, it may be related to invoking an thread-unsafe method without locking it (but I could figure it out in the dotnet source code)

Metadata

Metadata

Labels

area-System.IOuntriagedNew issue has not been triaged by the area owner

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions