Skip to content
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

[Win11] [Bug] [ConcurrentBag regression in .Net 9] System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' #110355

Closed
drweb86 opened this issue Nov 29, 2024 · 10 comments
Labels
area-System.Collections needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@drweb86
Copy link

drweb86 commented Nov 29, 2024

Steps:

  1. Create new .Net 9 project and put code in Visual Studio 2022
using System.Collections.Concurrent;

var breakingBadHoleInTheBag = new ConcurrentBag<ConcurrentBagItem>();

int tries = 500000;
while (tries-- > 0)
{
    breakingBadHoleInTheBag.SingleOrDefault(x => x == (object)breakingBadHoleInTheBag);
    breakingBadHoleInTheBag.Add(new ConcurrentBagItem());
}

class ConcurrentBagItem { }
  1. Execute with or without debugger

Actual result:

An unhandled exception of type 'System.AccessViolationException' occurred in System.Collections.Concurrent.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Expected result:

Works without error, as in .Net 9.

Please introduce ConcurrentBag - from .Net 8 because uinstable work in .Net 9 breaks my app

@drweb86
Copy link
Author

drweb86 commented Nov 29, 2024

Image
If to go through stack trace - System.Linq is third one, Ion double click on it VIsual Studio 2022 crashes (!).

@drweb86 drweb86 changed the title System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' [Win11] [Bug] [ConcurrentBag regression in .Net 9] System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' Nov 29, 2024
@ViktorHofer
Copy link
Member

@dotnet/area-system-collections

@ViktorHofer ViktorHofer transferred this issue from dotnet/core Dec 3, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 3, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 3, 2024
@ViktorHofer ViktorHofer added area-System.Collections untriaged New issue has not been triaged by the area owner and removed untriaged New issue has not been triaged by the area owner needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 3, 2024
@ViktorHofer ViktorHofer added this to the 9.0.x milestone Dec 3, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 3, 2024
@stephentoub
Copy link
Member

It's very unlikely this is actually related to ConcurrentBag. It's more likely some code in your app is resulting in corruption that then appears as an AV in other code, in this case in CB. Without a repro or crash dump, though, we can't investigate.

@eiriktsarpalis eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label Dec 4, 2024
@drweb86
Copy link
Author

drweb86 commented Dec 4, 2024 via email

@dotnet-policy-service dotnet-policy-service bot added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 4, 2024
@jeffhandley jeffhandley added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels Dec 5, 2024
@drweb86
Copy link
Author

drweb86 commented Dec 5, 2024

Here's the reproduction sample

using System.Collections.Concurrent;

var breakingBadHoleInTheBag = new ConcurrentBag<ConcurrentBagItem>();

int tries = 500000;
while (tries-- > 0)
{
    breakingBadHoleInTheBag.SingleOrDefault(x => x == (object)breakingBadHoleInTheBag);
    breakingBadHoleInTheBag.Add(new ConcurrentBagItem());
}

class ConcurrentBagItem { }

Build for .Net 9 and run => Access Violation,
Build for .Net 8 and run => No error.

Please return .Net 8 implementation since other software might expect old behavior, or new one create with postsfix ConcurrentBagV2/put into breaking changes list.

I updated first post with steps.

@dotnet-policy-service dotnet-policy-service bot added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 5, 2024
@stephentoub
Copy link
Member

Thanks for the repro.

This is not about the ConcurrentBag implementation.

I suspect this may be the same as the issue fixed by #109074. If you add <CETCompat>false</CETCompat> into a PropertyGroup in your csproj, does the problem go away?

@drweb86
Copy link
Author

drweb86 commented Dec 6, 2024

@stephentoub ,

Adding

<CETCompat>false</CETCompat>

worked.

But guys, seriously, we're in the dark times, when its needed to modify csproj config to make ConcurrentBag work.

@stephentoub
Copy link
Member

stephentoub commented Dec 6, 2024

But guys, seriously, we're in the dark times, when its needed to modify csproj config to make ConcurrentBag work.

Again, this has little to do with ConcurrentBag. It is a runtime bug to do with Intel's CET security mechanism, it's already been fixed in main with #109074 and it's being fixed in 9.0 with #109548 (cc: @janvorli, @jeffschwMSFT). I mentioned the csproj property to both confirm this is the same problem and to give you a temporary workaround.

@stephentoub
Copy link
Member

Closing as a duplicate of #109074. Thanks for reporting.

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Dec 6, 2024
@drweb86
Copy link
Author

drweb86 commented Dec 6, 2024

@stephentoub Thanks!

@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

5 participants