Skip to content

Commit

Permalink
Merge pull request #54110 from bernhardoj/fix/52027-accounting-tab-lo…
Browse files Browse the repository at this point in the history
…ads-indefinitely

Fix accounting tab load indefinitely
  • Loading branch information
neil-marcellini authored Dec 16, 2024
2 parents 64d6c38 + 9b16045 commit 6476a49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function process(): Promise<void> {
return currentRequestPromise;
}

function flush() {
function flush(shouldResetPromise = true) {
// When the queue is paused, return early. This will keep an requests in the queue and they will get flushed again when the queue is unpaused
if (isQueuePaused) {
Log.info('[SequentialQueue] Unable to flush. Queue is paused.');
Expand All @@ -154,10 +154,12 @@ function flush() {

isSequentialQueueRunning = true;

// Reset the isReadyPromise so that the queue will be flushed as soon as the request is finished
isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
if (shouldResetPromise) {
// Reset the isReadyPromise so that the queue will be flushed as soon as the request is finished
isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
}

// Ensure persistedRequests are read from storage before proceeding with the queue
const connection = Onyx.connect({
Expand Down Expand Up @@ -196,7 +198,7 @@ function unpause() {
const numberOfPersistedRequests = PersistedRequests.getAll().length || 0;
Log.info(`[SequentialQueue] Unpausing the queue and flushing ${numberOfPersistedRequests} requests`);
isQueuePaused = false;
flush();
flush(false);
}

function isRunning(): boolean {
Expand Down Expand Up @@ -252,7 +254,7 @@ function push(newRequest: OnyxRequest) {

// If the queue is running this request will run once it has finished processing the current batch
if (isSequentialQueueRunning) {
isReadyPromise.then(flush);
isReadyPromise.then(() => flush());
return;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/unit/APITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,23 @@ describe('APITests', () => {
expect(thirdRequestCommandName).toBe('MockCommandTwo');
});
});

test('Read request should not stuck when SequentialQueue is paused an resumed', async () => {
// Given 2 WRITE requests and 1 READ request where the first write request pauses the SequentialQueue
const xhr = jest.spyOn(HttpUtils, 'xhr').mockResolvedValueOnce({previousUpdateID: 1});
API.write('MockWriteCommandOne' as WriteCommand, {});
API.write('MockWriteCommandTwo' as WriteCommand, {});
API.read('MockReadCommand' as ReadCommand, null);

await waitForBatchedUpdates();

// When the SequentialQueue is unpaused
SequentialQueue.unpause();

await waitForBatchedUpdates();

// Then the pending READ command should be called
const [thirdCommand] = xhr.mock.calls.at(2) ?? [];
expect(thirdCommand).toBe('MockReadCommand');
});
});

0 comments on commit 6476a49

Please sign in to comment.