Skip to content

remote: fix metadata refresher lifetime and stale token recovery - #138

Merged
piscisaureus merged 2 commits into
mainfrom
fix-metadata-refresher
Jun 6, 2026
Merged

piscisaureus merged 2 commits into
mainfrom
fix-metadata-refresher

Conversation

@piscisaureus

Copy link
Copy Markdown
Member

Fixes #137.

  • Remote derives Clone and all clones share one metadata refresh
    task via Arc<JoinHandle>, but the Drop impl aborted that task on
    every clone drop. watch() captures a clone in its stream, so
    dropping a watch stream permanently killed the refresher for the
    whole database handle: the data path then reused the last token
    forever and, once it expired, every call failed with an auth error
    until the process restarted. The abort is moved into a
    MetadataRefresher guard owned by the Arc, so it fires only when
    the last clone is dropped.
  • The data path no longer sends tokens that are known to be expired:
    call_raw checks expires_at before use and forces an immediate
    metadata refresh (bounded wait) first. The refresh task wakes on
    demand via Notify instead of only on its timer, and wakes forced-
    refresh waiters even when a fetch attempt fails.
  • On an auth-failure response (400/401/403) from the data path, force
    a metadata refresh and retry once before surfacing the error, so a
    token the server considers stale (e.g. due to clock leeway past
    expiresAt) heals transparently.

New tests in remote/tests/refresh.rs use a mock transport with
controllable token rotation; the clone-drop, watch-drop, and
auth-retry tests fail against the previous implementation.

* `Remote` derives `Clone` and all clones share one metadata refresh
  task, but the `Drop` impl aborted that task on every clone drop.
  `watch()` captures a clone in its stream, so dropping a watch
  stream permanently killed the refresher for the whole database: the
  data path then reused the last token forever and every call failed
  with an auth error once the token expired. The abort now lives in a
  guard owned by the `Arc`, so it fires only when the last clone is
  dropped.
* The data path no longer sends tokens that are known to be expired:
  `call_raw` checks `expires_at` and forces an immediate metadata
  refresh first. The refresh task wakes on demand via `Notify`
  instead of only on its timer.
* On an auth-failure response (400/401/403), force a metadata refresh
  and retry once before surfacing the error, so a token that the
  server considers stale heals transparently instead of failing user
  calls.
Review feedback:

* A 400 response triggers the forced-refresh retry only when the
  response body mentions the authorization header, so genuinely
  malformed requests (e.g. an invalid atomic_write) are not replayed.
  401/403 still retry unconditionally.
* Add a test covering the expiry pre-flight: already-expired metadata
  with a wedged refresh fetch must force a refresh attempt, wait out
  the bounded timeout, and fall back to the held token instead of
  hanging or failing.
* Document the benign redundant fetch that occurs when a forced
  refresh lands while a metadata fetch is already in flight.
@piscisaureus
piscisaureus force-pushed the fix-metadata-refresher branch from 16d4e11 to c32f7ea Compare June 6, 2026 07:34
@piscisaureus
piscisaureus merged commit 9765053 into main Jun 6, 2026
20 checks passed
@piscisaureus
piscisaureus deleted the fix-metadata-refresher branch June 6, 2026 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

remote: expired metadata token is used without refresh or retry, surfacing auth errors to callers

1 participant