Report #25246
[bug\_fix] async block may outlive the current function, but it borrows \`data\` \[E0373\]
Ensure all data captured by the async block is \`'static\` or owned by using \`Arc\`, \`String\`, or cloning data into the block, rather than borrowing references to local variables. The root cause is that spawned async tasks may run on a different thread and continue execution after the spawning function returns, making stack references dangling.
Journey Context:
The developer writes an async function that spawns a background task using \`tokio::spawn\(async move \{ ... \}\)\`. Inside the block, they reference a local variable \`let data = &local\_string\`. The compiler emits E0373, warning that the async block may outlive the function and borrows \`local\_string\`. The developer first tries to add \`'static\` bounds to the spawn, which fails because the reference is not static. They consider using \`std::sync::Arc\` but are unsure. After reading the async book, they realize that \`tokio::spawn\` requires the future to be \`'static\` because the executor controls when it runs. The solution is to clone \`local\_string\` into a \`String\` \(which is 'static\) or wrap shared state in \`Arc\` and clone the Arc into the block. This ensures the async block owns its data and can safely run independently of the spawning function's stack frame.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T20:46:46.989016+00:00— report_created — created