Skip to content

fix(core): token race condition - #15320

Merged
darkskygit merged 2 commits into
canaryfrom
darksky/fix-token-race-condition
Jul 22, 2026
Merged

fix(core): token race condition#15320
darkskygit merged 2 commits into
canaryfrom
darksky/fix-token-race-condition

Conversation

@darkskygit

@darkskygit darkskygit commented Jul 22, 2026

Copy link
Copy Markdown
Member

fix #15318
fix #15310

PR Dependency Tree

This tree was auto-generated by Charcoal

Summary by CodeRabbit

  • Bug Fixes
    • Authenticated sessions are now restored automatically when the desktop app starts.
    • Previously saved access tokens are available immediately for recognized endpoints.
    • A problem initializing one saved session no longer prevents other sessions from loading.

@github-actions github-actions Bot added the app:electron Related to electron app label Jul 22, 2026
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c7f38fb8-baed-4903-865c-6497966559da

📥 Commits

Reviewing files that changed from the base of the PR and between d00e6cd and bcae921.

📒 Files selected for processing (1)
  • packages/frontend/apps/electron/test/main/auth-session.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/frontend/apps/electron/test/main/auth-session.spec.ts

📝 Walkthrough

Walkthrough

Persisted authentication sessions are initialized through a new exported function. Electron protocol registration awaits this initialization before installing protocol hooks, and tests verify restored endpoint management and access tokens.

Changes

Auth session startup

Layer / File(s) Summary
Persisted session restoration
packages/frontend/apps/electron/src/main/auth/auth-session.ts, packages/frontend/apps/electron/test/main/auth-session.spec.ts
initializeAuthSessions loads stored endpoints, initializes their brokers while logging individual failures, and tests restoration of managed endpoint state and access tokens.
Protocol registration ordering
packages/frontend/apps/electron/src/main/protocol.ts
registerProtocol imports and awaits initializeAuthSessions() before registering Electron protocol and handler hooks.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant registerProtocol
  participant initializeAuthSessions
  participant readStore
  participant AuthTokenBroker
  participant ElectronHooks
  registerProtocol->>initializeAuthSessions: initialize persisted sessions
  initializeAuthSessions->>readStore: read stored endpoints
  readStore-->>initializeAuthSessions: return session records
  initializeAuthSessions->>AuthTokenBroker: getAuthSessionBroker(endpoint)
  AuthTokenBroker-->>initializeAuthSessions: initialize endpoint broker
  initializeAuthSessions-->>registerProtocol: complete initialization
  registerProtocol->>ElectronHooks: register protocol and handlers
Loading

Possibly related PRs

Suggested labels: test, app:core

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ❓ Inconclusive The auth-session initialization change plausibly addresses the logout-on-reopen issues in #15318 and #15310, but the cleanup error is not directly fixed. Confirm whether the shutdown cleanup error is intentionally out of scope or add a fix if it is part of the acceptance criteria.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and matches the core auth token/session race condition fixed by the PR.
Out of Scope Changes check ✅ Passed The changes stay focused on auth-session initialization and related tests, with no clear unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot added app:core test Related to test cases labels Jul 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/frontend/apps/electron/src/main/auth/auth-session.ts`:
- Around line 146-149: Update initializeAuthSessions to check
secureStorageAvailable() before reading persisted store keys or restoring
brokers, and skip initialization when secure storage is unavailable so endpoints
are not marked managed without usable tokens. Add a regression test covering
restart with unavailable secure storage and preserving the renderer
Authorization header.

In `@packages/frontend/apps/electron/test/main/auth-session.spec.ts`:
- Around line 103-105: Update the seeded session in the test using
accessExpiresAt to derive the expiry from the test clock, Date.now(), and
pair.expiresIn instead of a fixed 2030 timestamp; leave the refresh token and
refreshExpiresAt setup unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d8d59595-6654-439e-94ae-5e1188937698

📥 Commits

Reviewing files that changed from the base of the PR and between 4b37f9d and d00e6cd.

📒 Files selected for processing (3)
  • packages/frontend/apps/electron/src/main/auth/auth-session.ts
  • packages/frontend/apps/electron/src/main/protocol.ts
  • packages/frontend/apps/electron/test/main/auth-session.spec.ts

Comment thread packages/frontend/apps/electron/src/main/auth/auth-session.ts
Comment thread packages/frontend/apps/electron/test/main/auth-session.spec.ts Outdated
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 22, 2026

Copy link
Copy Markdown

Deploying blocksuite-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: bcae921
Status:⚡️  Build in progress...

View logs

@darkskygit
darkskygit merged commit 02e7586 into canary Jul 22, 2026
41 of 42 checks passed
@darkskygit
darkskygit deleted the darksky/fix-token-race-condition branch July 22, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app:core app:electron Related to electron app test Related to test cases

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Bug]: every new session keeps logging me out. [Bug]: Win 11 Pro - Affine Logs me Out, Not Saving State

1 participant