LGUG2Z/komorebi · error

could not determine current session id

Error message

could not determine current session id

What it means

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.

Source

Thrown at komorebi/src/windows_api.rs:867

                HWND(as_ptr!(hwnd)),
                Option::from(std::ptr::addr_of_mut!(process_id)),
            )
        };

        (process_id, thread_id)
    }

    pub fn current_process_id() -> u32 {
        unsafe { GetCurrentProcessId() }
    }

    pub fn process_id_to_session_id() -> eyre::Result<u32> {
        let process_id = Self::current_process_id();
        let mut session_id = 0;

        unsafe {
            if ProcessIdToSessionId(process_id, &mut session_id).is_err() {
                bail!("could not determine current session id")
            }
        }

        Ok(session_id)
    }

    #[cfg(target_pointer_width = "64")]
    fn set_window_long_ptr_w(
        hwnd: HWND,
        index: WINDOW_LONG_PTR_INDEX,
        new_value: isize,
    ) -> eyre::Result<()> {
        Result::from(WindowsResult::from(unsafe {
            SetWindowLongPtrW(hwnd, index, new_value)
        }))
        .map(|_| {})
    }

View on GitHub (pinned to e0709f02bf)

Solutions

  1. Run komorebi inside the interactive user desktop session (not as a service or from SSH)
  2. Verify ProcessIdToSessionId's return value/error code with GetLastError for the specific cause
  3. Ensure the current process id is valid and not terminating
  4. Avoid calling this in service contexts; gate it behind an interactive-session check

Example fix

// before
let session_id = WindowsApi::process_id_to_session_id()?;
// after
let session_id = WindowsApi::process_id_to_session_id()
    .context("komorebi must run in an interactive user session")?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Check process is in the interactive session
fn is_interactive_session() -> bool {
    std::env::var("SESSIONNAME").map(|s| s.starts_with("Console")).unwrap_or(false)
}

Try / catch

let session_id = match WindowsApi::process_id_to_session_id() {
    Ok(id) => id,
    Err(e) => {
        log::error!("komorebi must run in an interactive desktop session: {e}");
        std::process::exit(1);
    }
};

Prevention

When it happens

Trigger: 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).

Common situations: Running komorebi from a service session, SSH session, or scheduled task that is not attached to the interactive desktop session; heavily restricted sandboxed environments.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of LGUG2Z/komorebi@e0709f02bf (2026-09-06). Data as JSON: /api/errors/80af4aed287e8fe5. Report an issue: GitHub.