dotnet / dotnet/runtime

`ThreadLocal<T>` finalization crashes with a `NullReferenceException`

Open
#128,568 14 comments 0 reactions 1 assignee Assigned to @eduardo-vp View on GitHub
area-System.Threading
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

I've received 3 reports of this crash for my desktop app (Paint.NET) over the last 6 months. It's unclear what causes it or how to reproduce it, and it does seem to be super ultra rare. I don't believe `ThreadLocal`'s dispose or finalize logic calls into anything on the `T` so I think this has to be something due to `ThreadLocal`'s code.

```
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Threading.ThreadLocal`1.Dispose(Boolean disposing)
at System.Threading.ThreadLocal`1.Finalize()
at System.GC.RunFinalizers()
```

I'm not sure how actionable this is, but figured I'd report it anyway.

### Reproduction Steps

I'm not sure what the repro steps are, other than "use my app a bunch."

### Expected behavior

`ThreadLocal.Dispose()` should _not_ throw a `NullReferenceException`

### Actual behavior

It does throw a `NullReferenceException`

### Regression?

Unclear, but I've never received a crash report with this callstack before .NET 9

### Known Workarounds

Don't use the app? 😅

### Configuration

The latest crash log that was just sent to me is from:

- AMD Ryzen 7 3700X 8-Core Processor
- Windows 10 Home x64 (10.0.19045.0)
- .NET 9.0.13 x64
- 16GB RAM

The previous crash log I got in December 2025 was:

- AMD Ryzen 7 5700U with Radeon Graphics
- Windows 11 Home Single Language x64 (10.0.22631.0)
- .NET 9.0.7 x64
- 10GB RAM

For the 3rd crash report I don't have any info other than the same callstack.

I didn't see anything else in the crash logs that were weird or suspicious, such as injected DLLs from software that's known to cause weird or "impossible" crashes (SentinelOne, Avast, etc.).

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.