Repository navigation
[No QA] fix: full reconnects on Reconnect/OpenApp with a newer cutoff #95497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 15 commits into
Expensify:main
from
callstack-internal:fix/record-full-reconnect-time-from-response
Jul 17, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7e16b17
fix: record the full-reconnect time from the cutoff the response deli…
adhorodyski 0e8c6b7
fix: replace unsafe type assertions with typeof guards and typed lite…
adhorodyski 035ff77
docs+test: pin the merge-ordering assumption and cover the write-queu…
adhorodyski 571f9b9
Merge branch 'Expensify:main' into fix/record-full-reconnect-time-fro…
adhorodyski e5e3a65
style: use CONST.JSON_CODE.SUCCESS instead of the raw 200 literal
adhorodyski 3860ba5
Merge branch 'Expensify:main' into fix/record-full-reconnect-time-fro…
adhorodyski 0db0475
Merge branch 'Expensify:main' into fix/record-full-reconnect-time-fro…
adhorodyski d0dce84
refactor: record the reconnect time solely from the response, clamped…
adhorodyski ef5a55e
docs: shorten the new comments
adhorodyski ba0a2c7
docs: drop the middleware comment, the code says it
adhorodyski 858293b
docs: drop comments the code already says
adhorodyski d415740
docs: restore the connectWithoutView justification comment
adhorodyski 5e809a1
Merge branch 'Expensify:main' into fix/record-full-reconnect-time-fro…
adhorodyski 87cefc9
docs: explain why the middleware exists
adhorodyski ffe8192
Merge branch 'Expensify:main' into fix/record-full-reconnect-time-fro…
adhorodyski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {AnyOnyxUpdate} from '@src/types/onyx/Request'; | ||
|
|
||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import DateUtils from './DateUtils'; | ||
|
|
||
| /** | ||
|
|
@@ -10,6 +15,22 @@ import DateUtils from './DateUtils'; | |
| * so comparing them as strings is correct. | ||
| */ | ||
|
|
||
| // The cutoff the client currently holds. Consumers that only need the value (not the change event, | ||
| // which subscribeToFullReconnect owns) read it through getServerReconnectCutoff instead of opening | ||
| // their own connection. Nothing in the UI shows it, so connectWithoutView is correct here. Do not | ||
| // copy this into a component: use useOnyx there so the UI updates when the value changes. | ||
| let currentServerReconnectCutoff = ''; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE, | ||
| callback: (value) => { | ||
| currentServerReconnectCutoff = value ?? ''; | ||
| }, | ||
| }); | ||
|
|
||
| function getServerReconnectCutoff(): string { | ||
| return currentServerReconnectCutoff; | ||
| } | ||
|
|
||
| /** | ||
| * An empty last reconnect time means the app has never reconnected, so this returns true. An empty | ||
| * cutoff means the server has not asked for one, so this returns false. | ||
|
|
@@ -32,4 +53,25 @@ function getLastFullReconnectTimeToRecord(serverReconnectCutoff: string): string | |
| return now >= serverReconnectCutoff ? now : serverReconnectCutoff; | ||
| } | ||
|
|
||
| export {shouldTriggerFullReconnect, getLastFullReconnectTimeToRecord}; | ||
| /** | ||
| * The response can deliver a newer cutoff than the one known when the request was built, and the | ||
| * held cutoff can be newer still (a Pusher update can overtake an in-flight response), so the | ||
| * recorded time satisfies whichever of the two is later. A time below either would read as stale | ||
| * and fire an extra reconnect right after the full download. | ||
| * | ||
| * The entry goes right before the delivered cutoff entry, so the reconnect subscription never sees | ||
| * a new cutoff next to an old reconnect time. That assumes Onyx broadcasts a batch's merges in | ||
| * array order: true today because both keys are cache-resident (see subscribeToFullReconnect.ts), | ||
| * and pinned by the SubscribeToFullReconnect e2e test. If it ever broke, the worst case is one | ||
| * transient extra reconnect, never a loop. | ||
|
Comment on lines
+62
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| */ | ||
| function recordFullReconnectTimeFromResponse(responseOnyxData: AnyOnyxUpdate[], knownServerReconnectCutoff: string): void { | ||
| const cutoffIndex = responseOnyxData.findIndex((update) => update.key === ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE); | ||
| const deliveredCutoffValue: unknown = cutoffIndex === -1 ? undefined : responseOnyxData.at(cutoffIndex)?.value; | ||
| const deliveredCutoff = typeof deliveredCutoffValue === 'string' ? deliveredCutoffValue : ''; | ||
| const cutoffToSatisfy = deliveredCutoff > knownServerReconnectCutoff ? deliveredCutoff : knownServerReconnectCutoff; | ||
| const insertionIndex = cutoffIndex === -1 ? responseOnyxData.length : cutoffIndex; | ||
| responseOnyxData.splice(insertionIndex, 0, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.LAST_FULL_RECONNECT_TIME, value: getLastFullReconnectTimeToRecord(cutoffToSatisfy)}); | ||
| } | ||
|
|
||
| export {shouldTriggerFullReconnect, getLastFullReconnectTimeToRecord, getServerReconnectCutoff, recordFullReconnectTimeFromResponse}; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import {isFullDownloadRequest} from '@libs/actions/RequestConflictUtils'; | ||
| import {getServerReconnectCutoff, recordFullReconnectTimeFromResponse} from '@libs/FullReconnectUtils'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import type {AnyOnyxUpdate} from '@src/types/onyx/Request'; | ||
|
|
||
| import type Middleware from './types'; | ||
|
|
||
| /** | ||
| * Records LAST_FULL_RECONNECT_TIME on every successful full-download (OpenApp/full-ReconnectApp) | ||
| * response. The time is recorded from the response rather than computed when the request is | ||
| * built, because the response can itself deliver a newer reconnect cutoff — a build-time value | ||
| * can land below it and fire extra full reconnects right after downloading everything. | ||
| */ | ||
| const recordFullReconnectTime: Middleware = (requestResponse, request) => | ||
|
adhorodyski marked this conversation as resolved.
Resolved
|
||
| requestResponse.then((response) => { | ||
| if (!isFullDownloadRequest(request) || response?.jsonCode !== CONST.JSON_CODE.SUCCESS) { | ||
| return response; | ||
| } | ||
|
|
||
| response.onyxData ??= []; | ||
| recordFullReconnectTimeFromResponse(response.onyxData as AnyOnyxUpdate[], getServerReconnectCutoff()); | ||
| return response; | ||
| }); | ||
|
|
||
| export default recordFullReconnectTime; | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import DateUtils from '@libs/DateUtils'; | ||
| import recordFullReconnectTime from '@libs/Middleware/RecordFullReconnectTime'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type Request from '@src/types/onyx/Request'; | ||
| import type {AnyOnyxUpdate} from '@src/types/onyx/Request'; | ||
| import type Response from '@src/types/onyx/Response'; | ||
|
|
||
| import type {OnyxKey} from 'react-native-onyx'; | ||
|
|
||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| const CLIENT_NOW = '2026-06-12 10:00:00.000'; | ||
| const DELIVERED_CUTOFF = '2026-06-12 10:05:00.000'; | ||
|
|
||
| function cutoffEntry(cutoff: string): AnyOnyxUpdate { | ||
| return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE, value: cutoff}; | ||
| } | ||
|
|
||
| function recordedTimeEntry(time: string): AnyOnyxUpdate { | ||
| return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.LAST_FULL_RECONNECT_TIME, value: time}; | ||
| } | ||
|
|
||
| function buildRequest(command: string, data: Record<string, unknown> = {}): Request<OnyxKey> { | ||
| return {command, data}; | ||
| } | ||
|
|
||
| function buildResponse(jsonCode: number): Response<OnyxKey> { | ||
| return {jsonCode, onyxData: [cutoffEntry(DELIVERED_CUTOFF)]}; | ||
| } | ||
|
|
||
| describe('RecordFullReconnectTime middleware', () => { | ||
| beforeEach(() => { | ||
| jest.spyOn(DateUtils, 'getDBTime').mockReturnValue(CLIENT_NOW); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test.each(['OpenApp', 'ReconnectApp'])('records the reconnect time from the cutoff a %s response delivers', async (command) => { | ||
| const request = buildRequest(command); | ||
| const response = buildResponse(200); | ||
|
|
||
| await recordFullReconnectTime(Promise.resolve(response), request, false); | ||
|
|
||
| expect(response.onyxData).toEqual([recordedTimeEntry(DELIVERED_CUTOFF), cutoffEntry(DELIVERED_CUTOFF)]); | ||
| }); | ||
|
|
||
| it('records nothing for a partial ReconnectApp, one that fetches from an update ID', async () => { | ||
| const request = buildRequest('ReconnectApp', {updateIDFrom: 123}); | ||
| const response = buildResponse(200); | ||
|
|
||
| await recordFullReconnectTime(Promise.resolve(response), request, false); | ||
|
|
||
| expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]); | ||
| }); | ||
|
|
||
| it('records client-now when a successful response carries no onyxData at all', async () => { | ||
| const request = buildRequest('OpenApp'); | ||
| const response: Response<OnyxKey> = {jsonCode: 200}; | ||
|
|
||
| await recordFullReconnectTime(Promise.resolve(response), request, false); | ||
|
|
||
| expect(response.onyxData).toEqual([recordedTimeEntry(CLIENT_NOW)]); | ||
| }); | ||
|
|
||
| test.each(['OpenReport', 'GetMissingOnyxMessages'])('leaves a %s response untouched', async (command) => { | ||
| const request = buildRequest(command, {updateIDFrom: 5, updateIDTo: 9}); | ||
| const response = buildResponse(200); | ||
|
|
||
| await recordFullReconnectTime(Promise.resolve(response), request, false); | ||
|
|
||
| expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]); | ||
| }); | ||
|
|
||
| it('leaves failed responses untouched', async () => { | ||
| const request = buildRequest('OpenApp'); | ||
| const response = buildResponse(407); | ||
|
|
||
| await recordFullReconnectTime(Promise.resolve(response), request, false); | ||
|
|
||
| expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]); | ||
| }); | ||
|
|
||
| it('passes the response through unchanged as the middleware result', async () => { | ||
| const request = buildRequest('OpenApp'); | ||
| const response = buildResponse(200); | ||
|
|
||
| await expect(recordFullReconnectTime(Promise.resolve(response), request, false)).resolves.toBe(response); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.