Repository navigation
Answer whether a pid is alive, on Windows and in one place - #117
Merged
Merged
Conversation
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
force-pushed
the
feat/windows-proc-alive
branch
from
September 23, 2026 05:23
63cca32 to
7b11cae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two callers were asking the same question with two copies of the answer, and on Windows both copies said
trueunconditionally:lockfile.rs)--parent(main.rs)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 againstSTILL_ACTIVE— has a real trap:STILL_ACTIVEis 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.SYNCHRONIZEis written out rather than imported — windows-sys only exports it underWin32::Storage::FileSystem(typed asFILE_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 callingGetLastErrordirectly.Also
twcore's
libcdependency 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-msvcandaarch64-pc-windows-msvcvia 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