Skip to content

ui-react: share intake rescans IndexedDB every second to recover abandoned shares #2573

Open

Description

@schlessera

Context

Share intake reviews stashed shares under a native Web Lock per share id. A tab takes ownership with navigator.locks.request(name, { ifAvailable: true }, ...) and keeps the lock for as long as it reviews the share (holdShareReview, packages/ui-react/src/lib/share-review.ts:51-57).

To recover a share whose owning tab was terminated, every mounted intake rescans once a second (window.setInterval(() => void scan(), 1000), packages/ui-react/src/hooks/use-share-intake.ts:130). Each scan:

  • lists the account's review partition;
  • prunes and lists the share stash (shareReviewIds, share-review.ts:88-93), where each list and prune opens and closes its own IndexedDB connection (withStore, packages/ui-sdk/src/client/share-store.ts:129-145);
  • tries an ifAvailable lock for every id not already held.

On an installed app this runs for as long as the app is open, hidden or not, whether or not any share exists.

The polling was deliberate. It came with the share-original retention work in #1804 (PR #1899, commit fb219e069), and its comment states the reason: "A survivor recovers an owner abandoned by a terminated tab. Locks never expire under a slow upload or background suspension."

Web Locks already provide that recovery without polling. A request made without ifAvailable waits in the browser's queue and is granted when the holder releases the lock, including when the holding tab dies, and a queued request can be withdrawn with signal. New shares reach a tab through its own navigation (claimFromLocation, use-share-intake.ts:43-61), which runs on mount. A share that lands in another tab can be announced with a BroadcastChannel.

Guide: modern-web-guidance performance: "DON'T block the thread with recurring timers: Avoid heavy polling with setInterval that starves the main thread." Support (MDN BCD 8.1.6): LockManager.request is in Chrome for Android 69, Samsung Internet 10 and Firefox for Android 96; BroadcastChannel in 54, 6 and 38. A browser without navigator.locks already refuses review with a kept original (share-review.ts:52), and that does not change.

Recorded decision — 2026-10-22

The maintainer ruling selects a hybrid: queued Web Locks, account-scoped BroadcastChannel announcements and one safety rescan every 60 seconds only while visible. This replaces #1804’s one-second mechanism while preserving abandoned-share recovery. No mechanism choice remains.

Scope

Implement the ruled hybrid. Preserve native lock ownership for the entire review lifetime.

  • On mount, and on each announcement, list the ids once. For each id this tab neither holds nor has queued, call navigator.locks.request(name, { signal: lease }, cb) without ifAvailable. The callback runs the existing recoverShareReview path and returns at once if the share is gone.
  • After a successful stash claim or a review state change, post { type: "share-stashed" | "share-settled", id } on a BroadcastChannel named per account, and listen for it to queue or drop ids. Without BroadcastChannel, fall back to rescanning on visibilitychange to visible.
  • Prune the stash once per mount, once per announcement and once per safety pass, never once a second.
  • Run the same idempotent list-and-queue safety pass every 60 seconds only while visible. Pause it when hidden; on visible run one immediate pass and resume. Skip held or already-queued ids.
  • Remove the one-second interval. Clean up the safety timer and visibility/channel listeners; abort every queued request on unmount, root change or account change through the existing lease/controller signals.
  • Update the comment at use-share-intake.ts:127-129.

Out of scope

  • The share-target service-worker handler, the stash format and the 303 redirect.
  • Review UI, confirm, dismiss, or upload behaviour.
  • The IndexedDB connection-per-call pattern in share-store.ts: with polling gone it is no longer hot, and pooling connections is a separate change.
  • Browsers without Web Locks.

Acceptance criteria

  • The hybrid ruling is recorded and supersedes ui-react: share review admission deletes the durable original before confirmation #1804’s one-second recovery mechanism.
  • Timer tests prove one safety pass at 60 seconds while visible, none while hidden, one immediate pass on becoming visible and no duplicate queued lock requests.
  • With lock queueing disabled in a test double, the safety pass alone recovers an abandoned share within one interval.
  • A real-browser test in the share-review suite (packages/ui-react/tests/browser/share-review/) shows that when tab A holds a share under review and is closed, tab B picks the share up without a reload. The test passes before and after the change, so recovery is preserved.
  • The same test shows that with no share stashed and both tabs idle for 10 s, no IndexedDB open happens after mount (counted by wrapping indexedDB.open), where the tree before the change opens one or more per second.
  • A share stashed through tab A's navigation is queued in tab B through the broadcast, without a timer.
  • After unmount, no lock request remains queued (navigator.locks.query() shows none pending for brain-ui:share-review:*).
  • Existing share intake tests (packages/ui-react/tests/share-intake.test.ts, the share-review browser suite) pass.

Verification

bun run test packages/ui-react/tests/share-intake.test.ts
bun run test:layout
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

Not contract, and no changeset: recovery behaves the same, only the mechanism changes. Add a changeset if the ruling changes observable behaviour.

2026-10-22 triage at main aa8c8966b59f031f054f4876dbca84048b40ae4f: verified the still-active one-second interval and explicit abandoned-owner recovery comment. The proposed queued-lock/BroadcastChannel replacement and the alternative hidden-page pause remain a genuine mechanism choice; preserve needs: decision, p3 and native #2565 parent. No timer removal, lock-queue runtime proof or approved replacement is inferred.

2026-10-22 readiness reconciliation at inspected main aa8c896: folded the later maintainer ruling into the active specification and criteria. Earlier proposal/needs statements above describe their dated review stage. The accepted brief has no remaining needs prerequisite; add agent-ready. No implementation or runtime completion is claimed.

Activity

  1. added
    type: refactor
    area: ui-react
    priority: p3
    needs: decision
    on Oct 22, 2026
  2. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    2026-10-22 triage at main aa8c8966b59f031f054f4876dbca84048b40ae4f: verified the still-active one-second interval and explicit abandoned-owner recovery comment. The proposed queued-lock/BroadcastChannel replacement and the alternative hidden-page pause remain a genuine mechanism choice; preserve needs: decision, p3 and native #2565 parent. No timer removal, lock-queue runtime proof or approved replacement is inferred.

  3. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    Maintainer ruling

    Hybrid. This replaces the 1-second rescan chosen in #1804 (PR #1899).

    1. Primary recovery: queued Web Locks. On mount and on each announcement, list the ids once. For each id this tab neither holds nor has queued, call navigator.locks.request(name, { signal: lease }, cb) without ifAvailable. The browser grants the lock when the owner releases it or its tab dies. The callback runs recoverShareReview and returns at once if the share is gone.
    2. Announcements: a BroadcastChannel per account. After a stash claim or a review state change, post { type: "share-stashed" | "share-settled", id }. Listeners queue or drop ids accordingly. Without BroadcastChannel, rescan on visibilitychange to visible.
    3. Safety rescan: every 60 s, only while the page is visible. It runs the same list-and-queue pass, idempotently: ids that are already held or queued are skipped. It pauses on hidden, resumes on visible with one immediate pass, and stops on unmount, root change or account change. This is the belt-and-braces check in case a lock queue entry is ever lost.
    4. Prune the stash once per mount, once per announcement and once per safety pass. Never once a second.

    The 1 s setInterval is removed. Update the comment at use-share-intake.ts:127-129 to describe the three layers.

    Acceptance criteria adjustments

    • Unchanged: tab B picks up an abandoned share without a reload. A share stashed through tab A's navigation is queued in tab B through the broadcast. After unmount, nothing is pending in navigator.locks.query().
    • Idle IndexedDB test: stays as written. 10 s idle shows no indexedDB.open after mount, because the safety pass runs at 60 s.
    • New, with fake timers:
      • The safety pass runs once at 60 s while visible.
      • It does not run while hidden.
      • It runs once immediately on becoming visible again.
      • It queues no duplicate lock request for an id already queued.
    • New: with lock queueing disabled in a test double, the safety pass alone recovers an abandoned share within one interval.

    No contract impact and no changeset: recovery behaves the same, only the mechanism changes.

  4. added
    agent-ready
    and removed
    needs: decision
    on Oct 22, 2026
  5. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    2026-10-22 readiness reconciliation: folded the maintainer ruling into the body, replacing the unresolved-choice specification and aligning scope/criteria with the accepted branch. The remaining work is explicitly scoped; add agent-ready. Preserve priority, native parent, assignment and release attribution. This is issue preparation, not implementation, runtime proof or delivery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactpriority: p3type: refactor

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions