windows code to detect if a process is still alive

Martin Pool mbp at canonical.com
Thu Apr 14 05:49:30 UTC 2011


On 14 April 2011 15:24, Eli Zaretskii <eliz at gnu.org> wrote:
>> From: Martin Pool <mbp at canonical.com>
>> Date: Thu, 14 Apr 2011 11:26:49 +1000
>>
>> For detection of stale lock files, I want to detect whether a given
>> process id on the local machine still corresponds to a live process.
>> On unix, kill(pid, 0) will do this.  What's the best way to do this
>> from inside Python on Windows?
>
> In general, on Windows you need to get the snapshot of running
> processes, then walk it to look for your process.
>
> But you cannot be sure that a process ID will not be recycled, and
> another process will not use it in the meantime.  On Windows, process
> IDs tend to be recycled much faster than on Unix, in my experience.
>
> So if you want a reasonably safe way of detecting stale locks, I
> suggest that you tell more about the context: what is the algorithm
> for detecting stale locks, when will it run (wrt the time that the
> presumably dead process was run), do we know what executable ran in
> that process, etc.

Basically this just fires when we try to take a lock and discover that
it's held by someone else.  The lock info file records the username,
hostname, processid and date taken.  If the hostname is the same as
our local host, and the process is known not to be alive, it may be
reasonable to assume the lock is now stale.  Having the pid be reused
by someone else is fairly harmless.

It's a lot like the lock and backup-file recovery algorithm in vim and emacs.


Martin



More information about the bazaar mailing list