Skip to content

[Windows] Communication gateway fails to start due to _is_pid_alive incompatibility #90

@cbsincool

Description

@cbsincool

Problem

The OpenSpace communication gateway fails to start on Windows due to platform-specific code in _is_pid_alive function.

Error

OSError: [WinError 87] 参数错误。
SystemError: <built-in function kill> returned a result with an exception set

Root Cause

The _is_pid_alive function in openspace/communication/gateway_runtime.py uses os.kill(pid, 0) to check if a process is alive. This works on Unix-like systems but fails on Windows with OSError: [WinError 87].

Current Code (Line 36-46)

def _is_pid_alive(pid: int) -> bool:
    if pid <= 0:
        return False
    try:
        os.kill(pid, 0)
    except ProcessLookupError:
        return False
    except PermissionError:
        return True
    else:
        return True

Proposed Fix

Use Windows API on Windows platform:

def _is_pid_alive(pid: int) -> bool:
    if pid <= 0:
        return False
    try:
        if sys.platform == "win32":
            import ctypes
            kernel32 = ctypes.windll.kernel32
            PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
            STILL_ACTIVE = 259
            handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
            if not handle:
                return False
            try:
                exit_code = ctypes.c_ulong()
                if kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
                    return exit_code.value == STILL_ACTIVE
                return False
            finally:
                kernel32.CloseHandle(handle)
        else:
            os.kill(pid, 0)
    except ProcessLookupError:
        return False
    except PermissionError:
        return True
    else:
        return True

Environment

  • OS: Windows 10/11
  • Python: 3.12.0
  • OpenSpace: v0.1.0

Impact

This bug prevents Windows users from using the Feishu/WhatsApp communication gateway feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions