Skip to content

fix(sessions): enforce the concurrent session limit on sign-in age, atomically - #642

Open
Bccorb wants to merge 2 commits into
mainfrom
fix/concurrent-session-cap
Open

Bccorb wants to merge 2 commits into
mainfrom
fix/concurrent-session-cap

Conversation

@Bccorb

@Bccorb Bccorb commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Part of #600 (L-API-2).

The concurrent session limit (max_concurrent_sessions) could be exceeded in three ways. This closes all three.

What changes

  • Oldest means the sign-in that started first. Eviction ordered by the row's createdAt, and a refresh writes a new row, so a session that refreshed often always looked newest. Heads are now ordered by COALESCE("chainStartedAt", "createdAt").
  • Eviction is conditional on the chain head. The revoke is an update where the row is still the head (revokedAt and replacedBySessionId both null). If a refresh rotated the row after it was read, the eviction follows replacedBySessionId to the replacement and evicts that instead.
  • Count, evict and create are serialised per user. withConcurrentSessionSlot takes pg_advisory_xact_lock(class, hashtext(userId)), evicts inside a savepoint and creates the session in the same transaction, so parallel sign-ins can no longer each count the same sessions. A failed eviction rolls back to the savepoint and the sign-in goes ahead, which matches the existing fail-open behaviour for this housekeeping step. With no limit configured, nothing is locked.
  • A refresh caught mid-rotation counts as one sign-in. A refresh inserts its replacement before it claims the old row, so for a moment one sign-in has two heads. Heads are grouped by the chain's start time. Two sign-ins that start in the same millisecond would be counted as one, and the cost of that is one extra session.
  • The child-session cascade and the session_evicted audit events run after the commit, so they read the evictions as committed.

Session exchange is unchanged. Exchanged sessions do not count toward the limit and end with their parent.

A revoke-all can run in the same moment as an eviction for the same user. If the two deadlock, Postgres aborts one side. When the eviction loses, the sign-in goes ahead without evicting. When the revoke-all loses, its request fails and can be retried.

Contract

No route, schema or status code changes.

Tests

  • Unit (tests/unit/services/concurrentSessionPolicy.spec.ts): ordering, conditional eviction, following a rotation, an already-ended chain, a two-headed sign-in, the fallback to createdAt, the lock and savepoint, and fail-open.
  • Postgres (tests/postgres/concurrentSessionLimit.spec.ts): a refreshed old sign-in is still evicted first, four parallel sign-ins at limit 1 leave one live head, a racing rotation, and a sign-in mid-refresh counted once. tests/postgres/sessionExchange.spec.ts still passes.

…tomically

Eviction ordered by row creation, which a refresh resets, so a session that
refreshed often was never evicted. It now orders by when the sign-in started,
evicts with an update conditional on the row still being the chain head
(following a rotation to its replacement), and holds a per-user advisory lock
across count, evict and create.
A refresh inserts its replacement before claiming the row it replaces, so for a
moment one sign-in has two heads. Counting both evicted an unrelated session.
Heads are now grouped by the chain's start.
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