Skip to content

Don't destroy the session when the account lookup returns None#49

Merged
czpython merged 2 commits into
mainfrom
commonzenpython/remove-session-drop-on-missing-account
Jul 21, 2026
Merged

Don't destroy the session when the account lookup returns None#49
czpython merged 2 commits into
mainfrom
commonzenpython/remove-session-drop-on-missing-account

Conversation

@czpython

Copy link
Copy Markdown
Owner

Why

resolve_session_account dropped the session whenever Account.get(account_id) returned None — added in ENG-703 (#15) as cleanup for a "deleted account":

account = Account.get(account_id)
if not account:
    await sessions.drop_session(token)   # ← permanent logout
    return

But nothing deletes accounts — there's no code path, and every account FK is ON DELETE RESTRICT, so an account with any login/run/usage can't be removed. The "account is gone" case the guard defends can't happen legitimately.

So the guard only ever fires on an anomaly — a transient/ambiguous lookup miss — and in exactly that case, destroying the session is the wrong response: it turns a momentary blip into a forced re-login. This bit prod (2026-07-20): a session's account lookup transiently returned None, the session was dropped, and the operator was logged out despite the account being perfectly present.

Change

Remove the drop. A None still 401s that one request (current_session_account raises), but the session survives — so a transient miss self-heals on the next request instead of forcing a re-login. New regression test asserts a missing-account lookup leaves the session key intact.

Verification

  • ruff / ruff format / pyright clean.
  • test_auth.py full suite green (12 passed), including the new test_missing_account_does_not_drop_the_session.
  • Complements the existing test_redis_eviction_signs_out_but_keeps_credentials (real session-key loss → 401 is still correct; that path is untouched).

Related: the transient-None trigger itself (why Account.get returned None under a concurrent request burst) is a separate open thread that needs request-level logging to pin — this PR removes the destructive amplifier, not that root.

czpython added 2 commits July 20, 2026 22:11
resolve_session_account dropped the session whenever Account.get(account_id)
returned None — added in ENG-703 as cleanup for a "deleted account". But
nothing deletes accounts (no code path, and the account FKs are ON DELETE
RESTRICT), so that case can't happen legitimately. The guard only ever fires
on an anomaly — a transient/ambiguous lookup miss — and in exactly that case
destroying the session turns a momentary blip into a forced re-login.

Remove the drop. A None still 401s the request; the session survives so a
transient miss self-heals on the next request. Regression test asserts a
missing-account lookup leaves the session key intact.
@czpython
czpython merged commit 1d854ef into main Jul 21, 2026
1 check passed
@czpython
czpython deleted the commonzenpython/remove-session-drop-on-missing-account branch July 21, 2026 06:57
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.

1 participant