Skip to content

🐛 fix(auth): avoid race when storing fingerprint sessions#122

Merged
bitbacchus merged 3 commits into
mainfrom
fix-auth-race-condition
May 20, 2026
Merged

🐛 fix(auth): avoid race when storing fingerprint sessions#122
bitbacchus merged 3 commits into
mainfrom
fix-auth-race-condition

Conversation

@bitbacchus
Copy link
Copy Markdown
Member

@bitbacchus bitbacchus commented May 20, 2026

Summary

On reload, the backend auth middleware restores the user session and stores the session actor mapping. Previously, the middleware could continue before the actor-system session update had fully completed. Under unlucky timing, the frontend could then continue with an incomplete or stale backend session state.

This change makes the relevant actor-system calls awaitable so that the backend only proceeds after the session/user-store update has completed.

The changes make the relevant actor calls awaitable so that the backend only proceeds after the user-store update has completed.

Problem

Regular users could run into inconsistent state after reloading the UI. The likely sequence was:

  1. The browser reloads the frontend.
  2. The frontend reconnects and requests authenticated state.
  3. The backend auth middleware starts restoring/storing the user session.
  4. The middleware continues before the actor-system update has completed.
  5. The frontend observes incomplete or stale session/user state.

This could make the UI behave as if the user/session state was not fully restored yet.

Changes

  • In authMiddleware.ts:

    • await the ActorSystem.ask(...) call when storing a new session/user mapping.
    • ensure the authorization error path still returns immediately when no user id is available.
  • In FingerprintStore.ts:

    • store the stable user id (uid) in UserStore instead of the fingerprint id (fp.uid) when blocking/unblocking a user.
    • await the UserStoreMessages.Update(...) send call so the update completes before returning.

This should reduce inconsistent authentication/session state caused by asynchronous actor updates completing after the middleware has already continued.

claude and others added 3 commits May 20, 2026 12:57
…king

FingerprintStore.Block and Unblock retrieve the user record via
GetByFingerprint to find the user's uid, but then sent
UserStoreMessages.Update with `uid: fp.uid`, which is the fingerprint
id — so the wrong record (or no record) was being updated. Pass the
user's uid instead.
The WS authentication middleware previously did

  system.send(SessionStore, StoreSession({ uid, actorSystem }));
  next(undefined);

— a fire-and-forget that returned the HTTP 101 to the client before the
SessionStore's clientIndex had been updated with the new actorSystem ↔
uid mapping. The browser would then immediately ask actors that resolve
the caller's role via determineRole → SessionStore.GetSessionForClient,
which would miss in clientIndex and return "Unknown client session".

This produced a ~30 second Dashboard reload spinner: the frontend's
afterStart asks (UserAdminActor.GetAll, FingerprintStore.GetMostRecent,
LocalUserActor.GetOwn) would hit timeouts (5s each + 5s sweep) before
the system Promise could resolve and the spinner could clear.

Converting send → ask makes the handshake wait until clientIndex is
populated, so the very first ask the new client sends resolves its
session correctly.
…n-oJOKj

Fix user uid handling and WS handshake in backend
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/backend/src/actors/FingerprintStore.ts 0.00% 2 Missing ⚠️
packages/backend/src/middlewares/authMiddleware.ts 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@bitbacchus bitbacchus merged commit 783ce8b into main May 20, 2026
5 of 6 checks passed
@bitbacchus bitbacchus deleted the fix-auth-race-condition branch May 20, 2026 13:48
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