Skip to content

[PB-7045] fix: run only one placeholder refresh at a time per sync context - #1604

Open
victor-ferro wants to merge 3 commits into
mainfrom
fix/BR-2033-single-refresh-pass
Open

victor-ferro wants to merge 3 commits into
mainfrom
fix/BR-2033-single-refresh-pass

Conversation

@victor-ferro

@victor-ferro victor-ferro commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What is Changed / Added

  • One refresh at a time: refreshItemPlaceholders runs at most one placeholder refresh at a time per sync context.
  • Requests that arrive during a refresh are merged into a single refresh that starts when the current one ends.
    • If one of the merged requests is the app startup one (isFirstExecution), the refresh runs as a startup refresh, so nothing that only that refresh does is lost.
    • Each request gets the result of the refresh that covers it.
  • On logout the pending refresh is not started: it returns an AbortError, which is the error recovery sync already handles.

Why

Three refreshes start at the same time on startup. They are requested by the worker startup, the remote sync and the recovery sync, with no lock between them. Each refresh loads the whole local database in memory. On big accounts this makes the app crash with out of memory in the main process.

The running refresh is not reused. Requests that arrive later may come from database changes that refresh did not see, for example the ones from recovery sync.

Tested locally with a real customer database (1.88 M rows), the real code and Electron's 4 GB heap limit. The three startup requests plus a backup were run three times:

  • with main, it crashes all 3 times;
  • with this change, it survives all 3 times: 2 refreshes that never overlap, with a peak of 2.7-2.9 GB.

What it does not solve:

  • Each refresh and each backup still load the whole database. On machines with little free RAM it can still crash. The next PR, based on this branch, makes each of them read only the files and folders under its root.

Summary by CodeRabbit

  • Bug Fixes
    • Overlapping item refreshes now run sequentially. Requests received during an active refresh are combined into a single follow-up pass.
    • The follow-up pass retains the first-execution setting if any request in the group requires it.
    • Each request receives the result of its corresponding refresh pass, and refreshes using separate contexts continue independently.
    • If a context is cancelled before a queued refresh begins, that refresh is skipped and its request receives an abort error.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

refreshItemPlaceholders now queues overlapping requests by SyncContext. Requests received during a pass merge into one follow-up pass, and callers receive the result of their corresponding pass. Tests cover merging, independent contexts, errors, and aborts.

Changes: Placeholder refresh queue

Layer / File(s) Summary
Queue behavior and validation
src/apps/sync-engine/refresh-item-placeholders.ts, src/apps/sync-engine/refresh-item-placeholders.test.ts
The refresh function runs passes sequentially per context and merges requests received during a pass. The follow-up pass uses isFirstExecution: true if any merged request supplied it. Tests cover pass results, traversal errors, context independence, and aborts.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: Minimal · up to 1c9d8

This change serializes placeholder refreshes per sync context and merges overlapping requests, which reduces memory-pressure crashes during startup on large accounts. No concrete defect was established in the reviewed change. Machines with little free RAM can still run out of memory, because each refresh still loads the whole database; the author plans a follow-up for that.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: refresh calls of refreshItemPlaceholders are now serialized so only one placeholder refresh runs at a time per sync context. The changeset queues requests per SyncCont…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: refresh calls of refreshItemPlaceholders are now serialized so only one placeholder refresh runs at a time per sync context. The changeset queues requests per SyncContext, merges pending requests into one later pass, and handles aborts, which matches the title. The ticket prefix and the conventional 'fix:' label are acceptable and do not make the title unclear.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aea33b45-ad57-4a34-99dd-a4687b877c61
Commits

Reviewing files that changed from the base of the PR and between f2f36b3 and 1c9d850.

Files selected for processing: 1

  • src/apps/sync-engine/refresh-item-placeholders.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@victor-ferro victor-ferro self-assigned this Nov 17, 2026
@victor-ferro victor-ferro changed the title fix: run only one placeholder refresh at a time per sync context [PB-7045] fix: run only one placeholder refresh at a time per sync context Nov 17, 2026
Comment on lines +57 to +61
function createAbortError() {
const error = new Error('Refresh item placeholders was aborted');
error.name = 'AbortError';
return error;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we do really need an specific funcion to create an Aborrt error, perhaps a better idea is to have a common Error type and add the "abort error" code so that we can track better this error code, perhaps we already have it. The main idea with this is that we introduce the less amount of code and reuse what we already have, this way you are introducing an error that is only being used here and we will have to maintain just for this specific case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, removed it. Logout calls ctx.abortController.abort() with no argument, so signal.reason is already the standard AbortError

};

export async function refreshItemPlaceholders({ ctx, isFirstExecution }: Props): Promise<Result<boolean, Error>> {
type RefreshResult = Result<boolean, Error>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is not really necessary no? Result<boolean, Error> is as self explanatory as it gets

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, removed it and used Result<boolean, Error> directly


/**
* v2.7.4 Victor Fernandez
* Startup, remote sync and recovery sync can ask for a refresh at the same time, and every pass loads the whole

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If im not mistaken, a recovery sync cannot run while a remote sync is in place and vice versa

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've updated the comment

@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants