{"record":{"id":"80af4aed287e8fe5","repo":"LGUG2Z/komorebi","slug":"could-not-determine-current-session-id","errorCode":null,"errorMessage":"could not determine current session id","messagePattern":"could not determine current session id","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"komorebi/src/windows_api.rs","lineNumber":867,"sourceCode":"                HWND(as_ptr!(hwnd)),\n                Option::from(std::ptr::addr_of_mut!(process_id)),\n            )\n        };\n\n        (process_id, thread_id)\n    }\n\n    pub fn current_process_id() -> u32 {\n        unsafe { GetCurrentProcessId() }\n    }\n\n    pub fn process_id_to_session_id() -> eyre::Result<u32> {\n        let process_id = Self::current_process_id();\n        let mut session_id = 0;\n\n        unsafe {\n            if ProcessIdToSessionId(process_id, &mut session_id).is_err() {\n                bail!(\"could not determine current session id\")\n            }\n        }\n\n        Ok(session_id)\n    }\n\n    #[cfg(target_pointer_width = \"64\")]\n    fn set_window_long_ptr_w(\n        hwnd: HWND,\n        index: WINDOW_LONG_PTR_INDEX,\n        new_value: isize,\n    ) -> eyre::Result<()> {\n        Result::from(WindowsResult::from(unsafe {\n            SetWindowLongPtrW(hwnd, index, new_value)\n        }))\n        .map(|_| {})\n    }\n","sourceCodeStart":849,"sourceCodeEnd":885,"githubUrl":"https://github.com/LGUG2Z/komorebi/blob/e0709f02bfae4e503bf4640f58ee75ecbbfdbb97/komorebi/src/windows_api.rs#L849-L885","documentation":"process_id_to_session_id calls the Win32 ProcessIdToSessionId API on the current process id to obtain the terminal-services session id. If the API fails, komorebi bails with 'could not determine current session id'. This is essentially an OS-level failure to map the current PID to a session.","triggerScenarios":"Calling WindowsApi::process_id_to_session_id() in an environment where ProcessIdToSessionId fails, e.g. the process is not attached to an interactive session or the API returns FALSE (invalid process id, service-session contexts).","commonSituations":"Running komorebi from a service session, SSH session, or scheduled task that is not attached to the interactive desktop session; heavily restricted sandboxed environments.","solutions":["Run komorebi inside the interactive user desktop session (not as a service or from SSH)","Verify ProcessIdToSessionId's return value/error code with GetLastError for the specific cause","Ensure the current process id is valid and not terminating","Avoid calling this in service contexts; gate it behind an interactive-session check"],"exampleFix":"// before\nlet session_id = WindowsApi::process_id_to_session_id()?;\n// after\nlet session_id = WindowsApi::process_id_to_session_id()\n    .context(\"komorebi must run in an interactive user session\")?;","handlingStrategy":"try-catch","validationCode":"// Check process is in the interactive session\nfn is_interactive_session() -> bool {\n    std::env::var(\"SESSIONNAME\").map(|s| s.starts_with(\"Console\")).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"let session_id = match WindowsApi::process_id_to_session_id() {\n    Ok(id) => id,\n    Err(e) => {\n        log::error!(\"komorebi must run in an interactive desktop session: {e}\");\n        std::process::exit(1);\n    }\n};","preventionTips":["Launch komorebi from the user's interactive desktop session, never as a service","Avoid running komorebi over SSH or from session-0 contexts","Log GetLastError details when ProcessIdToSessionId fails to diagnose session context","Document the interactive-session requirement in your deployment scripts"],"tags":["win32","session"],"backgroundTag":"unsupported-platform","analyzedSha":"e0709f02bfae4e503bf4640f58ee75ecbbfdbb97","analyzedAt":"2026-09-06T07:08:05.107Z","contentChangedAt":"2026-09-06T07:08:05.107Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}