Skip to content

[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 as not planned

Description

@drweb86

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

Activity

  1. drweb86 commented on Nov 29, 2024

    @drweb86
    Author

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

  2. 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.'[/+] on Nov 29, 2024
  3. ViktorHofer commented on Dec 3, 2024

    @ViktorHofer
    Member

    @dotnet/area-system-collections

  4. transferred this issue fromdotnet/coreon Dec 3, 2024
  5. ghost added
    needs-area-label
    on Dec 3, 2024
  6. added
    untriaged
    on Dec 3, 2024
  7. added
    untriaged
    and removed
    untriaged
    needs-area-label
    on Dec 3, 2024
  8. added this to the 9.0.x milestone on Dec 3, 2024
  9. removed
    untriaged
    on Dec 3, 2024
  10. stephentoub commented on Dec 4, 2024

    @stephentoub
    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.

  11. added
    needs-author-action
    on Dec 4, 2024
  12. drweb86 commented on Dec 4, 2024

    @drweb86
    Author
  13. added
    needs-further-triage
    and removed
    needs-author-action
    on Dec 4, 2024
  14. added
    needs-author-action
    and removed
    needs-further-triage
    on Dec 5, 2024
  15. drweb86 commented on Dec 5, 2024

    @drweb86
    Author

    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.

  16. added
    needs-further-triage
    and removed
    needs-author-action
    on Dec 5, 2024
  17. stephentoub commented on Dec 6, 2024

    @stephentoub
    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?

  18. drweb86 commented on Dec 6, 2024

    @drweb86
    Author

    @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.

  19. stephentoub commented on Dec 6, 2024

    @stephentoub
    Member

    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.

  20. stephentoub commented on Dec 6, 2024

    @stephentoub
    Member

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

  21. drweb86 commented on Dec 6, 2024

    @drweb86
    Author

    @stephentoub Thanks!

  22. locked and limited conversation to collaborators on Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Collectionsneeds-further-triage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions