{"record":{"id":"418cae63a35b4fc0","repo":"bevyengine/bevy","slug":"couldn-t-get-the-window-handle-in-time-for-rendere","errorCode":null,"errorMessage":"Couldn't get the window handle in time for renderer initialization","messagePattern":"Couldn't get the window handle in time for renderer initialization","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_render/src/renderer/mod.rs","lineNumber":246,"sourceCode":"        },\n    };\n\n    #[cfg(not(feature = \"raw_vulkan_init\"))]\n    let instance = Instance::new(instance_descriptor);\n    #[cfg(feature = \"raw_vulkan_init\")]\n    let mut additional_vulkan_features = raw_vulkan_init::AdditionalVulkanFeatures::default();\n    #[cfg(feature = \"raw_vulkan_init\")]\n    let instance = raw_vulkan_init::create_raw_vulkan_instance(\n        instance_descriptor,\n        &raw_vulkan_init_settings,\n        &mut additional_vulkan_features,\n    );\n\n    let surface = primary_window.and_then(|wrapper| {\n        let maybe_handle = wrapper\n            .0\n            .lock()\n            .expect(\"Couldn't get the window handle in time for renderer initialization\");\n        if let Some(wrapper) = maybe_handle.as_ref() {\n            // SAFETY: Plugins should be set up on the main thread.\n            let handle = unsafe { wrapper.get_handle() };\n            Some(\n                instance\n                    .create_surface(handle)\n                    .expect(\"Failed to create wgpu surface\"),\n            )\n        } else {\n            None\n        }\n    });\n\n    let force_fallback_adapter = std::env::var(\"WGPU_FORCE_FALLBACK_ADAPTER\")\n        .map_or(options.force_fallback_adapter, |v| {\n            !(v.is_empty() || v == \"0\" || v == \"false\")\n        });\n","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_render/src/renderer/mod.rs#L228-L264","documentation":"During renderer initialization Bevy locks the mutex guarding the primary window's raw-handle wrapper and expects the lock to succeed. std Mutex::lock() only returns Err when the mutex is poisoned — another thread panicked while holding this exact lock. This panic is therefore a secondary symptom: the real failure is an earlier panic on the thread that held the window-handle lock.","triggerScenarios":"Any prior panic on another thread while it held the lock around the window's RawHandleWrapper (for example a panic during surface creation, wgpu error handling, or app setup that had taken the handle), followed by renderer init trying to lock it.","commonSituations":"A panicking render/setup thread that surfaced the handle before failing; nested panic chains during startup; logs that show an earlier unrelated panic right before this one; multithreaded window-handle sharing in custom plugins.","solutions":["Scroll up in the logs and find the FIRST panic — fix that root cause; this expect is only collateral damage","Reproduce the earlier panic with RUST_BACKTRACE=1 to identify which thread held the handle lock","Audit custom code that takes the window handle across threads and ensure it cannot panic while holding the lock"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// If you share the handle wrapper in custom code:\nif my_handle_mutex.is_poisoned() {\n    // an earlier panic holds the lock; fix that root cause before renderer init\n    eprintln!(\"window handle lock poisoned by an earlier panic\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always read the FIRST panic in the log — this expect is downstream collateral damage","Run with RUST_BACKTRACE=1 during startup to identify the thread that poisoned the lock","Avoid panicking while holding locks around window handles in custom plugins"],"tags":["bevy","mutex","poisoned-lock","window-handle","panic"],"backgroundTag":"mutex-poisoned","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}