{"record":{"id":"9a5ff6fb4d8ed5bc","repo":"LGUG2Z/komorebi","slug":"could-not-close-window","errorCode":null,"errorMessage":"could not close window","messagePattern":"could not close window","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"komorebi/src/windows_api.rs","lineNumber":672,"sourceCode":"            };\n        } else {\n            unsafe {\n                let _ = ShowWindow(HWND(as_ptr!(hwnd)), command);\n            };\n        }\n    }\n\n    pub fn minimize_window(hwnd: isize) {\n        Self::show_window(hwnd, SW_MINIMIZE);\n    }\n\n    fn post_message(hwnd: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> eyre::Result<()> {\n        unsafe { PostMessageW(Option::from(hwnd), message, wparam, lparam) }.process()\n    }\n\n    pub fn close_window(hwnd: isize) -> eyre::Result<()> {\n        if Self::post_message(HWND(as_ptr!(hwnd)), WM_CLOSE, WPARAM(0), LPARAM(0)).is_err() {\n            bail!(\"could not close window\");\n        }\n\n        Ok(())\n    }\n\n    pub fn hide_window(hwnd: isize) {\n        Self::show_window(hwnd, SW_HIDE);\n    }\n\n    pub fn restore_window(hwnd: isize) {\n        Self::show_window(hwnd, SW_SHOWNOACTIVATE);\n    }\n\n    pub fn unmaximize_window(hwnd: isize) {\n        Self::show_window(hwnd, SW_NORMAL);\n    }\n\n    pub fn maximize_window(hwnd: isize) {","sourceCodeStart":654,"sourceCodeEnd":690,"githubUrl":"https://github.com/LGUG2Z/komorebi/blob/e0709f02bfae4e503bf4640f58ee75ecbbfdbb97/komorebi/src/windows_api.rs#L654-L690","documentation":"komorebi's close_window sends a WM_CLOSE message to the target window via the Win32 PostMessageW API. If PostMessageW returns an error (e.g. invalid handle or message refused), komorebi bails with 'could not close window'. It means the OS refused the request to close that window.","triggerScenarios":"Calling WindowsApi::close_window(hwnd) with an hwnd that is stale/invalid, belongs to another desktop or elevated process, or when PostMessageW fails for a protected/system window.","commonSituations":"A window was closed or recreated between layout passes so komorebi holds a dead hwnd; attempting to close elevated (admin) application windows from a non-elevated komorebi; UWP/system windows that ignore WM_CLOSE.","solutions":["Verify the hwnd is still valid before closing (IsWindow) and refresh komorebi's window list","Run komorebi with the same or higher privilege level as the target application","Check that the window isn't a protected system/elevated process window; use taskkill or close it manually","Restart komorebi to resynchronize hwnd state"],"exampleFix":"// before\nWindowsApi::close_window(stale_hwnd)?;\n// after\nif WindowsApi::is_window(stale_hwnd) {\n    WindowsApi::close_window(stale_hwnd)?;\n}","handlingStrategy":"try-catch","validationCode":"// Rust (windows crate)\nuse windows::Win32::UI::WindowsAndMessaging::IsWindow;\nfn can_close(hwnd: isize) -> bool {\n    unsafe { IsWindow(Some(HWND(hwnd as _))).as_bool() }\n}","typeGuard":"fn window_is_valid(hwnd: isize) -> bool {\n    hwnd != 0 && unsafe { IsWindow(Some(HWND(hwnd as _))).as_bool() }\n}","tryCatchPattern":"match WindowsApi::close_window(hwnd) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"could not close window\") => {\n        log::warn!(\"window {hwnd} refused WM_CLOSE or is gone; refreshing window list\");\n        refresh_window_state();\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Check IsWindow before sending WM_CLOSE to cached hwnds","Run komorebi at the same elevation level as target applications","Resubscribe/refresh hwnd state after window destroy/create events","Avoid closing elevated or UWP system windows via WM_CLOSE"],"tags":["win32","window-management"],"backgroundTag":"api-request-failed","analyzedSha":"e0709f02bfae4e503bf4640f58ee75ecbbfdbb97","analyzedAt":"2026-09-06T07:08:05.107Z","contentChangedAt":"2026-09-06T07:08:05.107Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}