{"record":{"id":"439c766333eb084e","repo":"slint-ui/slint","slug":"the-winit-event-loop-runs-inside-the-context-that-owns-this","errorCode":null,"errorMessage":"the winit event loop runs inside the context that owns this backend","messagePattern":"the winit event loop runs inside the context that owns this backend","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backends/winit/lib.rs","lineNumber":483,"sourceCode":"    is_wayland: bool,\n    /// Desktop settings read from the XDG portal (cursor blink, appearance query).\n    #[cfg(xdg_desktop_settings)]\n    desktop_settings: xdg_desktop_settings::DesktopSettings,\n    #[cfg(target_os = \"ios\")]\n    #[allow(unused)]\n    keyboard_notifications: ios::KeyboardNotifications,\n    #[cfg(target_os = \"ios\")]\n    #[allow(unused)]\n    scene_lifecycle: ios::SceneLifecycle,\n}\n\nimpl SharedBackendData {\n    /// Panics if the backend is not bound: an event loop only runs inside a live context.\n    pub(crate) fn context(&self) -> i_slint_core::SlintContext {\n        self.context\n            .get()\n            .and_then(|ctx| ctx.upgrade())\n            .expect(\"the winit event loop runs inside the context that owns this backend\")\n    }\n\n    fn new(\n        mut builder: EventLoopBuilder,\n        renderer_name: Option<String>,\n        requested_graphics_api: Option<RequestedGraphicsAPI>,\n        allow_fallback: bool,\n    ) -> Result<Self, PlatformError> {\n        #[cfg(not(target_arch = \"wasm32\"))]\n        use raw_window_handle::HasDisplayHandle;\n\n        #[cfg(all(unix, not(target_vendor = \"apple\")))]\n        {\n            #[cfg(feature = \"wayland\")]\n            {\n                use winit::platform::wayland::EventLoopBuilderExtWayland;\n                builder.with_any_thread(true);\n            }","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/slint-ui/slint/blob/3a7e700487dc74d404cad0726378053ab763d4e0/internal/backends/winit/lib.rs#L465-L501","documentation":"`SharedBackendData::context()` returns the `SlintContext` that owns the winit backend, stored as a weak reference. The event loop only ever runs while a live context exists, so if the weak upgrade fails this is an internal invariant violation and the code panics.","triggerScenarios":"Calling code that resolves `SharedBackendData::context()` when the owning `SlintContext` has already been dropped (or before it was registered), i.e. backend internals running without the platform context alive.","commonSituations":"Holding a backend/event-loop handle after dropping the `SlintContext`; custom platform setups that drop the context while the winit loop is referenced; bugs in embedding order of context creation vs backend use.","solutions":["Keep the `SlintContext` (and platform) alive for as long as the winit backend/event loop is used","Initialize the Slint platform before creating windows or touching backend internals","If hit inside the library, report a bug - this path should be unreachable from normal API use"],"exampleFix":"// before\nlet backend = get_backend();\ndrop(slint_context); // context freed\nlet ctx = backend.context(); // panics\n// after\nlet ctx = slint_context.context(); // keep context alive first\nlet backend = get_backend();\nlet ctx = backend.context(); // ok","handlingStrategy":"type-guard","validationCode":"// keep a strong SlintContext for the lifetime of the app\nlet ctx = slint_context.clone(); // SlintContext handle held until process exit","typeGuard":null,"tryCatchPattern":"std::panic::catch_unwind(|| backend.context())\n    .map_err(|_| anyhow!(\"SlintContext dropped before backend use; keep platform alive\"))?;","preventionTips":["Hold the SlintContext/platform until after the event loop exits","Initialize the platform before creating windows or touching backend APIs","Treat this panic as an embedding-order bug; audit drop order in custom platform setups"],"tags":["winit","lifetime","panic"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3a7e700487dc74d404cad0726378053ab763d4e0","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}