tokio-rs/tokio · error · syn::Error
`unhandled_panic` set multiple times.
Error message
`unhandled_panic` set multiple times.
What it means
`set_unhandled_panic` (entry.rs:171) records the first `unhandled_panic = "..."` (one of `ignore` / `shutdown_runtime`, controlling behavior when a task panics) and rejects a second one. The option is only valid for the `current_thread` flavor.
Source
Thrown at tokio-macros/src/entry.rs:173
fn set_crate_name(&mut self, name: syn::Lit, span: Span) -> Result<(), syn::Error> {
if self.crate_name.is_some() {
return Err(syn::Error::new(span, "`crate` set multiple times."));
}
let name_path = parse_path(name, span, "crate")?;
self.crate_name = Some(name_path);
Ok(())
}
fn set_unhandled_panic(
&mut self,
unhandled_panic: syn::Lit,
span: Span,
) -> Result<(), syn::Error> {
if self.unhandled_panic.is_some() {
return Err(syn::Error::new(
span,
"`unhandled_panic` set multiple times.",
));
}
let unhandled_panic = parse_string(unhandled_panic, span, "unhandled_panic")?;
let unhandled_panic =
UnhandledPanic::from_str(&unhandled_panic).map_err(|err| syn::Error::new(span, err))?;
self.unhandled_panic = Some((unhandled_panic, span));
Ok(())
}
fn macro_name(&self) -> &'static str {
if self.is_test {
"tokio::test"
} else {
"tokio::main"
}
}
View on GitHub (pinned to 625954f365)
Solutions
- Remove the duplicate `unhandled_panic`, keeping the single intended behavior.
Example fix
// before
#[tokio::main(flavor = "current_thread", unhandled_panic = "ignore", unhandled_panic = "shutdown_runtime")]
async fn main() {}
// after
#[tokio::main(flavor = "current_thread", unhandled_panic = "shutdown_runtime")]
async fn main() {} Defensive patterns
Strategy: validation
Prevention
- Set `unhandled_panic` at most once and only with `flavor = "current_thread"`.
- Choose `ignore` to keep the runtime alive or `shutdown_runtime` to tear it down on task panic.
- Document the chosen policy for your team.
When it happens
Trigger: Writing `#[tokio::main(unhandled_panic = "ignore", unhandled_panic = "shutdown_runtime")]`.
Common situations: Copy-paste; iterating on panic policy and forgetting to remove the previous setting.
Related errors
- `name` set multiple times.
- `flavor` set multiple times.
- `worker_threads` set multiple times.
- `start_paused` set multiple times.
- `crate` set multiple times.
AI-assisted analysis of tokio-rs/tokio@625954f365 (2026-08-11).
Data as JSON: /api/errors/d423470905cbb82b.
Report an issue: GitHub.