U0001 ui-raw-spawn¶
Rule: UI code starts threads and tasks only through the project's spawn wrapper.
| Code | U0001 (stable, never reused) |
| Id | ui-raw-spawn |
| Category | ui |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | off | |
| ai | off | |
| embedded | off | |
| ui | warn |
work started outside the spawn wrapper has no name, no crash report and no repaint; warn while toolkits ship no wrapper |
| egui | warn |
work started outside the spawn wrapper has no name, no crash report and no repaint; vhrd_egui has no wrapper yet, so warn |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
functions |
list of strings | ["std::thread::spawn", "std::thread::Builder::new", "tokio::spawn", "tokio::task::spawn", "tokio::task::spawn_blocking", "tokio::task::spawn_local", "async_std::task::spawn", "smol::spawn", "rayon::spawn", "wasm_bindgen_futures::spawn_local"] |
functions that start a thread or a task |
wrapper |
string | "" |
the project's spawn wrapper, named in the help |
ui-types |
list of strings | [] |
parameter types of a function that is UI code; empty: every function of a UI crate |
toolkits |
list of strings | ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"] |
UI toolkits: a crate that depends on one directly is a UI crate, unless the crate map says ui = false |
Why¶
A thread or task started from UI code has to do three things the UI depends on: carry a name that shows in a crash report and a profiler, report its failure to the person instead of dying silently, and ask the UI for a repaint when it has news. One spawn wrapper does all three for every call; each raw std::thread::spawn or tokio::spawn does them by hand or not at all. rerun bans its raw spawn_local for the same reason.
The lint looks at UI crates and reports every call of a function in functions, resolved through the file's use items. With ui-types set (the egui tier: Ui, Context, Frame), only functions that take a parameter of one of these types count as UI code; a worker module of the same crate is left alone. The crate that implements the wrapper gets an exception in spirit_level.toml.
std::thread::spawn(move || load(path)); // reported
app.spawn("load project", move || load(path)); // the wrapper
How to fix¶
Start the work through the project's spawn wrapper (the wrapper parameter names it in the help). Where the project has none yet, write one: it takes a name, runs the closure, catches its error and requests a repaint.
Configure¶
[lints.ui-raw-spawn]
level = "deny"
functions = ["std::thread::spawn", "std::thread::Builder::new", "tokio::spawn", "tokio::task::spawn", "tokio::task::spawn_blocking", "tokio::task::spawn_local", "async_std::task::spawn", "smol::spawn", "rayon::spawn", "wasm_bindgen_futures::spawn_local"]
wrapper = ""
ui-types = []
toolkits = ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"]
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(ui-raw-spawn) <reason> (waivers); the code works there too, as in [lints.U0001].