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

Any function in List raised "Null Reference" exception. #112059

Closed
SMAH1 opened this issue Feb 1, 2025 · 3 comments
Closed

Any function in List raised "Null Reference" exception. #112059

SMAH1 opened this issue Feb 1, 2025 · 3 comments

Comments

@SMAH1
Copy link

SMAH1 commented Feb 1, 2025

Description

Hi
I use xUnit for unit test.
I have "Async" method add item to List.
To avoid repeating a key, I use the Any function in the List.
I rarely get a Null Reference exception when running tests.
I can't reproduce this error every time, but I've seen it rarely.

Image

Reproduction Steps

I can't reproduce this error every time, but I've seen it rarely.

Expected behavior

The list has data. Why should it give a no data error when executing linq?

Actual behavior

Run Any method correctly.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

OS: Windows 22H2 (19042.4529).
.NET core:

6.0.425 [C:\Program Files\dotnet\sdk]
7.0.410 [C:\Program Files\dotnet\sdk]
8.0.206 [C:\Program Files\dotnet\sdk]
9.0.102 [C:\Program Files\dotnet\sdk]

Microsoft Visual Studio Community 2022 (64-bit) Version 17.12.4

@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 Feb 1, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 1, 2025
@Tornhoof
Copy link
Contributor

Tornhoof commented Feb 1, 2025

Why an async method for that?
Where do you await the Task?
List is not threadsafe to Insert, so it's likely you do something concurrently with it outside of the screenshot.

@huoyaoyuan huoyaoyuan added area-System.Collections and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Feb 1, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

Concurrent access of List<T> is incorrect. To gather a few items into a list, running in single thread is much more efficient. To gather a huge number of items by utilizing multiple threads, PLINQ is the convenient solution.

Closing as answered.

@huoyaoyuan huoyaoyuan closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants