fix(trailbase-db-collection): handle 403 on subscribe/* with polling fallback #1521
main fromrefs/pull/1521/head·opened Problem#
When a TrailBase table has row-level access rules (_ROW_.*), the server cannot evaluate those predicates for wildcard subscriptions and returns 403 Forbidden on GET /api/records/v1/<table>/subscribe/*.
This caused two separate bugs:
Bug 1 — Collection stuck in loading state forever#
start() called subscribe("*") without any error handling. The thrown 403 error propagated past the finally block, so markReady() was never called → useLiveQuery.data remained undefined indefinitely, even though list requests returned data normally.
Bug 2 — Every optimistic mutation rolled back after 120 s#
onInsert, onUpdate, and onDelete called awaitIds() unconditionally. awaitIds waits for the subscription SSE stream to confirm the server has persisted the record by checking seenIds. In polling-only mode seenIds is never populated (no SSE events arrive), so the wait always timed out → TimeoutWaitingForIdsError → the optimistic mutation was rolled back.
Fix#
- Wrap
subscribe("*")intry/catch— log aconsole.debugmessage and continue without SSE when the subscribe call fails (e.g. 403). - Hoist
sseAvailableflag to closure scope — set totrueonly when SSE connects successfully. This allows the three mutation handlers to check it. markReady()is always called viafinally— the collection becomes usable even without live updates.onInsert/onUpdate/onDeleteskipawaitIds()when!sseAvailable— the collection reflects server state after the next polling cycle instead of timing out.periodicCleanupTaskis only started when SSE is actually available — avoids a no-op interval running forever in polling-only mode.
Behaviour after fix#
- Tables with open
subscribe/*access → unchanged behaviour (SSE +awaitIdsas before). - Tables with row-level access rules that deny
subscribe/*→ polling-only mode: data loads vialistrequests, optimistic mutations persist immediately and are confirmed after the next poll.
Repro#
// TrailBase config.textproto — row-level rule denies wildcard subscribe
read_access_rule: "_ROW_.user_id = _USER_.id"
- Open the app → collections stuck loading (
useLiveQuery.data === undefined) - Insert a record → appears briefly, then disappears after ~2 min (
TimeoutWaitingForIdsError)
- 1
KyleAMathews(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.