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
[Due for payment 2026-03-02] Onyx write methods can execute before Onyx.init() completes, causing race conditions #81835
Onyx has a race condition where write methods execute before Onyx.init() has completed, potentially corrupting or losing data.
How it works today
Onyx.init() initializes the storage backend, registers keys, loads initial key states from persistent storage, and resolves a deferredInitTask promise when done (lib/Onyx.ts L34-79).
Read subscriptions (Onyx.connect / useOnyx) already wait for init to complete -- they chain on deferredInitTask.promise before delivering values (lib/OnyxUtils.ts L1117).
Write methods (set, multiSet, merge, mergeCollection, clear, update, setCollection) do NOT wait for init. They execute immediately against whatever storage state exists, which may be uninitialized.
What can go wrong
Writes against uninitialized storage -- if Onyx.set() or Onyx.merge() is called before Onyx.init() finishes loading existing values from storage, the write may operate on empty/stale cache state.
Race between init and writes -- the init process loads default key states and existing persisted data; a concurrent write could overwrite or conflict with this loading.
Data loss -- values written before init may be silently overwritten when init's initializeWithDefaultKeyStates() runs and populates the cache.
In the App, Onyx.init() is called in src/setup/index.ts during app bootstrap. While in normal app flow the init is fast enough that writes typically follow it, there is no guarantee -- especially in headless JS contexts (Android push notifications), slow storage, or during tests.
Solution
Add an afterInit() utility in OnyxUtils that gates execution on deferredInitTask -- if Onyx is already initialized it runs the action immediately (same microtask, critical for performance and React batching), otherwise it chains on the init promise. Wrap all Onyx write methods (set, multiSet, merge, mergeCollection, clear, update, setCollection) with afterInit() so they automatically defer until init completes.
Additionally, fix App test files that are missing Onyx.init() calls, which would now cause them to hang since writes wait for init that never comes.
changed the title [-]Onyx write methods can execute before Onyx.init() completes, causing race conditions[/-][+][Due for payment 2026-03-02] Onyx write methods can execute before Onyx.init() completes, causing race conditions[/+]on Feb 23, 2026
Activity
melvin-bot commentedon Feb 9, 2026
Triggered auto assignment to @JmillsExpensify (
Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.adhorodyski commentedon Feb 10, 2026
I will do the final testing on the Onyx PR by tomorrow so we're ready to get it merged.
fabioh8010 commentedon Feb 13, 2026
Updates:
fabioh8010 commentedon Feb 17, 2026
Updates:
3 remaining items
fabioh8010 commentedon Feb 20, 2026
Updates:
[-]Onyx write methods can execute before Onyx.init() completes, causing race conditions[/-][+][Due for payment 2026-03-02] Onyx write methods can execute before Onyx.init() completes, causing race conditions[/+]melvin-bot commentedon Feb 23, 2026
Reviewinglabel has been removed, please complete the "BugZero Checklist".JmillsExpensify commentedon Feb 24, 2026
Only C+ payment is required for this issue, right? Is it happening on this issue or another one?
mountiny commentedon Feb 25, 2026
Yep only c+
JmillsExpensify commentedon Feb 26, 2026
Payment summary: $250 @mkhutornyi via new Expensify.
JmillsExpensify commentedon Feb 26, 2026
Please request!
quinthar commentedon Apr 4, 2026
Approved $250 to @mkhutornyi