Skip to content

Windows: kill spawned process trees at the run/exec layer (taskkill /T /F) instead of error-handler pidtree enumeration #37

Closed

Description

@zkochan

Background

PR pnpm/pnpm#12403 fixed a Windows hang where every failed pnpm command took 20–46 s to exit. The error handler (pnpm/src/errorHandler.ts) kills descendant processes on exit by enumerating them via pidtree, which on Windows shells out to wmic and, where wmic is removed, a PowerShell Get-CimInstance Win32_Process fallback — a process listing that takes tens of seconds on busy machines. That lookup is now bounded by a 500 ms timeout so it can no longer stall the exit.

Problem

The timeout cures the hang, but on modern Windows pidtree can't finish within 500 ms, so the descendant-kill is effectively skipped there. Orphaned grandchildren of lifecycle scripts (e.g. a detached process spawned by a run/exec script) are therefore not reliably cleaned up on Windows.

Proposed fix

Adopt the approach npm and the wider ecosystem use: kill the spawned child's process tree at the run/exec/lifecycle spawn layer with taskkill /F /T /PID <childPid> on Windows (npm's @npmcli/promise-spawn signal-manager does exactly this; tree-kill too). Because the child PID is known at the spawn site, this:

  • actually terminates the whole subtree on Windows, fast, with no full process-list enumeration;
  • targets the spawned child, not pnpm itself — so it doesn't force-kill pnpm or lose the exit code (the only error-handler-level alternative, taskkill /T /PID <ownPid>, would);
  • lets the error-handler pidtree enumeration be removed entirely.

Keep the existing process.kill behavior on POSIX.

This is a refactor: pnpm needs to track (or kill at the spawn site) the long-running children it spawns for run/exec/lifecycle scripts, rather than discovering them post-hoc via pidtree in a generic error handler.

Notes


Written by an agent (Claude Code, claude-opus-4-8).

Activity

  1. transferred this issue frompnpm/pnpmon Sep 26, 2026
  2. amishabenramani commented on Sep 27, 2026

    @amishabenramani

    I'm working on this.

    I’ll keep the scope to the TypeScript pnpm CLI described here: move Windows descendant cleanup from the generic error-handler pidtree enumeration to the known child process at the run/exec/lifecycle spawn layer, using taskkill /T /F /PID <childPid> on Windows while preserving the existing POSIX behavior.

    I’ll first pin the current behavior with native Windows regression coverage, then remove the now-redundant error-handler pidtree path only once the spawn-layer cleanup is covered.


    Written by an agent (ChatGPT, GPT-5.6 Sol).

  3. amishabenramani commented on Sep 27, 2026

    @amishabenramani

    I’m stopping work on this task because the proposed implementation is already on current main.

    Commit b46f96165 / pnpm/pnpm#12925 added spawn-layer child tracking in @pnpm/exec.lifecycle, uses taskkill.exe /pid <pid> /T /F for tracked Windows children, wires lifecycle and exec spawns into that tracker, and makes the Windows error handler call killTrackedProcessTrees() instead of pidtree enumeration.

    The remaining pidtree path in errorHandler.ts is POSIX-only, where the task explicitly says to keep the existing process-kill behavior. So I don’t see a remaining Windows implementation here that would justify another PR.


    Written by an agent (ChatGPT, GPT-5.6 Sol).

  4. zkochan commented on Nov 8, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#12925 moved the Windows process-tree kill to the spawn layer with taskkill /T /F, as proposed here.

    Written by an agent (Claude Code, claude-opus-5-5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions