Save backend data on quit and stop it when Electron exits#11

merged1
main
hoplite/knosos-22e338ca
opened21d agobyUusehopliteBot
View on GitHubTwo-way sync enabledPR last imported 21d ago

Problem

  • Lost data on quit (Windows): the Go backend batches saves every 15s and only flushes on SIGTERM. On Windows, child.kill() is a hard terminate, so up to 15s of samples were dropped on every Quit. On macOS/Linux Electron didn't wait for the flush either.
  • Orphaned backend: if Electron crashed or was force-killed, nothing told the backend to stop; it kept running and holding the data file.

Fix

  • Backend: new --exit-with-stdin flag. When stdin closes it shuts down the HTTP server (letting in-flight /record calls finish), flushes, then exits. The OS closes the pipe when the parent dies, so this works on every platform without signals.
  • Electron (backend-client.js): spawns the backend with a stdin pipe and the flag; stopBackend() now ends stdin and resolves when the process exits, killing it after 3s as a fallback.
  • main.js: before-quit holds the quit until queued samples are sent and the backend has exited, then quits again.
  • scripts/lifecycle-test.js: covers the deferred quit in backend mode.

Verification

  • go vet, go test ./..., npm test, node scripts/lifecycle-test.js pass.
  • End-to-end against the built binary via backend-client.js: graceful stop saved the recorded sample and exited in ~4ms; SIGKILLing the Node parent made the backend save and exit on its own.
  • Not tested on a real Windows machine or through the packaged app / update-install flow (quit is delayed by milliseconds, so the installer should be unaffected).

Devin Review

Checks

1 cancelled ยท 1 successful
Some checks were not successful

Cancelled1

9c5cfbd
Synced from GitHub

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 36a0af98-c024-4c85-b4a2-bce8ca79eb45

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix ยท Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

electron/backend-client.jsline 156Outdated ยท 67bf604Open thread
……@@ -147,9 +149,15 @@ function startBackend({ root, dataFile, frontendDir }) {
153+function stopBackend(child, timeoutMs = 3000) {
154+ if (!child || child.exitCode !== null || child.signalCode !== null) return Promise.resolve();
155+ return new Promise((resolve) => {
156+ const timer = setTimeout(() => { child.kill(); resolve(); }, timeoutMs);
Ddevin-ai-integrationBoton line 15621d ago

๐Ÿ”ด Shutdown timeout can discard saved usage

When an HTTP handler lasts three seconds, stopBackend() kills the backend before its final flush. The backend also allows three seconds for Shutdown, so Windows quits can lose buffered samples.

Learn more

The backend waits up to three seconds for active HTTP requests before calling Flush. The parent starts its own three-second timer as soon as it closes stdin. With a request still active at that deadline, the parent kills the process while it is still shutting down, before it has flushed the 15-second save batch. On Windows that kill immediately terminates the backend; resolving the promise without waiting for exit also breaks the stated shutdown contract.

Example: A snapshot request remains open for 3.1 seconds when the user quits, while recorded usage is dirty. The Node timer fires at three seconds, killing the backend before it reaches data.Flush(), and that usage is absent on restart.

Recommended fix: Make the parent deadline longer than the backend HTTP shutdown timeout plus sufficient time for writing the data file. On timeout, await the child's actual exit and expose whether shutdown failed rather than claiming a successful saved exit.

Devin Review


Was this helpful? React with ๐Ÿ‘ or ๐Ÿ‘Ž to provide feedback.

electron/main.jslines 462โ€“463Outdated ยท 67bf604Open thread
……@@ -447,11 +447,24 @@ if (!app.requestSingleInstanceLock()) {
460+ event.preventDefault();
461+ backendStopping = true;
462+ Promise.resolve(store.pendingRecords).catch(() => {})
463+ .then(() => stopBackend(child))
Ddevin-ai-integrationBoton lines 462โ€“46321d ago

๐Ÿ”ด In-flight browser samples lost on quit

When quitting during browser lookup, pendingRecords excludes the in-flight tick() and the backend can close before it records. tracker.stop() cancels future ticks but does not wait for the current one, losing its sample.

Learn more

The tracker can remain inside an asynchronous browser-history lookup when before-quit fires. stop clears its timer but does not await a running tick. That tick records after the lookup resolves. The quit handler snapshots store.pendingRecords before this new record enters the queue, closes backend stdin, and allows the process to exit. The late request then fails or never runs.

Example: A Chrome sample is awaiting history lookup when the user clicks Quit. The queue is empty, so shutdown starts immediately. The lookup completes afterward and the sample is never saved, despite the new quit wait.

Recommended fix: Track the active tick promise in Tracker and await it after stopping the scheduler, then await the final store.pendingRecords before calling stopBackend. Do not allow a new tick after the shutdown wait begins.

Devin Review


Was this helpful? React with ๐Ÿ‘ or ๐Ÿ‘Ž to provide feedback.

Added 1 commit

  • Save backend data on quit and stop it when Electron exits9c5cfbd
electron/main.jslines 460โ€“467Open thread
……@@ -447,11 +447,24 @@ if (!app.requestSingleInstanceLock()) {
464+ .finally(() => {
465+ backendRuntime = null;
466+ backendStopping = false;
467+ app.quit();
Ddevin-ai-integrationBoton lines 460โ€“46721d ago

๐Ÿ” Check update installation after deferred quit

The updater calls quitAndInstall, but this listener cancels its first quit and later calls app.quit. Verify that the packaged installer still runs on Windows and AppImage.

Devin Review


Was this helpful? React with ๐Ÿ‘ or ๐Ÿ‘Ž to provide feedback.