Repository navigation
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
createdAt, and a refresh writes a new row, so a session that refreshed often always looked newest. Heads are now ordered byCOALESCE("chainStartedAt", "createdAt").revokedAtandreplacedBySessionIdboth null). If a refresh rotated the row after it was read, the eviction followsreplacedBySessionIdto the replacement and evicts that instead.withConcurrentSessionSlottakespg_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.session_evictedaudit 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
tests/unit/services/concurrentSessionPolicy.spec.ts): ordering, conditional eviction, following a rotation, an already-ended chain, a two-headed sign-in, the fallback tocreatedAt, the lock and savepoint, and fail-open.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.tsstill passes.