Skip to content

fix(plugins): tie plugin host lifetime to the parent and bound IPC diagnostics - #2381

Merged
vastsa merged 4 commits into
vastsa:mainfrom
koeria:fix/issue-2374-plugin-host-lifetime
Nov 16, 2026
Merged

vastsa merged 4 commits into
vastsa:mainfrom
koeria:fix/issue-2374-plugin-host-lifetime

Conversation

@koeria

@koeria koeria commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Plugin host processes could outlive their parent and recursively log IPC errors (issue #2374):

  • Lifetime: the host had no parent-disconnect exit. A plugin holding a referenced timer kept the Node host alive forever after the parent IPC channel closed. Now: node transports exit on disconnect; Electron utilityProcess's pinned parent-port closes its connector without emitting a JS disconnect, so the host keeps a ping/pong round-trip (PI_PLUGIN_PING_INTERVAL_MS/PI_PLUGIN_PING_MISSES, default 10s x 2) and exits when the parent stops answering. The broker replies to ping with pong. A failed send also marks the transport broken and exits — the host never lingers without its owner.
  • Diagnostics: log and res replies now go through a best-effort trySend; a failing send never throws into the caller, and the uncaughtException/unhandledRejection handlers swallow reporting failures instead of escaping or re-entering recursively. Ordinary exception reporting and plugin responsiveness while the parent is healthy are unchanged.

Test

  • New apps/desktop/test/plugin-host-lifetime.test.mjs: host exits cleanly (code 0) on parent disconnect despite a referenced plugin timer (the issue's repro); host exits when the send channel fails. Both fail on main and pass with the fix.
  • plugin-bus, plugin-command-ownership, plugin-isolation, main-module-path, plugin-agent-extensions, plugin-audio-unsupported, plugin-clipboard-history, plugin-complete, plugin-composer-transform, plugin-provider-oauth: all pass.
  • tsc -p apps/desktop --noEmit: clean. Biome: these paths are repo-ignored.

Fixes #2374

Review follow-up

Async send failures (review ①/②). trySend previously only caught synchronous throws; a dead connector reports delivery failure asynchronously — via the process.send callback or a process-level error event (EPIPE) — so diagnostics sent from inside uncaughtException/unhandledRejection re-entered those handlers recursively (thanks for the fixture; we reproduced the exit-77 loop the same way). The transport now handles delivery at every surface:

  • send passes a delivery callback on the node IPC channel (trySend and the host-API call path both use it); Electron's parent-port gets close / messageerror listeners.
  • A process-level error handler catches async channel failures before they can surface as uncaughtException.
  • All surfaces converge on one idempotent terminal path: transportBroken is set once, one stderr note is emitted, and process.exit(0) runs exactly once (guarded by an exitStarted flag) — later failure events see the flag and stop.

Async regression tests (review ③). Two new cases in test/plugin-host-lifetime.test.mjs, one driving uncaughtException, one unhandledRejection. The fixture plugin overrides process.send so diagnostic log frames succeed synchronously but report EPIPE asynchronously via process.emit("error", ...) (the real Node IPC error boundary), while other frames still reach the real channel. Each test asserts the host exits by itself with code 0 (not SIGKILLed by the harness timeout) and that exactly one "parent IPC channel is broken" note is written — no recursion. Both tests fail (host loops until the 5s harness SIGKILL) against the previous commit and pass with the fix. Full file: node --test test/plugin-host-lifetime.test.mjs → 4 pass.

Electron parent-kill acceptance (review ④). New apps/desktop/test/plugin-host-parent-kill-acceptance.mjs, runnable with node test/plugin-host-parent-kill-acceptance.mjs from apps/desktop (needs the electron devDependency; ELECTRON_BIN overrides the binary). It forks the real host under an Electron utilityProcess with a minimal broker: phase 1 proves healthy-parent responsiveness (every ping answered with pong, round-trips keep flowing past the miss budget, host stays alive, last pong within deadline); phase 2 SIGKILLs the Electron main — no disconnect handshake — and asserts the orphaned host exits by itself within the keepalive budget (observed ~200 ms with a 1 s interval / 2 misses). Negative control: against main (no keepalive) phase 1 times out, as expected.

Note honestly: this PR was developed in a sandbox without a preinstalled Electron; we installed Electron 43 headless (xvfb) there and both phases passed, but we ask the maintainer to run the acceptance script in their normal environment (macOS/Windows included) as the authoritative check — the sandbox run is Linux-only evidence. The exit-0-on-disconnect expectations in the older tests are unchanged (review ⑤: not treated as a defect).

…agnostics

Plugin host processes could outlive their parent: nothing exited the
host when the parent IPC channel went away, so a plugin holding a
referenced timer kept the process alive forever. Diagnostics also went
through the same transport unguarded, so a failing send inside the
exception handlers could re-enter them recursively.

- Exit on lost ownership: node transports exit on `disconnect`; Electron
  utilityProcess's pinned parent-port never surfaces one, so the host
  keeps a ping/pong round-trip (`PI_PLUGIN_PING_INTERVAL_MS` /
  `PI_PLUGIN_PING_MISSES`, 10s / 2 by default) and exits when the parent
  stops answering. The broker replies to `ping` with `pong`. A failed
  send marks the transport broken and exits; nothing re-enters it.
- Guard diagnostics: `log`/reply sends are best-effort (`trySend`) and
  the `uncaughtException` / `unhandledRejection` handlers swallow
  reporting failures instead of escaping or recursing.

Ordinary exception reporting, plugin responsiveness, and concurrent
host API calls are unchanged while the parent is healthy.

Fixes vastsa#2374

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent production-process regression on e875d4f reproduces an incomplete fix: asynchronous Node IPC send failures still recursively log. The fixture overrides process.send so a log send queues process.emit("error", EPIPE), matching the asynchronous error boundary. Both uncaught-exception and unhandled-rejection cases repeatedly attempt log sends and hit the fixture recursion guard (exit 77). trySend only catches synchronous throws; send provides no callback and no transport error handler marks transportBroken before the error reaches uncaughtException. Please handle asynchronous send errors with a callback/transport error listener and one idempotent terminal path, and add both async regression cases. The two new lifetime tests both disconnect the channel and do not exercise this failure. This is a reproduced root-cause gap, so I am leaving this unmerged. The older fixture expects exit 1 whereas this PR chooses exit 0; synchronous exit-code differences are not counted as defects. Native Electron parent-kill acceptance has not been run in this review; please also supply that evidence, including a healthy-parent responsiveness check.

koeria and others added 3 commits November 17, 2026 02:11
Node IPC delivery is asynchronous: when the parent connector dies between
the send and the flush, `process.send` reports the failure through a
callback or a process-level `error` event (EPIPE), not a synchronous
throw. The previous try/catch-only guard never saw those, so diagnostics
sent from inside uncaughtException/unhandledRejection kept re-entering
the handlers recursively (reviewer-reproduced, exit 77).

- trySend/call pass a delivery callback; a failed async delivery joins a
  single idempotent terminal path (transportBroken + one process.exit)
- a process-level `error` handler catches async channel failures before
  they become uncaughtException; Electron's parent-port `close` and
  `messageerror` take the same path
- two regression tests: fixture send that emits EPIPE asynchronously
  under an uncaughtException and an unhandledRejection, asserting one
  clean exit and exactly one broken-channel note
- new parent-kill acceptance script for the Electron utilityProcess
  keepalive, including a healthy-parent ping responsiveness check
…lure

The child-side node IPC channel invokes the process.send callback with
null on a successful flush. The async delivery-failure path treated any
callback invocation as a transport error and rejected the pending call,
so every host->parent call during plugin load (e.g. commands.register)
failed with null and plugin load broke (PLUGIN_LOAD_FAILED in
e2e-plugin-projects). Only a non-null error is now a delivery failure;
the idempotent terminal-exit path is unchanged.

vastsa commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Independent recheck: async exception/rejection delivery failures now terminate rather than reaching the prior recursion guard (the old diagnostic's exit-1 assertions are intentionally not acceptance requirements). Native macOS Electron 43.6.0 parent-kill acceptance PASS: healthy parent answered 5 pings, orphan exited ~201 ms after SIGKILL. Lifetime + bus + command ownership + isolation: 28/28 PASS. Incorporated actual origin/main 3cc8b4a non-destructively, no conflicts, preserved author commits; pushed a6675c2 with maintainer permission. Repeated all 28 tests and native acceptance on that integrated candidate: PASS; diff check clean. Waiting for refreshed CI and actual merge-tree verification before approval/merge. Minor optional harness cleanup: fail() references brokerOutput before initialization if Electron is absent; not a product root-cause blocker.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final landing check: all five current-head CI checks now pass, including the previously pending Rust test job. Latest main remains 3cc8b4a; fetched actual merge candidate and independently validated head share tree ed3753cb21921e15c5d200fa919da3d552ebd304. Prior recorded 28/28 production regressions and native macOS Electron healthy-parent/SIGKILL acceptance apply unchanged to this exact candidate; no redundant rerun claimed. Async delivery failures converge on the guarded terminal path and the reproduced recursive diagnostics/orphan lifecycle defects are resolved. Approved for merge.

@vastsa
vastsa merged commit b5418ce into vastsa:main Nov 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Plugin hosts outlive their parent and recursively log IPC errors

2 participants