A [ThreadStatic] field is not static, or has an initializer that runs only once

tx0/csharp:thread-static-misusedefaultwarningreliability/concurrencydepth fastbug

What it finds

[ThreadStatic] works only on static fields; on an instance field it is ignored. Its initializer runs once, on the thread that initializes the type, so every other thread sees the default value instead. Initialize it lazily where it is used.

From the rule's source, thread_static_misuse.rs.

Example from the rule's tests
using System;using System.Text; class Buffers{    [ThreadStatic]    private StringBuilder cache;tx0 reports: [ThreadStatic] has no effect on an instance field     [ThreadStatic]    private static StringBuilder shared = new StringBuilder();tx0 reports: the initializer of the thread-static shared runs on one thread only     [ThreadStatic]    private static StringBuilder lazy;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:thread-static-misuse" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:thread-static-misuse] reason="..."

tx0 explain tx0/csharp:thread-static-misuse shows the same in the terminal. Found a false positive? Report it.