[PR #7262] [MERGED] fix: keep client extensions alive in background tabs (#7260) #7649

Closed
opened 2026-10-24 04:06:11 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7262
Author: @SpicyMarinara
Created: 10/7/2026
Status: ✅ Merged
Merged: 10/7/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/extension-watchdog-background-7260


📝 Commits (5)

  • 2060f26 fix: keep client extensions alive in background tabs
  • 64fb8af fix: start client extensions reliably in Safari and test Restart
  • 2f8f39a fix: keep other stopped extensions stopped on Restart
  • b79798b fix: forget stopped extensions when the injector unmounts
  • 6cc4b83 fix: dismiss a stopped notice once the extension is no longer stopped

📊 Changes

7 files changed (+265 additions, -7 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/development/personal-extensions.md (+1 -1)
➕ e2e/personal-extension-restart.e2e.ts (+113 -0)
📝 packages/client/src/components/layout/PersonalExtensionInjector.tsx (+41 -2)
📝 packages/client/src/localization/locales/en.json (+3 -0)
📝 packages/server/src/routes/personal-extensions.routes.ts (+15 -4)
📝 scripts/regressions/extension-security.regression.ts (+91 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7260

Reported on Discord by spacyH (Safari 27 on macOS), who also traced the watchdog code and suggested the fix direction. Thank you!

Why this change

Client extensions stopped working whenever you left the Marinara tab for about 10 seconds. The extension's panel and top-bar buttons stayed on screen, but every click did nothing, and nothing told you why.

The cause is the sandbox's heartbeat watchdog (served by GET /api/personal-extensions/:id/sandbox.html). The extension worker sends a heartbeat every second, and the watchdog stopped the worker after 5 seconds without one. Browsers throttle or pause timers in hidden tabs, during sleep and in suspended mobile apps, so the heartbeat stalls through no fault of the worker. When the watchdog then ran (while hidden, or as soon as you came back), it stopped a healthy worker. The host never heard that the worker was gone (it only logged to the console), so the dead controls stayed. The developer docs already promised that "the host removes all contributions when the worker is stopped"; that was not true for watchdog stops.

What changed

  • Watchdog (packages/server/src/routes/personal-extensions.routes.ts, sandbox bootstrap): each tick now restarts the 5-second silence window when the page is hidden (document.hidden, which a nested iframe takes from the top-level tab) or when the tick itself arrives more than 2 seconds after the previous one. A late tick proves this page's own timers were held back (background throttling, sleep, a frozen page, a long main-thread stall), so a missing heartbeat is expected. A worker that hangs on a visible page is still stopped after the same 5 seconds.
  • Stopped state is now visible: when the sandbox stops a worker (watchdog or message limit), it marks the error with stopped: true. The host (PersonalExtensionInjector.tsx) then removes that extension's buttons, menu items and panels, and shows a notice, "{name} stopped working. Its buttons and panels are hidden until you restart it.", with a Restart button. Restart re-runs the normal start-up for that extension only, if it is still enabled and approved. Other stopped extensions stay stopped until you restart them, their code changes, they are turned off and on again, or the page reloads. Ordinary extension errors (a thrown error in a handler, a failed start-up promise) do not set the flag and behave as before.
  • Safari start-up (same sandbox bootstrap): the sandbox revoked the worker's blob URL right after new Worker(). WebKit loads the worker script after the constructor returns, so the load failed (WebKitBlobResource error 1, then "Browser extension worker failed"). The extension never started, and its top-bar button never appeared. The URL is now kept for the sandbox's lifetime and released when the iframe is removed. This is the "top-bar button missing after a fresh load" bug from the issue (see below).
  • English copy in en.json only (3 keys under extensions.runtime.*).
  • Regression in scripts/regressions/extension-security.regression.ts: runs the served sandbox bootstrap in a node:vm context with a fake clock, fake document.hidden and a fake Worker, then checks: a hidden tab with stalled heartbeats is not stopped (30 s of 1 Hz ticks plus a 60 s throttled gap), coming back gives a fresh window, a 120 s late tick on a visible page is not a hang, and a worker silent for over 5 s on a visible page is stopped and reported with stopped: true. It also checks that the worker blob URL is not revoked during start-up. On origin/staging it fails with A hidden page must not stop a worker whose heartbeat is throttled. With only the early revoke put back, it fails with The worker blob URL must outlive the Worker constructor call.
  • Host e2e e2e/personal-extension-restart.e2e.ts (desktop-chromium): runs the real PersonalExtensionInjector with a fixture sandbox. An ordinary error keeps the button. A stopped: true error removes the button and the iframe and shows the notice. Restart then creates a fresh iframe for the same extension and brings the button back, while a second stopped extension stays stopped. It fails with a no-op Restart handler, without restartRequest in the effect deps, without the stopped-extension check in setup, and with the staging injector.
  • docs/development/personal-extensions.md: two sentences on the new watchdog behaviour. Translations are tracked in #7261.
  • CHANGELOG entry under [Unreleased].

Top-bar button missing after a fresh load (the minor bug in the issue)

Root cause found and fixed here: the early URL.revokeObjectURL(workerUrl) described above. In Playwright's WebKit, against a real approved client extension, the top-bar button was missing on 8 of 8 fresh loads before the change and present on 8 of 8 after it, about 0.5 s after load. Chromium resolves blob URLs when the Worker is created, so it was never affected (5 of 5 before and after). Real Safari probably wins the race sometimes, which fits "often missing". Before this PR, the watchdog then stopped the worker that never started, and nothing told the user.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Agent-run checks (not a substitute for the boxes above): pnpm check passed; pnpm regression:extensions-security passed and fails on the origin/staging route as described above; e2e/personal-extension-restart.e2e.ts and e2e/topbar-accessibility.e2e.ts passed on desktop-chromium. Local CodeRabbit: 0 findings on the first commit. Later runs found two things, both fixed: Restart also restarted other stopped extensions (2f8f39af7), and the stopped list should be cleared when the injector unmounts (b79798b01). The GitHub CodeRabbit thread about the source-text-only Restart check was fixed with the e2e above (64fb8af24).

Local real-browser probe (temporary spec, not committed) with a real approved client extension and the real server sandbox, desktop Chromium:

  • Busy-looping the worker for 8 s while the sandbox reported document.hidden: with this PR the button still worked afterwards ("Probe 4"); with the staging watchdog the worker was stopped, the button stayed on screen, and clicking it did nothing (title stuck at "Probe 3"). This is the reported symptom.
  • Busy-looping the worker for 10 s on a visible page: it was stopped, the button disappeared, the notice appeared, and Restart brought back a working button.
  • Freezing the whole page for 9 s (CDP Page.setWebLifecycleState) or stalling the sandbox thread for 7 s did not stop the worker, before or after. Chromium delivers the queued heartbeats before the overdue timer, which is why Chromium users rarely hit this.

Manual verification notes

  • Manually verify in Safari on macOS: enable a client extension with a top-bar button or panel, check that it responds, switch to another tab for at least 30 seconds, come back, and check that the button and panel controls still respond.
  • Manually verify in Safari: reload the app at least 5 times and check that the extension's top-bar button appears every time, without switching tabs or toggling the extension.
  • Manually verify the same in Chrome or Firefox, including leaving the tab hidden for more than 5 minutes (Chrome's heavier background throttling).
  • Manually verify sleep: with the extension open, put the Mac to sleep for a minute, wake it, and check the controls still respond.
  • Manually verify on iOS Safari or the Android app: switch to another app for a minute, come back, and check the controls still respond.
  • Manually verify that a hung extension is still stopped: enable a test client extension whose button handler runs while (true) {}, click it on a visible tab, and check that within about 6 seconds its buttons and panel disappear and the notice "{name} stopped working." appears with Restart.
  • Manually verify that Restart brings the extension's buttons back, and that the notice uses theme colours in light and dark mode.
  • Manually verify that an extension that throws an ordinary error (for example throw new Error("x") inside a button handler) keeps its buttons and does not show the stopped notice.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

Docs: docs/development/personal-extensions.md and CHANGELOG.md updated; [docs-i18n] follow-up is #7261.

UI evidence (if applicable)

The only new UI is a standard Sonner notice with a Restart action, styled by the app's existing toast theme. No screenshot yet; it appears only after the sandbox stops a hung extension.

Summary by CodeRabbit

  • Bug Fixes

    • Client extensions remain active when switching tabs, when the browser is in the background, or while a device sleeps.
    • Safari extensions start on each page load, helping keep their buttons available in the top bar.
  • New Features

    • When an extension stops, its buttons and panels are hidden and a notice offers a Restart option. Restarting restores the extension.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/7262 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/7/2026 **Status:** ✅ Merged **Merged:** 10/7/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/extension-watchdog-background-7260` --- ### 📝 Commits (5) - [`2060f26`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2060f266d3dc9b2d403ce255a3b535caabefa8b3) fix: keep client extensions alive in background tabs - [`64fb8af`](https://github.com/Pasta-Devs/Marinara-Engine/commit/64fb8af24ccfaaa68b85661d853eaf888fae4aba) fix: start client extensions reliably in Safari and test Restart - [`2f8f39a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2f8f39af7bc9a00b5943027df705acc64c43a231) fix: keep other stopped extensions stopped on Restart - [`b79798b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b79798b0110a5a5e0e0eb334a2826018cd96da21) fix: forget stopped extensions when the injector unmounts - [`6cc4b83`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6cc4b83968ea27e39d07c65965848a066093af0a) fix: dismiss a stopped notice once the extension is no longer stopped ### 📊 Changes **7 files changed** (+265 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/development/personal-extensions.md` (+1 -1) ➕ `e2e/personal-extension-restart.e2e.ts` (+113 -0) 📝 `packages/client/src/components/layout/PersonalExtensionInjector.tsx` (+41 -2) 📝 `packages/client/src/localization/locales/en.json` (+3 -0) 📝 `packages/server/src/routes/personal-extensions.routes.ts` (+15 -4) 📝 `scripts/regressions/extension-security.regression.ts` (+91 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7260 Reported on Discord by **spacyH** (Safari 27 on macOS), who also traced the watchdog code and suggested the fix direction. Thank you! ## Why this change Client extensions stopped working whenever you left the Marinara tab for about 10 seconds. The extension's panel and top-bar buttons stayed on screen, but every click did nothing, and nothing told you why. The cause is the sandbox's heartbeat watchdog (served by `GET /api/personal-extensions/:id/sandbox.html`). The extension worker sends a heartbeat every second, and the watchdog stopped the worker after 5 seconds without one. Browsers throttle or pause timers in hidden tabs, during sleep and in suspended mobile apps, so the heartbeat stalls through no fault of the worker. When the watchdog then ran (while hidden, or as soon as you came back), it stopped a healthy worker. The host never heard that the worker was gone (it only logged to the console), so the dead controls stayed. The developer docs already promised that "the host removes all contributions when the worker is stopped"; that was not true for watchdog stops. ## What changed - **Watchdog** (`packages/server/src/routes/personal-extensions.routes.ts`, sandbox bootstrap): each tick now restarts the 5-second silence window when the page is hidden (`document.hidden`, which a nested iframe takes from the top-level tab) or when the tick itself arrives more than 2 seconds after the previous one. A late tick proves this page's own timers were held back (background throttling, sleep, a frozen page, a long main-thread stall), so a missing heartbeat is expected. A worker that hangs on a visible page is still stopped after the same 5 seconds. - **Stopped state is now visible**: when the sandbox stops a worker (watchdog or message limit), it marks the error with `stopped: true`. The host (`PersonalExtensionInjector.tsx`) then removes that extension's buttons, menu items and panels, and shows a notice, "{name} stopped working. Its buttons and panels are hidden until you restart it.", with a **Restart** button. Restart re-runs the normal start-up for that extension only, if it is still enabled and approved. Other stopped extensions stay stopped until you restart them, their code changes, they are turned off and on again, or the page reloads. Ordinary extension errors (a thrown error in a handler, a failed start-up promise) do not set the flag and behave as before. - **Safari start-up** (same sandbox bootstrap): the sandbox revoked the worker's blob URL right after `new Worker()`. WebKit loads the worker script after the constructor returns, so the load failed (`WebKitBlobResource error 1`, then "Browser extension worker failed"). The extension never started, and its top-bar button never appeared. The URL is now kept for the sandbox's lifetime and released when the iframe is removed. This is the "top-bar button missing after a fresh load" bug from the issue (see below). - English copy in `en.json` only (3 keys under `extensions.runtime.*`). - **Regression** in `scripts/regressions/extension-security.regression.ts`: runs the served sandbox bootstrap in a `node:vm` context with a fake clock, fake `document.hidden` and a fake `Worker`, then checks: a hidden tab with stalled heartbeats is not stopped (30 s of 1 Hz ticks plus a 60 s throttled gap), coming back gives a fresh window, a 120 s late tick on a visible page is not a hang, and a worker silent for over 5 s on a visible page is stopped and reported with `stopped: true`. It also checks that the worker blob URL is not revoked during start-up. On `origin/staging` it fails with `A hidden page must not stop a worker whose heartbeat is throttled`. With only the early revoke put back, it fails with `The worker blob URL must outlive the Worker constructor call`. - **Host e2e** `e2e/personal-extension-restart.e2e.ts` (desktop-chromium): runs the real `PersonalExtensionInjector` with a fixture sandbox. An ordinary error keeps the button. A `stopped: true` error removes the button and the iframe and shows the notice. **Restart** then creates a fresh iframe for the same extension and brings the button back, while a second stopped extension stays stopped. It fails with a no-op Restart handler, without `restartRequest` in the effect deps, without the stopped-extension check in setup, and with the staging injector. - `docs/development/personal-extensions.md`: two sentences on the new watchdog behaviour. Translations are tracked in #7261. - CHANGELOG entry under `[Unreleased]`. ### Top-bar button missing after a fresh load (the minor bug in the issue) Root cause found and fixed here: the early `URL.revokeObjectURL(workerUrl)` described above. In Playwright's WebKit, against a real approved client extension, the top-bar button was missing on 8 of 8 fresh loads before the change and present on 8 of 8 after it, about 0.5 s after load. Chromium resolves blob URLs when the Worker is created, so it was never affected (5 of 5 before and after). Real Safari probably wins the race sometimes, which fits "often missing". Before this PR, the watchdog then stopped the worker that never started, and nothing told the user. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Agent-run checks (not a substitute for the boxes above): `pnpm check` passed; `pnpm regression:extensions-security` passed and fails on the `origin/staging` route as described above; `e2e/personal-extension-restart.e2e.ts` and `e2e/topbar-accessibility.e2e.ts` passed on desktop-chromium. Local CodeRabbit: 0 findings on the first commit. Later runs found two things, both fixed: Restart also restarted other stopped extensions (2f8f39af7), and the stopped list should be cleared when the injector unmounts (b79798b01). The GitHub CodeRabbit thread about the source-text-only Restart check was fixed with the e2e above (64fb8af24). Local real-browser probe (temporary spec, not committed) with a real approved client extension and the real server sandbox, desktop Chromium: - Busy-looping the worker for 8 s while the sandbox reported `document.hidden`: with this PR the button still worked afterwards ("Probe 4"); with the staging watchdog the worker was stopped, the button stayed on screen, and clicking it did nothing (title stuck at "Probe 3"). This is the reported symptom. - Busy-looping the worker for 10 s on a visible page: it was stopped, the button disappeared, the notice appeared, and **Restart** brought back a working button. - Freezing the whole page for 9 s (CDP `Page.setWebLifecycleState`) or stalling the sandbox thread for 7 s did not stop the worker, before or after. Chromium delivers the queued heartbeats before the overdue timer, which is why Chromium users rarely hit this. ### Manual verification notes - Manually verify in **Safari** on macOS: enable a client extension with a top-bar button or panel, check that it responds, switch to another tab for at least 30 seconds, come back, and check that the button and panel controls still respond. - Manually verify in **Safari**: reload the app at least 5 times and check that the extension's top-bar button appears every time, without switching tabs or toggling the extension. - Manually verify the same in Chrome or Firefox, including leaving the tab hidden for more than 5 minutes (Chrome's heavier background throttling). - Manually verify sleep: with the extension open, put the Mac to sleep for a minute, wake it, and check the controls still respond. - Manually verify on iOS Safari or the Android app: switch to another app for a minute, come back, and check the controls still respond. - Manually verify that a hung extension is still stopped: enable a test client extension whose button handler runs `while (true) {}`, click it on a visible tab, and check that within about 6 seconds its buttons and panel disappear and the notice "{name} stopped working." appears with **Restart**. - Manually verify that **Restart** brings the extension's buttons back, and that the notice uses theme colours in light and dark mode. - Manually verify that an extension that throws an ordinary error (for example `throw new Error("x")` inside a button handler) keeps its buttons and does not show the stopped notice. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) Docs: `docs/development/personal-extensions.md` and `CHANGELOG.md` updated; `[docs-i18n]` follow-up is #7261. ## UI evidence (if applicable) The only new UI is a standard Sonner notice with a **Restart** action, styled by the app's existing toast theme. No screenshot yet; it appears only after the sandbox stops a hung extension. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Client extensions remain active when switching tabs, when the browser is in the background, or while a device sleeps. * Safari extensions start on each page load, helping keep their buttons available in the top bar. * **New Features** * When an extension stops, its buttons and panels are hidden and a notice offers a **Restart** option. Restarting restores the extension. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:06:11 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7649
No description provided.