@@ -784,40 +784,38 @@ def get_daemon_pid_path(port: int) -> Path:
784784
785785
786786def _is_windows_pid_running (pid : int ) -> bool :
787- """Check process state without sending a signal on Windows.
788-
789- Indeterminate results are treated as running so a valid daemon lock is not
790- discarded merely because the process cannot be inspected.
791- """
792- import ctypes
793- from ctypes import wintypes
794-
795- synchronize = 0x00100000
796- error_invalid_parameter = 87
797- wait_object_0 = 0x00000000
798- wait_timeout = 0x00000102
799-
800- kernel32 = ctypes .WinDLL ("kernel32" , use_last_error = True )
801- kernel32 .OpenProcess .argtypes = (wintypes .DWORD , wintypes .BOOL , wintypes .DWORD )
802- kernel32 .OpenProcess .restype = wintypes .HANDLE
803- kernel32 .WaitForSingleObject .argtypes = (wintypes .HANDLE , wintypes .DWORD )
804- kernel32 .WaitForSingleObject .restype = wintypes .DWORD
805- kernel32 .CloseHandle .argtypes = (wintypes .HANDLE ,)
806- kernel32 .CloseHandle .restype = wintypes .BOOL
807-
808- handle = kernel32 .OpenProcess (synchronize , False , pid )
809- if not handle :
810- return ctypes .get_last_error () != error_invalid_parameter
811-
812787 try :
813- wait_result = kernel32 .WaitForSingleObject (handle , 0 )
814- if wait_result == wait_object_0 :
815- return False
816- if wait_result == wait_timeout :
788+ import ctypes
789+ from ctypes import wintypes
790+
791+ synchronize = 0x00100000
792+ error_invalid_parameter = 87
793+ wait_object_0 = 0x00000000
794+ wait_timeout = 0x00000102
795+
796+ kernel32 = ctypes .WinDLL ("kernel32" , use_last_error = True )
797+ kernel32 .OpenProcess .argtypes = (wintypes .DWORD , wintypes .BOOL , wintypes .DWORD )
798+ kernel32 .OpenProcess .restype = wintypes .HANDLE
799+ kernel32 .WaitForSingleObject .argtypes = (wintypes .HANDLE , wintypes .DWORD )
800+ kernel32 .WaitForSingleObject .restype = wintypes .DWORD
801+ kernel32 .CloseHandle .argtypes = (wintypes .HANDLE ,)
802+ kernel32 .CloseHandle .restype = wintypes .BOOL
803+
804+ handle = kernel32 .OpenProcess (synchronize , False , pid )
805+ if not handle :
806+ return ctypes .get_last_error () != error_invalid_parameter
807+
808+ try :
809+ wait_result = kernel32 .WaitForSingleObject (handle , 0 )
810+ if wait_result == wait_object_0 :
811+ return False
812+ if wait_result == wait_timeout :
813+ return True
817814 return True
815+ finally :
816+ kernel32 .CloseHandle (handle )
817+ except Exception :
818818 return True
819- finally :
820- kernel32 .CloseHandle (handle )
821819
822820
823821def is_pid_running (pid : int ) -> bool :
@@ -852,11 +850,23 @@ def get_running_daemon_pid(port: int) -> int | None:
852850 return None
853851 try :
854852 pid = int (pid_path .read_text ().strip ())
853+ except (OSError , ValueError ):
854+ try :
855+ pid_path .unlink (missing_ok = True )
856+ except OSError :
857+ pass
858+ return None
859+
860+ try :
855861 if is_pid_running (pid ):
856862 return pid
857- pid_path .unlink (missing_ok = True )
858863 except Exception :
864+ return pid
865+
866+ try :
859867 pid_path .unlink (missing_ok = True )
868+ except OSError :
869+ pass
860870 return None
861871
862872
0 commit comments