pi-mcp-extension 1.14.3 → 1.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -378,7 +378,7 @@ flowchart TD
378
378
  7. **AbortSignal** is wired through to `notifications/cancelled` for clean cancellation
379
379
  8. **Reconnection & recovery** uses a fixed delay schedule with configurable max retries on initial failure, health check failure, or unexpected transport close/error (`client.onclose` / `client.onerror`), while mid-session HTTP `401` / `403 insufficient_scope` invalidates stale tokens and surfaces a re-authorization hint
380
380
  9. **`list_changed` notifications** trigger re-discovery, deactivating stale tools automatically and invalidating the cached resource catalog
381
- 10. **Session shutdown** cancels pending auth/connection flows, stops the callback server, deactivates all tools, flushes the debug log, and cleanly stops all servers (with process-tree `SIGTERM` → `SIGKILL` and pipe cleanup for `stdio` child processes)
381
+ 10. **Session shutdown** cancels pending auth/connection flows, stops the callback server, deactivates all tools, flushes the debug log, and cleanly stops all servers (with process-tree `SIGTERM` → `SIGKILL` — a PID the kernel refuses to signal is left alone instead of being polled — and pipe cleanup for `stdio` child processes)
382
382
 
383
383
  ## MCP Spec Compliance
384
384
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mcp-extension",
3
- "version": "1.14.3",
3
+ "version": "1.14.4",
4
4
  "description": "Full-featured, lightweight MCP extension for the Pi coding agent: stdio, SSE & Streamable HTTP transports, OAuth 2.1 (PKCE + DCR), lazy/eager lifecycles — minimal dependencies, zero bloat.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -450,63 +450,105 @@ function resolveDeps(deps?: Partial<StdioProcessDeps>): StdioProcessDeps {
450
450
  };
451
451
  }
452
452
 
453
- export function isPidAlive(
454
- pid: number,
455
- deps?: Partial<Pick<StdioProcessDeps, "kill">>,
456
- ): boolean {
457
- const kill = deps?.kill ?? defaultDeps.kill;
458
- try {
459
- kill(pid, 0);
460
- return true;
461
- } catch (err) {
462
- if ((err as NodeJS.ErrnoException).code === "ESRCH") {
463
- return false;
464
- }
465
- return true;
466
- }
453
+ /** What one `kill(2)` attempt reported about a PID. */
454
+ type PidSignalOutcome =
455
+ /** The signal was delivered: the PID resolves to a process this caller may signal. */
456
+ | "signalled"
457
+ /** ESRCH: nothing holds this PID any more. */
458
+ | "gone"
459
+ /** EPERM or another platform refusal: the PID is not this caller's to signal. */
460
+ | "denied";
461
+
462
+ /**
463
+ * POSIX liveness: a PID is alive while `kill(pid, 0)` fails with anything but ESRCH.
464
+ *
465
+ * WHY: EPERM means the PID is taken but belongs to a process this caller may not signal (a
466
+ * recycled PID under another uid, a sandboxed child, PID 1). A caller that asks only whether the
467
+ * PID is *occupied* must read that as occupied, not free; teardown asks the stricter
468
+ * `isPidSignalable` instead. Auth-lock's staleness check needs this reading too and keeps its own
469
+ * probe (`defaultIsPidAlive` in `src/auth-lock.ts`), so it does not call this one.
470
+ */
471
+ export function isPidAlive(pid: number, deps?: Partial<Pick<StdioProcessDeps, "kill">>): boolean {
472
+ return signalPid(pid, 0, resolveDeps(deps)) !== "gone";
467
473
  }
468
474
 
469
- /** Send `signal` to one PID; `false` when the PID was already gone (ESRCH). */
470
- function signalPid(pid: number, signal: NodeJS.Signals, d: StdioProcessDeps): boolean {
475
+ function signalPid(
476
+ pid: number,
477
+ signal: NodeJS.Signals | number,
478
+ d: StdioProcessDeps,
479
+ ): PidSignalOutcome {
471
480
  try {
472
481
  d.kill(pid, signal);
473
- return true;
482
+ return "signalled";
474
483
  } catch (err) {
475
484
  if ((err as NodeJS.ErrnoException).code === "ESRCH") {
476
- return false;
485
+ return "gone";
477
486
  }
478
487
  // WHY: EPERM and platform refusals mean this PID cannot be signalled (a recycled PID
479
488
  // or a sandboxed process). Teardown still has to signal the rest of the tree within
480
489
  // the bounded wait instead of rejecting the caller's shutdown path.
481
- return true;
490
+ return "denied";
482
491
  }
483
492
  }
484
493
 
494
+ /**
495
+ * A PID this teardown can still act on: alive *and* signalable.
496
+ *
497
+ * WHY: a PID the caller cannot signal is not one a bounded wait can make progress on. `EPERM`
498
+ * means no signal of ours reaches that PID — the child has exited and the PID was recycled to a
499
+ * process under another uid, the child changed credentials (a wrapper that `setuid`s), or a
500
+ * policy layer refuses the signal — so polling it re-reads the same `EPERM` every 25ms until
501
+ * the timeout, and escalating to SIGKILL only buys a second refused signal.
502
+ *
503
+ * This answers "can we still reach it", never "is it our child": a PID recycled to another
504
+ * process of the *same* uid stays signalable and is signalled, exactly as before this guard
505
+ * existed. Identity would need the child's start time compared against `/proc/<pid>/stat`, and
506
+ * the `ChildProcess` handle is gone by the time teardown runs.
507
+ */
508
+ function isPidSignalable(pid: number, d: StdioProcessDeps): boolean {
509
+ return signalPid(pid, 0, d) === "signalled";
510
+ }
511
+
485
512
  function allExited(pids: readonly number[], d: StdioProcessDeps): boolean {
486
- return pids.every((pid) => !isPidAlive(pid, d));
513
+ return pids.every((pid) => !isPidSignalable(pid, d));
487
514
  }
488
515
 
516
+ /**
517
+ * Send `signal` to the PIDs that accept it, then wait only for those to exit.
518
+ *
519
+ * Resolves `true` when nothing that could still be running is left, which is what gates the
520
+ * SIGKILL escalation. A PID that reported ESRCH is gone, and one that reported EPERM is not
521
+ * ours to wait for — but it is not assumed dead either.
522
+ */
489
523
  async function signalAndWaitForExit(
490
524
  pids: readonly number[],
491
525
  signal: NodeJS.Signals,
492
526
  timeoutMs: number,
493
527
  d: StdioProcessDeps,
494
528
  ): Promise<boolean> {
495
- const attempted = pids.map((pid) => signalPid(pid, signal, d));
496
- // Every target reported ESRCH: the tree is already gone, so there is nothing to wait for.
497
- if (!attempted.some(Boolean)) {
498
- return true;
499
- }
500
-
501
- const deadline = d.now() + timeoutMs;
502
- while (d.now() < deadline) {
503
- if (allExited(pids, d)) {
504
- return true;
529
+ const attempts = pids.map((pid) => ({ pid, outcome: signalPid(pid, signal, d) }));
530
+ const signalled = attempts.filter((a) => a.outcome === "signalled").map((a) => a.pid);
531
+ const refused = attempts.filter((a) => a.outcome === "denied").map((a) => a.pid);
532
+
533
+ // A window can only be ended by a signal that was actually delivered, so nothing is waited
534
+ // for when nothing was. (The refused targets still decide the result below.)
535
+ if (signalled.length > 0) {
536
+ const deadline = d.now() + timeoutMs;
537
+ while (d.now() < deadline && !allExited(signalled, d)) {
538
+ await d.sleep(25);
505
539
  }
506
- await d.sleep(25);
507
540
  }
508
541
 
509
- return allExited(pids, d);
542
+ // WHY: a refused signal is not proof of a dead target. Permission is mediated per signal on
543
+ // some systems — SELinux keeps separate `sigterm`/`sigkill` permissions, AppArmor matches a
544
+ // signal set, and a seccomp filter can match the signal argument — so SIGTERM can be refused
545
+ // while SIGKILL is allowed. A refused target that a liveness probe still finds signalable
546
+ // therefore keeps the tree alive and sends the caller straight to the SIGKILL escalation,
547
+ // without paying for a wait window this signal can never end; one that is no longer
548
+ // signalable is out of reach (recycled to another uid, or the child changed credentials) and
549
+ // counts as gone. ESRCH targets are never re-probed: that verdict is final, and re-probing a
550
+ // recycled PID could sign it up for a SIGKILL it has nothing to do with.
551
+ return allExited(signalled, d) && !refused.some((pid) => isPidSignalable(pid, d));
510
552
  }
511
553
 
512
554
  /**
@@ -523,9 +565,9 @@ async function escalateToSigkill(
523
565
  ): Promise<void> {
524
566
  const rescanned = await d.listDescendantPids(rootPid);
525
567
  const survivors = [...new Set([...rescanned, ...descendants])].filter((candidate) =>
526
- isPidAlive(candidate, d),
568
+ isPidSignalable(candidate, d),
527
569
  );
528
- const targets = [...survivors, rootPid].filter((candidate) => isPidAlive(candidate, d));
570
+ const targets = [...survivors, rootPid].filter((candidate) => isPidSignalable(candidate, d));
529
571
  if (targets.length === 0) return;
530
572
  await signalAndWaitForExit(targets, "SIGKILL", 200, d);
531
573
  }
@@ -535,16 +577,25 @@ export async function ensurePidTerminated(
535
577
  deps?: Partial<StdioProcessDeps>,
536
578
  ): Promise<void> {
537
579
  const d = resolveDeps(deps);
538
- if (!pid || !isPidAlive(pid, d)) {
539
- return;
540
- }
580
+ if (!pid) return;
581
+ const root = signalPid(pid, 0, d);
582
+ // WHY: ESRCH is the only verdict that says the PID is free — nothing to snapshot, nothing to
583
+ // signal. EPERM does not describe the tree: the root may be out of reach while descendants
584
+ // are ours (a privileged wrapper that dropped to our uid for the real server), so they are
585
+ // still signalled below.
586
+ if (root === "gone") return;
541
587
 
542
588
  // WHY: Snapshot before signalling any PID: a wrapper that exits first re-parents its
543
- // children to init, after which they can no longer be found by parent PID.
589
+ // children to init, after which they can no longer be found by parent PID. The snapshot runs
590
+ // even for an out-of-reach root, because that root may still own signalable descendants.
544
591
  const descendants = await d.listDescendantPids(pid);
545
- if (await signalAndWaitForExit([...descendants, pid], "SIGTERM", 500, d)) {
546
- return;
547
- }
592
+ const targets = descendants.filter((candidate) => isPidSignalable(candidate, d));
593
+ // WHY: a root nobody can signal is skipped as a target — no wait window can end it — but it
594
+ // must not hide the descendants that a signal can still reach.
595
+ if (root === "signalled") targets.push(pid);
596
+ if (targets.length === 0) return;
597
+
598
+ if (await signalAndWaitForExit(targets, "SIGTERM", 500, d)) return;
548
599
 
549
600
  await escalateToSigkill(pid, descendants, d);
550
601
  }