Skip to content

Answer whether a pid is alive, on Windows and in one place - #117

Merged
fylorn merged 1 commit into
mainfrom
feat/windows-proc-alive
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/windows-proc-alive

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Two callers were asking the same question with two copies of the answer, and on Windows both copies said true unconditionally:

caller what the stub did
the single-instance lock (lockfile.rs) reads a pid left behind by a crash, believes another instance holds it, refuses to start. Forever.
--parent (main.rs) never notices the UI is gone, so quitting the app leaves core running and holding the gateway port

The second one matters more than it looks: "it is one program" is the entire reason that flag exists.

The two copies also disagreed

The lock counted "exists but is not ours" (EPERM) as alive. The parent watch counted it as dead. There is now one answer, and it is the safe one — when we cannot tell, the process is alive. Getting that backwards either deletes a lock another instance is holding, or exits for no reason.

Why the Windows check waits instead of reading the exit code

It waits zero milliseconds on the process handle. A process handle becomes signalled when the process exits, so "the wait timed out" is exactly "still running".

The more widely shared recipe — GetExitCodeProcess, compare against STILL_ACTIVE — has a real trap: STILL_ACTIVE is 259, so a process that exited with code 259 reads as alive forever. Core would not exit that way, but the pid sitting in a stale lock file may have been recycled to any program at all.

SYNCHRONIZE is written out rather than imported — windows-sys only exports it under Win32::Storage::FileSystem (typed as FILE_ACCESS_RIGHTS), and pulling that whole feature in for one fixed constant is not worth it.

Failure to open the handle is read through std::io::Error::last_os_error(), the same shape as the unix branch, rather than calling GetLastError directly.

Also

twcore's libc dependency is now unix-only — this was its last user.

Verified

Host: clippy clean workspace-wide, full test suite green. The new tests cover this process being alive, a process that has exited and been reaped not being, and (unix) a process we may not touch still counting as alive. The dead-process test spawns and reaps a real child rather than inventing a pid, since an invented pid may genuinely be in use.

Windows: this file checked against x86_64-pc-windows-msvc and aarch64-pc-windows-msvc via a scratch crate — and the method checked by misspelling a call and confirming it failed, because a cfg'd-out module that "passes" looks exactly like one that was never compiled.

🤖 Generated with Claude Code

Two callers were asking the same question with two copies of the answer,
and on Windows both copies said `true` unconditionally:

  - the single-instance lock would read a pid left behind by a crash,
    believe another instance held it, and refuse to start. Forever.
  - `--parent` would never notice the UI had gone, so quitting the app
    would leave core running and holding the gateway port -- and "it is
    one program" is the whole reason that flag exists.

The two copies also disagreed. The lock counted "exists but is not ours"
as alive; the parent watch counted it as dead. There is now one answer,
and it is the safe one: when we cannot tell, the process is alive. Getting
that backwards deletes a lock another instance is holding, or exits for no
reason.

On Windows the check waits zero milliseconds on the process handle rather
than reading the exit code. A process handle becomes signalled when the
process exits, so "the wait timed out" is exactly "still running". The
more widely shared recipe -- `GetExitCodeProcess` and compare against
`STILL_ACTIVE` -- has a real trap in it: `STILL_ACTIVE` is 259, so a
process that exited with code 259 reads as alive forever. Core would not
exit that way, but the pid in a stale lock file may have been recycled to
any program at all.

`SYNCHRONIZE` is written out rather than imported: windows-sys only
exports it under `Win32::Storage::FileSystem`, and pulling that whole
feature in for one fixed constant is not worth it.

twcore's `libc` dependency is now unix-only, since this was its last user.

The Windows branch was checked against x86_64 and aarch64 MSVC targets by
pulling this file into a scratch crate -- and the method was checked by
misspelling a call and confirming it failed, because a cfg'd-out module
that "passes" looks exactly like one that was never compiled.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the feat/windows-proc-alive branch from 63cca32 to 7b11cae Compare September 23, 2026 05:23
@fylorn
fylorn merged commit 7d8494e into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the feat/windows-proc-alive branch September 23, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant