Skip to content

T0006 thread-local-state

Rule: Thread_local! state has one owner, or a reason.

Code T0006 (stable, never reused)
Id thread-local-state
Category state
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small off
small-workspace warn thread-local state is a hidden side channel between functions
medium warn thread-local state is a hidden side channel between functions
big warn thread-local state is a hidden side channel between functions
ai off
embedded off
ui off
egui off
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
types list of strings ["Cell", "RefCell", "Mutex", "RwLock", "UnsafeCell", "OnceCell"] type names that make a thread-local mutable

Why

thread_local! with a Cell or RefCell is global state in disguise: it works on one thread and silently differs on another, and it hides a side channel between functions that do not pass it. tpm's records store kept drafts in a thread-local side channel that one command filled and another read.

How to fix

Pass the state down the call tree (a context argument). A per-thread buffer that only its own thread writes and a rare reader dumps is a fine use: say so in a waiver.

Configure

[lints.thread-local-state]
level = "deny"
types = ["Cell", "RefCell", "Mutex", "RwLock", "UnsafeCell", "OnceCell"]
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(thread-local-state) <reason> (waivers); the code works there too, as in [lints.T0006].