fix(host-service): stop the Linux file watcher from taking the host down, and make superset start recover

Merged
KI
2mo ago
+1057-134

Summary

A customer's standalone host service (CLI 1.29.0, Linux) died with SIGSEGV inside @parcel/watcher (InotifyBackend::~InotifyBackend <- removeShared <- Backend::handleError), and superset start kept running with no worker, so systemd never restarted it.

Root cause, reproduced on Linux: parcel's inotify backend runs its own thread around poll(). Any signal landing on that thread (SIGCHLD from a git child is enough) makes poll() return EINTR. Parcel treats that as fatal and destroys the backend from inside the backend's own thread, which ends in a deadlocked event loop or a SIGSEGV. Under the same load parcel also corrupts V8 global handles. Parcel 2.6.0 has identical code, so an upgrade does not help.

Changes

  • workspace-fs: FsWatcherManager now talks to a NativeWatchBackend interface. Parcel moves behind it unchanged and is loaded lazily. Linux defaults to a chokidar backend (Node's own fs.watch, events on the event loop, no watcher thread, no native state shared between subscriptions). macOS keeps parcel, whose FSEvents backend has no poll loop. Coalescing, rename reconciliation, throttling, root-deletion recovery and search-index patching are unchanged and shared.
  • host-service: WatchAttachGuard serializes attaches per root and backs off a root whose native attach failed, instead of GitWatcher re-attaching it every 30s. A failed fs:watch stream no longer leaves a dead subscription that blocks re-watching. This is a mitigation, not the fix for the crash.
  • cli: foreground superset start exits 1 when the host exits on its own, and also when it stops answering health.check (6 misses at 10s), after SIGKILLing it. A wedged host stays alive as a process, so watching for exit alone is not enough.

Testing

Real host-service built on Linux from the commit before this branch and from this branch, run for 15 minutes each against a fake cloud with 4 git workspaces, 8 websocket clients on git:watch/fs:watch, a subdirectory created and deleted in a loop, continuous edits and git.getStatus calls:

BuildRunsHungRuns logging Unable to poll: Interrupted system callfs events delivered
before61 (at 186s, main thread in ~InotifyBackend)3 of the 5 that finished (10-12 times each)0 to 358k
guard only101 (4 times)180k
this branch500246k-442k

Also: a library-level rig produced a SIGSEGV, V8 IsInUse fatals and deadlocks with parcel, none with parcel not loaded, none with chokidar. Attach cost through the manager on Linux is on par (100k files: 6.1s vs 5.0s, less memory). workspace-fs suite on Linux with CI's bun 1.3.14: 144 pass, 2 skipped (two inotify races parcel loses, skipped only for parcel on Linux). The supervisor was checked end to end with a fake host that segfaults and one that spins forever.

Not covered: the real-service runs are arm64 only (amd64 evidence is from the library rig under emulation); no run on a customer-sized repo on real x64 hardware; the desktop app was typechecked but not built.

Follow-ups

  • The Linux parcel binaries still ship in the CLI bundle, now unused.
  • An upstream parcel fix (retry poll() on EINTR, do not destroy the backend on its own thread) would let macOS and Linux share one backend again.

Summary by CodeRabbit

  • New Features

    • Added host liveness monitoring to detect unexpected exits and unresponsive hosts, with clearer restart guidance.
    • Added cross-platform filesystem watching with improved handling for file creation, updates, deletions, ignored paths, and symlinks.
    • Added watcher retry backoff to prevent repeated failures from overwhelming the system.
  • Bug Fixes

    • Failed filesystem watches now clean up correctly and can be re-established.
    • Improved recovery when watched directories are recreated or temporarily unavailable.
    • Enhanced detection and reporting of host shutdowns and startup failures.

Summary by cubic

Fixes the Linux host-service crash where @parcel/watcher's inotify backend destroyed itself from its own poll() thread after EINTR, deadlocking or SIGSEGVing the process. Linux now watches through chokidar on fs.watch, and foreground superset start exits 1 when the host process exits or stops answering health checks so a supervisor can restart it.

What changed

  • FsWatcherManager now talks to a NativeWatchBackend; @parcel/watcher stays on macOS and is lazy-loaded so Linux never maps its native addon.
  • Watch attaches are serialized per root, and roots whose native attach fails back off exponentially instead of being retried every 30s; failed fs:watch streams are removed so clients can re-watch.
  • superset start SIGKILLs a wedged host (a stuck event loop never runs a SIGTERM handler), clears the manifest, and exits 1 after 6 consecutive missed health checks.

Not covered

  • Real-service verification ran on arm64; amd64 evidence is from an emulated library-level rig.
  • The Linux @parcel/watcher binaries remain in the CLI bundle but are unused.