Skip to content

children() / parent() PID-reuse check may be incorrect #2910

Description

@giampaolo

Both methods detect PID reuse by comparing ctime (create_time()) values. And ctime is bound to PID identity / PID reuse. ctime is something we already decided not to trust blindly:

_get_ident() sets ctime to None on:

  • FreeBSD/OpenBSD, because it's subject to clock updates (so it's unreliable)
  • SunOS/AIX, because we don't know if that can happen
  • Windows, for high privileged processes
  • Also note that on some platforms (NetBSD, OpenBSD) ctime can be 0 for zombie processes.

In these 4 cases it means PID reuse detection is effectively disabled for things like kill(), is_running(), etc., and this matters for both children() and parent(), because right now they rely on the ctime logic without considering this:

def children():
    ...
    child = Process(pid)
    # if child happens to be older than its parent
    # (self) it means child's PID has been reused
    if proc_ctime <= child.create_time():
        ret.append(child)
def parent():
    ...
    # Get a fresh (non-cached) ctime in case the system clock
    # was updated. TODO: use a monotonic ctime on platforms
    # where it's supported.
    proc_ctime = Process(self.pid).create_time()
    try:
        parent = Process(ppid)
        if parent.create_time() <= proc_ctime:
            return parent
        # ...else ppid has been reused by another process
    except NoSuchProcess:
        pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions