fix(frontend): coordinate HTTP token refresh with a shared worker by RainbowDashy · Pull Request #21622 · bytebase/bytebase · GitHub
Skip to content

fix(frontend): coordinate HTTP token refresh with a shared worker - #21622

Merged
RainbowDashy merged 7 commits into
mainfrom
p0ny/shared-worker-token-refresh
Oct 21, 2026
Merged

RainbowDashy merged 7 commits into
mainfrom
p0ny/shared-worker-token-refresh

Conversation

@RainbowDashy

@RainbowDashy RainbowDashy commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On plain HTTP origins, navigator.locks is unavailable and session refresh fails. Keep the existing Web Locks/BroadcastChannel flow where supported and use a SharedWorker otherwise. The worker owns the refresh request and shares its result across tabs, avoiding concurrent use of the backend's single-use refresh token.

The fallback uses a separate Connect client with cookies and no UI interceptors. Same-tab callers share one promise. The client waits for the worker readiness handshake before dispatching a refresh. Startup errors and timeouts close the unused port and retry within the same shared refresh promise, up to three attempts with 250 ms and 500 ms delays. If startup retries are exhausted, the auth interceptor returns temporary unavailability without expiring the session; later API requests can retry startup throughout a longer rollout. Failures or timeouts after readiness require reloading the page before trying the worker again. The worker refresh RPC has no client deadline: the page watchdog gives startup and the dispatched refresh separate 35-second intervals, and rejects waiting callers without aborting an in-flight token rotation. Backend token rotation is unchanged, with no grace period.

This is an independent alternative to #21616; that PR is unchanged.

Validation

  • pnpm --dir frontend fix and pnpm --dir frontend test pass: guards, Biome, TypeScript, production bundle, and 586 test files / 6,614 tests.

  • Added tests for Web Locks selection, fallback selection, concurrent worker ports, rotations lasting beyond the page watchdog without starting a second request, shared failures, later refreshes, recoverable worker startup errors and timeouts, a full refresh watchdog interval after slow startup, terminal failures after readiness, stale worker errors, and unavailable SharedWorker.

  • Chromium verification using the built fallback on a LAN HTTP origin (isSecureContext === false, no navigator.locks) against a simulated single-use refresh endpoint: two tabs produce one request, both succeed, the next rotation uses the replacement HTTP-only cookie, and closing the initiating tab does not interrupt the remaining tab's refresh.

  • No full Bytebase browser E2E run; the browser endpoint above was simulated.

  • Review follow-up: Chromium verification with a production-built worker and a simulated old-replica HTML response confirmed that a single refresh call survives the initial HTML response: two worker loads produce exactly one refresh request. Interceptor regression tests verify that recovery and repeated startup retry exhaustion do not set the session-expired flag or show a reauthentication notification, and that a later request recovers once the worker becomes available.

Compatibility and limits

HTTP fallback requires SharedWorker with module-worker support. Unsupported or blocked workers fail explicitly rather than issuing competing requests. HTTPS/localhost deployments retain the existing Web Locks path. Lost responses after server-side rotation can still require reauthentication, as before.

@RainbowDashy
RainbowDashy requested a review from a team as a code owner October 20, 2026 07:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2cd383c77d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread frontend/vite.config.ts
@RainbowDashy

Copy link
Copy Markdown
Contributor Author

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8a31067202

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread frontend/src/api/refreshTokenSharedWorker.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be81447ef1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread frontend/src/api/refreshToken.worker.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5f3579fe53

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread frontend/src/api/refreshTokenSharedWorker.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30be173013

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread frontend/src/api/refreshTokenSharedWorker.ts Outdated
@RainbowDashy
RainbowDashy merged commit 844a319 into main Oct 21, 2026
9 checks passed
@RainbowDashy
RainbowDashy deleted the p0ny/shared-worker-token-refresh branch October 21, 2026 09:20
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.

2 participants