{"record":{"id":"5b5b8e1153d8efd2","repo":"slint-ui/slint","slug":"spawn-is-called-while-the-slintcontext-is-still-alive","errorCode":null,"errorMessage":"spawn is called while the SlintContext is still alive","messagePattern":"spawn is called while the SlintContext is still alive","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backends/winit/xdg_desktop_settings.rs","lineNumber":279,"sourceCode":"            name.as_str(),\n            \"org.freedesktop.DBus.Error.ServiceUnknown\"\n                | \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n        ),\n        zbus::Error::FDO(err) => matches!(\n            **err,\n            zbus::fdo::Error::ServiceUnknown(_) | zbus::fdo::Error::NameHasNoOwner(_)\n        ),\n        _ => false,\n    }\n}\n\n/// Starts the portal watcher and the timeout that releases the first windows if\n/// the portal is slow or missing. Returns the task so the backend can abort it.\npub(crate) fn spawn(\n    shared_data: &Rc<SharedBackendData>,\n    ctx: &SlintContextWeak,\n) -> Option<i_slint_core::future::JoinHandle<()>> {\n    let strong_ctx = ctx.upgrade().expect(\"spawn is called while the SlintContext is still alive\");\n    // Hold back the first windows until the query applies the real appearance.\n    shared_data.desktop_settings.appearance_pending.set(true);\n\n    let shared_weak = Rc::downgrade(shared_data);\n    let ctx_weak = ctx.clone();\n    strong_ctx\n        .spawn_local(async move {\n            if let Err(err) = watch(&shared_weak, ctx_weak).await {\n                if !portal_unavailable(&err) {\n                    i_slint_core::debug_log!(\"Error watching for xdg desktop settings: {err}\");\n                }\n                // The portal is unavailable; create the waiting windows anyway.\n                finish_appearance_query(&shared_weak);\n            }\n        })\n        .ok()\n}\n","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/internal/backends/winit/xdg_desktop_settings.rs#L261-L297","documentation":"`xdg_desktop_settings::spawn()` starts the portal watcher that tracks desktop appearance settings (dark mode etc.). It upgrades the passed `SlintContextWeak`; if the context is already gone the function cannot proceed and panics, since spawn must be called while the context is alive.","triggerScenarios":"Calling `spawn` (from backend startup) after the `SlintContext` was dropped or before it was fully created - i.e. backend shutdown/teardown racing with settings initialization.","commonSituations":"Dropping the Slint platform/context very early in embedded or custom-platform setups; shutting down the app during initialization; misordered platform creation in custom integrations.","solutions":["Keep the `SlintContext` (platform) alive until after backend startup completes; only drop it at process end","Ensure `slint::platform::set_platform()` is called before any teardown paths run","If this occurs during normal shutdown, defer dropping the context until after the event loop exits"],"exampleFix":"// before\n{\n    let ctx = slint::platform::Platform::context();\n    drop(ctx); // context gone\n    start_backend(); // spawn panics upgrading weak ctx\n}\n// after\n{\n    let ctx = slint::platform::Platform::context();\n    start_backend(); // spawn succeeds\n    // ctx dropped last\n}","handlingStrategy":"type-guard","validationCode":"// ensure the platform context is alive before backend startup\nlet ctx = slint::platform::Platform::context(); // strong reference held\nbackend_start(&ctx);","typeGuard":null,"tryCatchPattern":"std::panic::catch_unwind(|| xdg_desktop_settings::spawn(&shared, &ctx_weak))\n    .map_err(|_| anyhow!(\"SlintContext dropped before portal watcher spawn\"))?;","preventionTips":["Hold the SlintContext until the process/event loop ends","Call set_platform before any shutdown paths can run","Defer dropping the context until after the event loop exits"],"tags":["linux","portal","lifetime","panic"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}