OAuth non-rotating refresh: hold one DB connection per request (fix pool exhaustion)

Author: tamassolteszCreated Sep 24, 2026Updated Sep 24, 2026
Labelsagent-wip

Part of PLAN-017. Fixes the connection-ownership regression from Core PR #1246 that caused OAuth-refresh pool starvation (RCA Sept 24 2026). Full design: agent-plans PLAN-017.

Problem. OAuthTokenAPI.handleNonRotatingRefresh (:388, via handleNonRotatingRefreshWithLock :354/:372) opens an outer sqlStorage.startTransaction(con -> …, REPEATABLE_READ) (:398, @UnauditedTransaction :387) and holds that connection for the whole Hydra round-trip, while in-tx helpers borrow MORE connections from the same 3-max pool → hold-and-wait Hikari timeouts.

Cross-repo dependency (NOT auto-wired): needs the PLAN-017 pi contract (isOAuthTokenRevokedByGID_Transaction) and pg impl merged/available.

What to change — thread con so no in-tx helper borrows a 2nd connection.

  1. Revocation read (always nested; 289 RCA events): the in-tx call to OAuth.verifyAndUpdateIntrospectRefreshTokenPayload(...) (:419; def OAuth.java:460) → isTokenRevokedBasedOnPayload (OAuth.java:504) must use a con-aware overload calling oauthStorage.isOAuthTokenRevokedByGID_Transaction(appIdentifier, con, gid). Keep the existing non-con overload for the rotating caller (:189).
  2. Signing (cold-static + dynamic only; warm-static already DB-free via PR #1413 at SigningKeys.java:190-199 — KEEP that fast path). Add a con-threaded path: OAuth.transformTokens(...) (:449; def OAuth.java:331) → OAuthToken.reSignToken(..., con) → SigningKeys.getStaticKeyForAlgorithm(algorithm, con) / getAllKeys(con) / getLatestIssuedDynamicKey(con). On a cache miss the signing-key read must run getJWTSigningKeys_Transaction(appIdentifier, con) on the passed connection, never JWTSigningKey.getOrCreateAndGetKeyForAlgorithm's own startTransaction (JWTSigningKey.java:194). PREFERRED: pre-warm the signing keys BEFORE startTransaction so the in-tx path is read/cache only and no key is created inside the REPEATABLE_READ transaction.
  3. Client lookup: if any client resolution happens inside the tx (e.g. inside OAuthProxyHelper.proxyFormPOST credential handling), route it through con or hoist it before startTransaction. The handler already carries a loaded oauthClient param — prefer reusing it and assert no storage lookup occurs in the callback.
  4. Leave updateLastActive (:464, def :315) OUT of the tx — it may hit a different tenant's pool; document this so nobody threads it in.
  5. Keep the @UnauditedTransaction annotations as-is (behaviour unchanged; full startAuditedTransaction conversion is PLAN-012 backlog).

Acceptance.

  • A single non-rotating refresh performs exactly ONE pool acquisition for its whole DB lifetime (asserted by the PLAN-017 repro test).
  • Warm-static-key fast path preserved (no DB on cache hit).
  • Existing OAuth tests green; no behaviour change to responses.
  • CHANGELOG line under the topmost unreleased section: Fix: non-rotating OAuth refresh held multiple DB connections per request, causing pool exhaustion under load.
  • No version bump in the PR.

Depends-on: supertokens/supertokens-plugin-interface#223 Depends-on: supertokens/supertokens-postgresql-plugin#407 Plan: PLAN-017

Source: supertokens/supertokens-core