Skip to content

[Due for payment 2026-03-02] Onyx write methods can execute before Onyx.init() completes, causing race conditions #81835

Closed

Description

@mountiny

Problem

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

  1. 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.
  2. 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.
  3. 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.

Work is tracked in react-native-onyx PR #727 and App PR #81207.

Issue OwnerCurrent Issue Owner: @JmillsExpensify

Activity

  1. added
    Daily
    Bug
    on Feb 9, 2026
  2. melvin-bot commented on Feb 9, 2026

    @melvin-bot

    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.

  3. adhorodyski commented on Feb 10, 2026

    @adhorodyski
    Contributor

    I will do the final testing on the Onyx PR by tomorrow so we're ready to get it merged.

  4. fabioh8010 commented on Feb 13, 2026

    @fabioh8010
    Contributor

    Updates:

  5. added
    Reviewing
    Weekly
    and removed
    Daily
    on Feb 16, 2026
  6. fabioh8010 commented on Feb 17, 2026

    @fabioh8010
    Contributor

    Updates:

  7. 3 remaining items

  8. fabioh8010 commented on Feb 20, 2026

    @fabioh8010
    Contributor

    Updates:

    • Fixed tests in the Bump PR and asked for review again.
  9. added
    Weekly
    Awaiting Payment
    and removed
    Weekly
    on Feb 23, 2026
  10. 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
  11. removed
    Reviewing
    on Feb 23, 2026
  12. melvin-bot commented on Feb 23, 2026

    @melvin-bot

    Reviewing label has been removed, please complete the "BugZero Checklist".

  13. moved this from CRITICAL to AWAITING DEPLOYMENT in [#whatsnext] #qualityon Feb 23, 2026
  14. JmillsExpensify commented on Feb 24, 2026

    @JmillsExpensify
    Contributor

    Only C+ payment is required for this issue, right? Is it happening on this issue or another one?

  15. mountiny commented on Feb 25, 2026

    @mountiny
    ContributorAuthor

    Yep only c+

  16. JmillsExpensify commented on Feb 26, 2026

    @JmillsExpensify
    Contributor

    Payment summary: $250 @mkhutornyi via new Expensify.

  17. JmillsExpensify commented on Feb 26, 2026

    @JmillsExpensify
    Contributor

    Please request!

  18. moved this from AWAITING DEPLOYMENT to Done in [#whatsnext] #qualityon Feb 26, 2026
  19. quinthar commented on Apr 4, 2026

    @quinthar
    Contributor

    Approved $250 to @mkhutornyi

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

Metadata

Metadata

Labels

Awaiting PaymentBugWeekly

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions