E0706
rust
type_error
ai_generated
partial
error[E0706]: thread-local variable cannot be borrowed across a yield point
ID: rust/e0706-thread-local-cannot-be-borrowed-across-yield
78%Fix Rate
83%Confidence
1Evidence
2024-02-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.75.0 | active | — | — | — |
| 1.77.0 | active | — | — | — |
| 1.79.0 | active | — | — | — |
Root Cause
A reference to a thread-local variable (e.g., `thread_local!`) is held across an `.await` point in async code, which is unsafe because the thread may change.
generic中文
在异步代码中,对线程局部变量(例如 `thread_local!`)的引用跨越了 `.await` 点,这可能导致线程切换,因此不安全。
Official Documentation
https://doc.rust-lang.org/error_codes/E0706.htmlWorkarounds
-
82% success Clone the thread-local value before the await point: `let val = THREAD_LOCAL.with(|v| v.clone());` then use `val` after await.
Clone the thread-local value before the await point: `let val = THREAD_LOCAL.with(|v| v.clone());` then use `val` after await.
-
75% success Restructure code to avoid holding the borrow across await: use `let result = { THREAD_LOCAL.with(|v| v.do_something()) }; result.await`.
Restructure code to avoid holding the borrow across await: use `let result = { THREAD_LOCAL.with(|v| v.do_something()) }; result.await`.
中文步骤
在 await 点之前克隆线程局部值:`let val = THREAD_LOCAL.with(|v| v.clone());` 然后在 await 后使用 `val`。
重构代码以避免跨 await 持有借用:使用 `let result = { THREAD_LOCAL.with(|v| v.do_something()) }; result.await`。
Dead Ends
Common approaches that don't work:
-
Wrapping the thread-local in `Rc` to extend lifetime
85% fail
`Rc` is not `Send`, so cannot be used across await points in async contexts.
-
Using `unsafe` to extend the borrow lifetime manually
90% fail
Undefined behavior; compiler may still reject or cause data races.