You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[$175] After the session expires, the app resends the failed request in a loop with no limit or pause #106958
After the user signs back in, the app sends the failed request again right away. There is no retry limit and no pause. One device in this state can send thousands of requests.
Impact
Severity: P1
About 9,000 new expense requests a day hit an expired session.
One request type got 3,500 retries in a single 6 hour window.
changed the title [-]After the session expires, the app resends the failed request in a loop with no limit or pause[/-][+][$175] After the session expires, the app resends the failed request in a loop with no limit or pause[/+]on Nov 16, 2026
The original request has no retry budget after successful reauthentication. For queued writes and requests with side effects, the middleware immediately retries the request through the same pipeline. If it keeps receiving 407 while authentication succeeds, this cycle can continue indefinitely.
Existing limits do not stop this path: authentication backoff counts failed authentication attempts, while the sequential queue waits for the recursive request chain to finish before applying its own backoff.
Confidence: Medium overall. The control-flow gap is confirmed in code. Production attribution remains unverified: log access was unavailable, and the issue provides no incident timestamps or request identifiers. Normal requests use the refreshed token, so ordinary session expiry alone does not explain persistent 407 responses.
Authentication-only throttle:Reauthentication.ts:23–62 applies backoff only when authentication rejects.
Outer queue protection is bypassed:SequentialQueue.ts:435–437 waits for the middleware promise; its retry handler cannot intervene while recursive retries remain pending.
What changes do you think we should make in order to solve the problem?
Add a bounded retry budget and exponential backoff per original request, counting replays after successful authentication. Successful authentication must not reset that budget.
Coordinate middleware and queue retries so exhausting one budget cannot start another automatic retry cycle.
On exhaustion, stop automatic retries, settle the request, clear loading state, and show an actionable error while preserving recoverable expense/receipt data. Ensure later queued requests can proceed.
Add regression tests for repeated original-request 407 responses with successful authentication between attempts. Verify finite attempts, delays, terminal cleanup exactly once, and queue progress. Also cover normal recovery, concurrent requests, side-effect requests, and both HTTP- and JSON-level 407.
Production confirmation still needed: correlate one logical operation’s repeated 407 responses with successful authentication and replay timing. The reported aggregate counts alone do not establish that sequence or explain why refreshed authentication is rejected.
What alternative solutions did you explore? (Optional)
Rely on the hydration fix: removes one trigger, but leaves replay unbounded.
Change only the authentication-failure throttle: does not cover successful authentication followed by another rejected original request.
Next Steps for Contributor+ team: To accept:@MelvinBot implement [this](https://github.com/Expensify/App/issues/106958) to create a draft PR. To refine:@MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.
When an API request (such as a queued write from SequentialQueue or a side-effect request) receives a 407 (session expired), Reauthentication.ts invokes reauthenticate(). Once reauthentication resolves with result.wasSuccessful === true, the middleware immediately and recursively replays the request via processWithMiddleware(request, isFromSequentialQueue) without any retry counter, backoff delay, or recursion cap.
If the subsequent replayed request is again rejected with a 407 (e.g. if the refreshed session credential is not recognized by a specific downstream microservice, is delayed in server-side replication, or encounters an authorization mismatch), processWithMiddleware recursively re-triggers reauthentication and re-execution indefinitely in an unthrottled loop. Because this recursion happens entirely inside the middleware promise chain before SequentialQueue's own retry logic can intervene, a single client device can emit thousands of requests (e.g. 3,500 retries in 6 hours).
What changes do you think we should make in order to solve the problem?
Add a retry counter and exponential backoff to request replay in Reauthentication.ts:
Track the number of consecutive re-auth replay attempts on the request object (e.g. request.reauthRetryCount = (request.reauthRetryCount ?? 0) + 1).
Enforce a maximum retry threshold (e.g. CONST.NETWORK.MAX_REAUTH_RETRIES = 3).
Before replaying the request via processWithMiddleware, apply an exponential backoff delay based on request.reauthRetryCount (e.g. 1s, 2s, 4s) using a delay promise.
Halt and surface an error when retry limit is exhausted:
If request.reauthRetryCount > MAX_REAUTH_RETRIES:
Terminate the recursive retry loop.
Reject the request with a descriptive error (e.g. new Error('Max reauthentication retries exceeded')).
Allow SequentialQueue / OnyxUpdates to apply failureData and finallyData, cleanly clearing optimistic loading states and surfacing an actionable error to the user instead of spinning infinitely.
What alternative solutions did you explore? (Optional)
We explored only relying on the authentication throttle in Reauthentication.ts:23–62. However, that throttle only activates when reauthenticate() itself fails; when reauthentication returns wasSuccessful === true but the replayed original request is still rejected by the server with a 407, that throttle is completely bypassed. Enforcing the retry budget directly on the replayed request guarantees a hard stop regardless of re-authentication status.
After the session expires, the app resends a failed request in a loop: the
request is replayed after re-authentication, the replay is rejected again,
and the cycle repeats with no attempt limit and no pause.
Root cause
The loop is in the REPLAY, not in the authentication call - and the existing
throttle guards the wrong one.
In handleExpiredSession (src/libs/Middleware/Reauthentication.ts), a 407
triggers reauthenticate(), and on success the request is replayed through
the full middleware chain: processWithMiddleware(request, isFromSequentialQueue).
The replay re-enters this same middleware. If the replayed request is
answered 407 again - Authenticate succeeded, but the session is still
rejected for that command - handleExpiredSession runs again, reauthenticates
again, replays again. There is no counter and no backoff anywhere in this
cycle, so it repeats indefinitely.
The reauthThrottle (RequestThrottle) does not cover this: it only
throttles failures INSIDE reauthenticate() itself (retryReauthenticate).
When Authenticate keeps succeeding, the throttle never engages.
The codebase already documents the correct give-up contract in this same
function: "A resolved 407 reads as success to SequentialQueue, which deletes
the persisted write. Throw on every give-up." The replay path simply never
gives up, so the contract is never reached.
Changes
New constant.CONST.NETWORK.MAX_REAUTH_RETRIES: 3, next to the
existing retry limits (MAX_REQUEST_RETRIES: 10, MAX_OPEN_APP_REQUEST_RETRIES: 2, src/CONST/index.ts:3391-3392).
Count attempts on the request. Add an optional reauthAttemptCount
to the Request type (src/types/onyx/Request.ts). handleExpiredSession
increments it each time it is about to replay a request because of a 407.
The count lives on the request object, so it travels with the replay
through processWithMiddleware - no module-level state, no cross-request
leakage.
Cap and back off in handleExpiredSession.
Before replaying, if reauthAttemptCount has reached MAX_REAUTH_RETRIES: Log.alert with the command and the attempt
count, then give up the way the file's own contract requires - throw new Error('Failed to reauthenticate') for the sequential-queue path
(SequentialQueue then applies the request's failure data and stops,
instead of deleting the write as if it had succeeded), and return the
original 407 data for the non-queue paths so their callers handle it.
Between attempts, wait via the existing throttle: reauthThrottle.sleep(error, 'Authenticate') before the replay, so
retries are spaced instead of a tight loop hammering both Authenticate
and the original command.
On a replay that succeeds (no 407), the count is irrelevant - the
request completes and is discarded, so no reset bookkeeping is needed
beyond the request's own lifetime.
Nothing else in the middleware changes. The offline early-throw, the shouldRetry / skipReauthentication exemptions, the delegate-session
restore handling (which clears failure/finally data), and the MainQueue
replay for deprecated-API requests all keep their current behaviour -
the cap only applies to the processWithMiddleware replay cycle that
loops.
Not changed:reauthenticate() and retryReauthenticate (Authenticate
itself is not the loop), SequentialQueue's own transport retry limits, and
the sign-in redirect on genuine re-authentication failure.
Tests
New file tests/unit/Middleware/ReauthenticationTest.ts (the middleware has
no dedicated test file today):
A request whose replay keeps returning 407 is replayed at most MAX_REAUTH_RETRIES times; the next 407 throws Failed to reauthenticate on the sequential-queue path and the request is
not replayed again.
A request that 407s once and then succeeds is replayed exactly once and
completes normally.
The backoff is invoked between attempts (the throttle's sleep is called
with the attempt's error).
Offline still throws the offline error immediately, before any counting.
A request with skipReauthentication / no shouldRetry is returned
untouched, as today.
Alternatives considered
Cap inside reauthenticate(). Rejected: Authenticate is succeeding -
capping it would break the normal "session expired once, re-auth, carry
on" flow and still would not stop the replay cycle, which is driven by
the replayed command's 407s, not by auth failures.
Pause the whole sequential queue on the first 407. Rejected: a single
expired session is routine and self-heals today; pausing everything would
stall unrelated requests behind one command's problem.
Rely on SequentialQueue's MAX_REQUEST_RETRIES. Rejected: that
counter governs transport-level retries of the outer request. Each
middleware replay RESOLVES with a response, so from the queue's point of
view no retry ever happens - the loop is invisible to it. The cap has to
live in the middleware.
Reuse SUSTAINED_FAILURE_THRESHOLD_COUNT instead of a new constant.
Rejected: that constant describes a failure-rate window for a different
mechanism; a named MAX_REAUTH_RETRIES next to the other retry limits is
clearer and independently tunable.
Proposal updated: please review this version of my proposal above (#106958 (comment)), reformatted into the official template. The solution and scope are unchanged.
Please re-state the problem that we are trying to solve in this issue.
After the session expires, the app resends a failed request in a loop: the
request is replayed after re-authentication, the replay is rejected again,
and the cycle repeats with no attempt limit and no pause.
What is the root cause of that problem?
The loop is in the REPLAY, not in the authentication call - and the existing
throttle guards the wrong one.
In handleExpiredSession (src/libs/Middleware/Reauthentication.ts), a 407
triggers reauthenticate(), and on success the request is replayed through
the full middleware chain: processWithMiddleware(request, isFromSequentialQueue).
The replay re-enters this same middleware. If the replayed request is
answered 407 again - Authenticate succeeded, but the session is still
rejected for that command - handleExpiredSession runs again, reauthenticates
again, replays again. There is no counter and no backoff anywhere in this
cycle, so it repeats indefinitely.
The reauthThrottle (RequestThrottle) does not cover this: it only
throttles failures INSIDE reauthenticate() itself (retryReauthenticate).
When Authenticate keeps succeeding, the throttle never engages.
The codebase already documents the correct give-up contract in this same
function: "A resolved 407 reads as success to SequentialQueue, which deletes
the persisted write. Throw on every give-up." The replay path simply never
gives up, so the contract is never reached.
What changes do you think we should make in order to solve the problem?
New constant.CONST.NETWORK.MAX_REAUTH_RETRIES: 3, next to the
existing retry limits (MAX_REQUEST_RETRIES: 10, MAX_OPEN_APP_REQUEST_RETRIES: 2, src/CONST/index.ts:3391-3392).
Count attempts on the request. Add an optional reauthAttemptCount
to the Request type (src/types/onyx/Request.ts). handleExpiredSession
increments it each time it is about to replay a request because of a 407.
The count lives on the request object, so it travels with the replay
through processWithMiddleware - no module-level state, no cross-request
leakage.
Cap and back off in handleExpiredSession.
Before replaying, if reauthAttemptCount has reached MAX_REAUTH_RETRIES: Log.alert with the command and the attempt
count, then give up the way the file's own contract requires - throw new Error('Failed to reauthenticate') for the sequential-queue path
(SequentialQueue then applies the request's failure data and stops,
instead of deleting the write as if it had succeeded), and return the
original 407 data for the non-queue paths so their callers handle it.
Between attempts, wait via the existing throttle: reauthThrottle.sleep(error, 'Authenticate') before the replay, so
retries are spaced instead of a tight loop hammering both Authenticate
and the original command.
On a replay that succeeds (no 407), the count is irrelevant - the
request completes and is discarded, so no reset bookkeeping is needed
beyond the request's own lifetime.
Nothing else in the middleware changes. The offline early-throw, the shouldRetry / skipReauthentication exemptions, the delegate-session
restore handling (which clears failure/finally data), and the MainQueue
replay for deprecated-API requests all keep their current behaviour -
the cap only applies to the processWithMiddleware replay cycle that
loops.
Not changed:reauthenticate() and retryReauthenticate (Authenticate
itself is not the loop), SequentialQueue's own transport retry limits, and
the sign-in redirect on genuine re-authentication failure.
Tests:
New file tests/unit/Middleware/ReauthenticationTest.ts (the middleware has
no dedicated test file today):
A request whose replay keeps returning 407 is replayed at most MAX_REAUTH_RETRIES times; the next 407 throws Failed to reauthenticate on the sequential-queue path and the request is
not replayed again.
A request that 407s once and then succeeds is replayed exactly once and
completes normally.
The backoff is invoked between attempts (the throttle's sleep is called
with the attempt's error).
Offline still throws the offline error immediately, before any counting.
A request with skipReauthentication / no shouldRetry is returned
untouched, as today.
What alternative solutions did you explore? (Optional)
Cap inside reauthenticate(). Rejected: Authenticate is succeeding -
capping it would break the normal "session expired once, re-auth, carry
on" flow and still would not stop the replay cycle, which is driven by
the replayed command's 407s, not by auth failures.
Pause the whole sequential queue on the first 407. Rejected: a single
expired session is routine and self-heals today; pausing everything would
stall unrelated requests behind one command's problem.
Rely on SequentialQueue's MAX_REQUEST_RETRIES. Rejected: that
counter governs transport-level retries of the outer request. Each
middleware replay RESOLVES with a response, so from the queue's point of
view no retry ever happens - the loop is invisible to it. The cap has to
live in the middleware.
Reuse SUSTAINED_FAILURE_THRESHOLD_COUNT instead of a new constant.
Rejected: that constant describes a failure-rate window for a different
mechanism; a named MAX_REAUTH_RETRIES next to the other retry limits is
clearer and independently tunable.
Please re-state the problem that we are trying to solve in this issue.
When an API request (such as a queued sequential write or side-effect request) encounters an expired session (407), Reauthentication.ts reauthenticates and immediately resends the failed request. Because the request tracks no retry count or pause, a request that repeatedly fails or triggers authentication renewal enters an infinite loop without a retry ceiling, causing a single client to flood the backend with thousands of duplicate retries.
What is the root cause of that problem?
In src/libs/Middleware/Reauthentication.ts:139-141:
request carries no metadata tracking how many reauthentication retry attempts have been executed (reauthRetryCount).
Once reauthenticate() succeeds, processWithMiddleware(request, isFromSequentialQueue) is immediately executed with zero backoff or rate-limiting.
If the downstream request continues to fail with a 407 or authentication edge case, this block re-triggers on every response indefinitely, consuming network bandwidth and spamming the backend.
What changes do you think we should make in order to solve the problem?
In src/libs/Network/types.ts (or Request.ts):
Add an optional property to the Request type:
reauthRetryCount?: number;
In src/CONST/NETWORK.ts (or CONST.ts):
Define a safe retry threshold:
In src/libs/Middleware/Reauthentication.ts:
Before invoking processWithMiddleware(request, isFromSequentialQueue):
Check and increment request.reauthRetryCount = (request.reauthRetryCount ?? 0) + 1;
If request.reauthRetryCount > CONST.NETWORK.MAX_REAUTH_RETRIES:
Log.hmmm('Reauthentication retry limit exceeded for request',{command: request.command,retries: request.reauthRetryCount,});if(isFromSequentialQueue){thrownewError('Exceeded maximum reauthentication retries');}returndata;
For valid retries, introduce a brief pause (await sleep(CONST.NETWORK.REAUTH_RETRY_DELAY_MS)) before calling processWithMiddleware to ensure fresh session credentials propagate cleanly.
What alternative solutions did you consider?
Relying purely on SequentialQueue error handling: Rejected because side-effect requests outside the sequential queue also pass through Reauthentication.ts and can loop.
Unconditionally signing out on any second 407: Rejected as too disruptive to the user experience if a transient network race occurs.
Please re-state the problem that we are trying to solve in this issue.
After the session expires and the app signs back in, it resends the failed request straight away, and does it again every time the server answers 407. There is no retry limit and no wait between attempts.
What is the root cause of that problem?
After a successful reauthenticate(), handleExpiredSession resends queued writes and side-effect requests immediately with processWithMiddleware (Reauthentication.ts#L139-L141). That resend runs through the same Reauthentication middleware, so another 407 reauthenticates again (successfully) and resends again, with no end.
The app's retry limits are never reached on this path:
reauthThrottle in the same file only backs off when Authenticate itself fails. Here Authenticate succeeds every time.
The SequentialQueue has the backoff, the 10-attempt limit and the "give up" step that applies failureData, but it only runs when the request rejects. The resend happens inside the middleware, so the queue never sees a failure.
I reproduced it in a unit test on main: with Authenticate succeeding and the server answering 407 thirty times, one queued RequestMoney write was sent 31 times and reauthenticated 30 times in under a second.
What changes do you think we should make in order to solve the problem?
Queued writes: after a successful reauthentication, stop resending from inside the middleware. Reject instead, so the request goes back to the SequentialQueue. Its existing retry path then handles it: rollback, backoff (10-100 ms, doubling up to 10 s), at most MAX_REQUEST_RETRIES (10) attempts, then it gives up, applies failureData (the error shows on the expense) and removes the request. The new session token is used on the next attempt, so a normal expiry still succeeds on the first retry.
Side-effect requests (MAKE_REQUEST_WITH_SIDE_EFFECTS, not queued): keep the resend, but count it per request (a RequestThrottle per request, e.g. in a WeakMap) so it waits with the same backoff and stops after MAX_REQUEST_RETRIES. At the limit, resolve with UNABLE_TO_RETRY, as this middleware already does when reauthentication fails, so the caller shows its error.
When either limit is hit, log once with Log.hmmm (command and attempt count), so a stuck request shows up as one log entry instead of thousands of requests.
I tried change 1 locally: the loop stops, and the existing ReauthenticationMiddlewareTest and SequentialQueueTest suites still pass (69 tests). I would add tests for: repeated 407 after successful reauthentication stops at the limit and applies failureData; a single 407 followed by success still goes through.
What alternative solutions did you explore? (Optional)
A new reauth retry counter on queued requests, next to the queue's own retries. The two budgets multiply (up to 10 middleware retries for each of the queue's 10 attempts) unless they are kept in sync. Handing the request back to the queue keeps one budget and one failure path.
Reusing the module-level reauthThrottle for these resends. It is shared by all requests, so one stuck request would slow every other request's reauthentication, and a success elsewhere would reset its count.
// Each side-effect request gets its own replay budget, so one stuck request doesn't slow the others' reauthentication.constsideEffectReplayThrottles=newWeakMap<Request<OnyxKey>,RequestThrottle>();// ...after a successful reauthenticate():if(isFromSequentialQueue){// The new authToken is already stored, so hand the write back to SequentialQueue: its backoff,// MAX_REQUEST_RETRIES and failureData apply instead of an unbounded resend from here.thrownewError(CONST.ERROR.REAUTHENTICATED_RETRY);}if(apiRequestType===CONST.API_REQUEST_TYPE.MAKE_REQUEST_WITH_SIDE_EFFECTS){constreplayThrottle=sideEffectReplayThrottles.get(request)??newRequestThrottle('ReauthenticationReplay');sideEffectReplayThrottles.set(request,replayThrottle);// Past MAX_REQUEST_RETRIES sleep() rejects, and the caller resolves the request with UNABLE_TO_RETRY.returnreplayThrottle.sleep({name: CONST.ERROR.REAUTHENTICATED_RETRY,message: CONST.ERROR.REAUTHENTICATED_RETRY},request.command).catch(()=>{Log.hmmm('[Reauthenticate] Giving up on a request that kept failing after successful reauthentication',{command: request.command});thrownewError(CONST.ERROR.REAUTHENTICATED_RETRY);}).then(()=>processWithMiddleware(request,false));}
CONST.ERROR.REAUTHENTICATED_RETRY is a new error message. SequentialQueue treats it like any other retryable error: rollback, backoff, retry, and after MAX_REQUEST_RETRIES it gives up and applies failureData.
Results:
Before: one queued RequestMoney getting 407 thirty times was sent 31 times in under a second.
After: the queued write is sent once and handed back to the queue.
After: a side-effect request still succeeds after two 407s.
After: a side-effect request that always gets 407 is sent MAX_REQUEST_RETRIES + 1 times and resolves UNABLE_TO_RETRY.
ReauthenticationMiddlewareTest, SequentialQueueTest, SequentialQueueReadGateTest and NetworkTest still pass (87 tests), and tsc is clean.
Proposal updated: added the code for changes 1 and 2 and local test results (bug reproduced, fix verified, existing network and queue suites pass). #106958 (comment)
Activity
expensify-chore-bot commentedon Nov 16, 2026
Triggered auto assignment to Contributor-plus team member for initial proposal review - @hoangzinh (
External)[-]After the session expires, the app resends the failed request in a loop with no limit or pause[/-][+][$175] After the session expires, the app resends the failed request in a loop with no limit or pause[/+]expensify-chore-bot commentedon Nov 16, 2026
Job added to Upwork: https://www.upwork.com/jobs/~022122193067163277982
MelvinBot commentedon Nov 16, 2026
Proposal
What is the root cause of that problem?
The original request has no retry budget after successful reauthentication. For queued writes and requests with side effects, the middleware immediately retries the request through the same pipeline. If it keeps receiving
407while authentication succeeds, this cycle can continue indefinitely.Existing limits do not stop this path: authentication backoff counts failed authentication attempts, while the sequential queue waits for the recursive request chain to finish before applying its own backoff.
Confidence: Medium overall. The control-flow gap is confirmed in code. Production attribution remains unverified: log access was unavailable, and the issue provides no incident timestamps or request identifiers. Normal requests use the refreshed token, so ordinary session expiry alone does not explain persistent
407responses.Code evidence and related merged change
What changes do you think we should make in order to solve the problem?
407responses with successful authentication between attempts. Verify finite attempts, delays, terminal cleanup exactly once, and queue progress. Also cover normal recovery, concurrent requests, side-effect requests, and both HTTP- and JSON-level407.Production confirmation still needed: correlate one logical operation’s repeated
407responses with successful authentication and replay timing. The reported aggregate counts alone do not establish that sequence or explain why refreshed authentication is rejected.What alternative solutions did you explore? (Optional)
Related issue: Reauth retry counter never resets after a successful reauth concerns the same subsystem but a different counter lifecycle; fixing it alone would not stop this loop.
Next Steps for Contributor+ team:
To accept:
@MelvinBot implement [this](https://github.com/Expensify/App/issues/106958)to create a draft PR.To refine:
@MelvinBot <your feedback>To reject: Explain why you are rejecting Melvin's proposal.
view run
kevinjosh10 commentedon Nov 16, 2026
Proposal
What is the root cause of that problem?
In
src/libs/Middleware/Reauthentication.ts:139–141:When an API request (such as a queued write from
SequentialQueueor a side-effect request) receives a407(session expired),Reauthentication.tsinvokesreauthenticate(). Once reauthentication resolves withresult.wasSuccessful === true, the middleware immediately and recursively replays the request viaprocessWithMiddleware(request, isFromSequentialQueue)without any retry counter, backoff delay, or recursion cap.If the subsequent replayed request is again rejected with a
407(e.g. if the refreshed session credential is not recognized by a specific downstream microservice, is delayed in server-side replication, or encounters an authorization mismatch),processWithMiddlewarerecursively re-triggers reauthentication and re-execution indefinitely in an unthrottled loop. Because this recursion happens entirely inside the middleware promise chain beforeSequentialQueue's own retry logic can intervene, a single client device can emit thousands of requests (e.g. 3,500 retries in 6 hours).What changes do you think we should make in order to solve the problem?
Reauthentication.ts:requestobject (e.g.request.reauthRetryCount = (request.reauthRetryCount ?? 0) + 1).CONST.NETWORK.MAX_REAUTH_RETRIES = 3).processWithMiddleware, apply an exponential backoff delay based onrequest.reauthRetryCount(e.g.1s, 2s, 4s) using a delay promise.request.reauthRetryCount > MAX_REAUTH_RETRIES:new Error('Max reauthentication retries exceeded')).SequentialQueue/OnyxUpdatesto applyfailureDataandfinallyData, cleanly clearing optimistic loading states and surfacing an actionable error to the user instead of spinning infinitely.What alternative solutions did you explore? (Optional)
Reauthentication.ts:23–62. However, that throttle only activates whenreauthenticate()itself fails; when reauthentication returnswasSuccessful === truebut the replayed original request is still rejected by the server with a407, that throttle is completely bypassed. Enforcing the retry budget directly on the replayed request guarantees a hard stop regardless of re-authentication status.Contributor Details:
dankezo commentedon Nov 16, 2026
Contributor details
Your Expensify account email: dannyphan190@gmail.com
Upwork Profile Link: https://www.upwork.com/freelancers/~013b564a690d9bcdab
Proposal
Problem
After the session expires, the app resends a failed request in a loop: the
request is replayed after re-authentication, the replay is rejected again,
and the cycle repeats with no attempt limit and no pause.
Root cause
The loop is in the REPLAY, not in the authentication call - and the existing
throttle guards the wrong one.
In
handleExpiredSession(src/libs/Middleware/Reauthentication.ts), a 407triggers
reauthenticate(), and on success the request is replayed throughthe full middleware chain:
processWithMiddleware(request, isFromSequentialQueue).The replay re-enters this same middleware. If the replayed request is
answered 407 again - Authenticate succeeded, but the session is still
rejected for that command -
handleExpiredSessionruns again, reauthenticatesagain, replays again. There is no counter and no backoff anywhere in this
cycle, so it repeats indefinitely.
The
reauthThrottle(RequestThrottle) does not cover this: it onlythrottles failures INSIDE
reauthenticate()itself (retryReauthenticate).When Authenticate keeps succeeding, the throttle never engages.
The codebase already documents the correct give-up contract in this same
function: "A resolved 407 reads as success to SequentialQueue, which deletes
the persisted write. Throw on every give-up." The replay path simply never
gives up, so the contract is never reached.
Changes
New constant.
CONST.NETWORK.MAX_REAUTH_RETRIES: 3, next to theexisting retry limits (
MAX_REQUEST_RETRIES: 10,MAX_OPEN_APP_REQUEST_RETRIES: 2,src/CONST/index.ts:3391-3392).Count attempts on the request. Add an optional
reauthAttemptCountto the Request type (
src/types/onyx/Request.ts).handleExpiredSessionincrements it each time it is about to replay a request because of a 407.
The count lives on the request object, so it travels with the replay
through
processWithMiddleware- no module-level state, no cross-requestleakage.
Cap and back off in
handleExpiredSession.reauthAttemptCounthas reachedMAX_REAUTH_RETRIES:Log.alertwith the command and the attemptcount, then give up the way the file's own contract requires - throw
new Error('Failed to reauthenticate')for the sequential-queue path(SequentialQueue then applies the request's failure data and stops,
instead of deleting the write as if it had succeeded), and return the
original 407 data for the non-queue paths so their callers handle it.
reauthThrottle.sleep(error, 'Authenticate')before the replay, soretries are spaced instead of a tight loop hammering both Authenticate
and the original command.
request completes and is discarded, so no reset bookkeeping is needed
beyond the request's own lifetime.
Nothing else in the middleware changes. The offline early-throw, the
shouldRetry/skipReauthenticationexemptions, the delegate-sessionrestore handling (which clears failure/finally data), and the MainQueue
replay for deprecated-API requests all keep their current behaviour -
the cap only applies to the
processWithMiddlewarereplay cycle thatloops.
Not changed:
reauthenticate()andretryReauthenticate(Authenticateitself is not the loop), SequentialQueue's own transport retry limits, and
the sign-in redirect on genuine re-authentication failure.
Tests
New file
tests/unit/Middleware/ReauthenticationTest.ts(the middleware hasno dedicated test file today):
MAX_REAUTH_RETRIEStimes; the next 407 throwsFailed to reauthenticateon the sequential-queue path and the request isnot replayed again.
completes normally.
with the attempt's error).
skipReauthentication/ noshouldRetryis returneduntouched, as today.
Alternatives considered
reauthenticate(). Rejected: Authenticate is succeeding -capping it would break the normal "session expired once, re-auth, carry
on" flow and still would not stop the replay cycle, which is driven by
the replayed command's 407s, not by auth failures.
expired session is routine and self-heals today; pausing everything would
stall unrelated requests behind one command's problem.
MAX_REQUEST_RETRIES. Rejected: thatcounter governs transport-level retries of the outer request. Each
middleware replay RESOLVES with a response, so from the queue's point of
view no retry ever happens - the loop is invisible to it. The cap has to
live in the middleware.
SUSTAINED_FAILURE_THRESHOLD_COUNTinstead of a new constant.Rejected: that constant describes a failure-rate window for a different
mechanism; a named
MAX_REAUTH_RETRIESnext to the other retry limits isclearer and independently tunable.
melvin-bot commentedon Nov 16, 2026
✅ Contributor details stored successfully. Thank you for contributing to Expensify!
dankezo commentedon Nov 16, 2026
Proposal updated: please review this version of my proposal above (#106958 (comment)), reformatted into the official template. The solution and scope are unchanged.
Contributor details
Your Expensify account email: dannyphan190@gmail.com
Upwork Profile Link: https://www.upwork.com/freelancers/~013b564a690d9bcdab
Please re-state the problem that we are trying to solve in this issue.
After the session expires, the app resends a failed request in a loop: the
request is replayed after re-authentication, the replay is rejected again,
and the cycle repeats with no attempt limit and no pause.
What is the root cause of that problem?
The loop is in the REPLAY, not in the authentication call - and the existing
throttle guards the wrong one.
In
handleExpiredSession(src/libs/Middleware/Reauthentication.ts), a 407triggers
reauthenticate(), and on success the request is replayed throughthe full middleware chain:
processWithMiddleware(request, isFromSequentialQueue).The replay re-enters this same middleware. If the replayed request is
answered 407 again - Authenticate succeeded, but the session is still
rejected for that command -
handleExpiredSessionruns again, reauthenticatesagain, replays again. There is no counter and no backoff anywhere in this
cycle, so it repeats indefinitely.
The
reauthThrottle(RequestThrottle) does not cover this: it onlythrottles failures INSIDE
reauthenticate()itself (retryReauthenticate).When Authenticate keeps succeeding, the throttle never engages.
The codebase already documents the correct give-up contract in this same
function: "A resolved 407 reads as success to SequentialQueue, which deletes
the persisted write. Throw on every give-up." The replay path simply never
gives up, so the contract is never reached.
What changes do you think we should make in order to solve the problem?
New constant.
CONST.NETWORK.MAX_REAUTH_RETRIES: 3, next to theexisting retry limits (
MAX_REQUEST_RETRIES: 10,MAX_OPEN_APP_REQUEST_RETRIES: 2,src/CONST/index.ts:3391-3392).Count attempts on the request. Add an optional
reauthAttemptCountto the Request type (
src/types/onyx/Request.ts).handleExpiredSessionincrements it each time it is about to replay a request because of a 407.
The count lives on the request object, so it travels with the replay
through
processWithMiddleware- no module-level state, no cross-requestleakage.
Cap and back off in
handleExpiredSession.reauthAttemptCounthas reachedMAX_REAUTH_RETRIES:Log.alertwith the command and the attemptcount, then give up the way the file's own contract requires - throw
new Error('Failed to reauthenticate')for the sequential-queue path(SequentialQueue then applies the request's failure data and stops,
instead of deleting the write as if it had succeeded), and return the
original 407 data for the non-queue paths so their callers handle it.
reauthThrottle.sleep(error, 'Authenticate')before the replay, soretries are spaced instead of a tight loop hammering both Authenticate
and the original command.
request completes and is discarded, so no reset bookkeeping is needed
beyond the request's own lifetime.
Nothing else in the middleware changes. The offline early-throw, the
shouldRetry/skipReauthenticationexemptions, the delegate-sessionrestore handling (which clears failure/finally data), and the MainQueue
replay for deprecated-API requests all keep their current behaviour -
the cap only applies to the
processWithMiddlewarereplay cycle thatloops.
Not changed:
reauthenticate()andretryReauthenticate(Authenticateitself is not the loop), SequentialQueue's own transport retry limits, and
the sign-in redirect on genuine re-authentication failure.
Tests:
New file
tests/unit/Middleware/ReauthenticationTest.ts(the middleware hasno dedicated test file today):
MAX_REAUTH_RETRIEStimes; the next 407 throwsFailed to reauthenticateon the sequential-queue path and the request isnot replayed again.
completes normally.
with the attempt's error).
skipReauthentication/ noshouldRetryis returneduntouched, as today.
What alternative solutions did you explore? (Optional)
reauthenticate(). Rejected: Authenticate is succeeding -capping it would break the normal "session expired once, re-auth, carry
on" flow and still would not stop the replay cycle, which is driven by
the replayed command's 407s, not by auth failures.
expired session is routine and self-heals today; pausing everything would
stall unrelated requests behind one command's problem.
MAX_REQUEST_RETRIES. Rejected: thatcounter governs transport-level retries of the outer request. Each
middleware replay RESOLVES with a response, so from the queue's point of
view no retry ever happens - the loop is invisible to it. The cap has to
live in the middleware.
SUSTAINED_FAILURE_THRESHOLD_COUNTinstead of a new constant.Rejected: that constant describes a failure-rate window for a different
mechanism; a named
MAX_REAUTH_RETRIESnext to the other retry limits isclearer and independently tunable.
melvin-bot commentedon Nov 16, 2026
✅ Contributor details stored successfully. Thank you for contributing to Expensify!
RajGahoi commentedon Nov 16, 2026
Contributor details
Your Expensify account email: gahoiraj01@gmail.com
Upwork Profile Link: https://www.upwork.com/freelancers/~014898525b9c53fc32
Proposal
Please re-state the problem that we are trying to solve in this issue.
When an API request (such as a queued sequential write or side-effect request) encounters an expired session (
407),Reauthentication.tsreauthenticates and immediately resends the failed request. Because the request tracks no retry count or pause, a request that repeatedly fails or triggers authentication renewal enters an infinite loop without a retry ceiling, causing a single client to flood the backend with thousands of duplicate retries.What is the root cause of that problem?
In
src/libs/Middleware/Reauthentication.ts:139-141:requestcarries no metadata tracking how many reauthentication retry attempts have been executed (reauthRetryCount).reauthenticate()succeeds,processWithMiddleware(request, isFromSequentialQueue)is immediately executed with zero backoff or rate-limiting.407or authentication edge case, this block re-triggers on every response indefinitely, consuming network bandwidth and spamming the backend.What changes do you think we should make in order to solve the problem?
src/libs/Network/types.ts(orRequest.ts):Add an optional property to the
Requesttype:src/CONST/NETWORK.ts(orCONST.ts):Define a safe retry threshold:
src/libs/Middleware/Reauthentication.ts:Before invoking
processWithMiddleware(request, isFromSequentialQueue):request.reauthRetryCount = (request.reauthRetryCount ?? 0) + 1;request.reauthRetryCount > CONST.NETWORK.MAX_REAUTH_RETRIES:await sleep(CONST.NETWORK.REAUTH_RETRY_DELAY_MS)) before callingprocessWithMiddlewareto ensure fresh session credentials propagate cleanly.What alternative solutions did you consider?
SequentialQueueerror handling: Rejected because side-effect requests outside the sequential queue also pass throughReauthentication.tsand can loop.melvin-bot commentedon Nov 16, 2026
✅ Contributor details stored successfully. Thank you for contributing to Expensify!
0xNagato commentedon Nov 16, 2026
Contributor details
Your Expensify account email: femiodunaiya@gmail.com
Upwork Profile Link: https://www.upwork.com/freelancers/~01627837e5d3874506
Proposal
Please re-state the problem that we are trying to solve in this issue.
After the session expires and the app signs back in, it resends the failed request straight away, and does it again every time the server answers 407. There is no retry limit and no wait between attempts.
What is the root cause of that problem?
After a successful
reauthenticate(),handleExpiredSessionresends queued writes and side-effect requests immediately withprocessWithMiddleware(Reauthentication.ts#L139-L141). That resend runs through the same Reauthentication middleware, so another 407 reauthenticates again (successfully) and resends again, with no end.The app's retry limits are never reached on this path:
reauthThrottlein the same file only backs off whenAuthenticateitself fails. HereAuthenticatesucceeds every time.failureData, but it only runs when the request rejects. The resend happens inside the middleware, so the queue never sees a failure.I reproduced it in a unit test on
main: withAuthenticatesucceeding and the server answering 407 thirty times, one queuedRequestMoneywrite was sent 31 times and reauthenticated 30 times in under a second.What changes do you think we should make in order to solve the problem?
MAX_REQUEST_RETRIES(10) attempts, then it gives up, appliesfailureData(the error shows on the expense) and removes the request. The new session token is used on the next attempt, so a normal expiry still succeeds on the first retry.MAKE_REQUEST_WITH_SIDE_EFFECTS, not queued): keep the resend, but count it per request (aRequestThrottleper request, e.g. in aWeakMap) so it waits with the same backoff and stops afterMAX_REQUEST_RETRIES. At the limit, resolve withUNABLE_TO_RETRY, as this middleware already does when reauthentication fails, so the caller shows its error.Log.hmmm(command and attempt count), so a stuck request shows up as one log entry instead of thousands of requests.I tried change 1 locally: the loop stops, and the existing
ReauthenticationMiddlewareTestandSequentialQueueTestsuites still pass (69 tests). I would add tests for: repeated 407 after successful reauthentication stops at the limit and appliesfailureData; a single 407 followed by success still goes through.What alternative solutions did you explore? (Optional)
A new reauth retry counter on queued requests, next to the queue's own retries. The two budgets multiply (up to 10 middleware retries for each of the queue's 10 attempts) unless they are kept in sync. Handing the request back to the queue keeps one budget and one failure path.
Reusing the module-level
reauthThrottlefor these resends. It is shared by all requests, so one stuck request would slow every other request's reauthentication, and a success elsewhere would reset its count.Code (tested locally on
main)The change, in
handleExpiredSessionafter a successfulreauthenticate()(Reauthentication.ts#L139-L141):CONST.ERROR.REAUTHENTICATED_RETRYis a new error message. SequentialQueue treats it like any other retryable error: rollback, backoff, retry, and afterMAX_REQUEST_RETRIESit gives up and appliesfailureData.Results:
RequestMoneygetting 407 thirty times was sent 31 times in under a second.MAX_REQUEST_RETRIES + 1times and resolvesUNABLE_TO_RETRY.ReauthenticationMiddlewareTest,SequentialQueueTest,SequentialQueueReadGateTestandNetworkTeststill pass (87 tests), andtscis clean.melvin-bot commentedon Nov 16, 2026
✅ Contributor details stored successfully. Thank you for contributing to Expensify!
0xNagato commentedon Nov 16, 2026
Proposal updated: added the code for changes 1 and 2 and local test results (bug reproduced, fix verified, existing network and queue suites pass). #106958 (comment)