Skip to content

[Due for payment 2025-09-04] [$250] [Onyx] Fix infinite loading issue for empty collections in useOnyx #68136

Closed

Description

@kacper-mikolajczak

Problem

The useOnyx hook is experiencing an infinite loading state when subscribing to collection keys that don't exist in storage.

Solution

Remove conditional check from subscribeToKey that prevents collections to be treated equally as other keys in terms of empty state identification (nullishStorageKeys). (source)

Draft PR

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021954839604653186908
  • Upwork Job ID: 1954839604653186908
  • Last Price Increase: 2025-08-11
Issue OwnerCurrent Issue Owner: @Christinadobrzyn

Activity

  1. kacper-mikolajczak commented on Aug 7, 2025

    @kacper-mikolajczak
    ContributorAuthor

    Explanation

    The problem stemmed from the subscribeToKey function in OnyxUtils.ts. When a connection is made to a key that doesn't exist in storage (returns no matching keys), the system was supposed to:

    1. Add the key to the nullish storage keys set via cache.addNullishStorageKey(mapping.key)
    2. Send a null value to the connection to indicate no data exists
    3. Allow useOnyx to transition from loading to loaded state

    However, the original code had a conditional check that prevented collection keys from being added to the nullish storage keys set:

    if (matchingKeys.length === 0) {
        if (mapping.key && !isCollectionKey(mapping.key)) {  // ❌ This condition excluded collections
            cache.addNullishStorageKey(mapping.key);
        }
        // ...
    }

    The useOnyx hook determines fetchStatus based on several factors, including whether there's cache available for a key:

    const hasCacheForKey = OnyxCache.hasCacheForKey(key);

    The hasCacheForKey method returns true if either:

    • The key has actual cached data, OR
    • The key is in the nullish storage keys set
    hasCacheForKey(key: OnyxKey): boolean {
        return this.storageMap[key] !== undefined || this.hasNullishStorageKey(key);
    }
    1. When useOnyx subscribes to a non-existent collection key
    2. subscribeToKey finds no matching keys (matchingKeys.length === 0)
    3. Due to the conditional check, collection keys were NOT added to nullish storage keys
    4. hasCacheForKey returns false for the collection key
    5. useOnyx.getSnapshot() continues to return loading status indefinitely
    6. The hook never transitions to loaded state, causing infinite loading

    This fix resolves a critical UX issue where useOnyx would get stuck in an infinite loading state for non-existent collections. The solution is minimal, consistent, and aligns the behavior of collection keys with individual keys. The change ensures that all Onyx keys, regardless of type, can properly transition through the loading lifecycle, providing a better developer experience and preventing UI freezes in applications using collection subscriptions.

    Now tests that were added to monitor this regression (source) are passing just fine.

  2. fabioh8010 commented on Aug 7, 2025

    @fabioh8010
    Contributor
  3. added
    Daily
    Bug
    on Aug 7, 2025
  4. melvin-bot commented on Aug 7, 2025

    @melvin-bot

    Triggered auto assignment to @Christinadobrzyn (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.

  5. kacper-mikolajczak commented on Aug 7, 2025

    @kacper-mikolajczak
    ContributorAuthor

    Do we have an ability to re-run tests from E/App for changes in react-native-onyx in CI? I've noticed that there were couple of test suites which process was randomly terminated on my machine (dunno why) and I would like to be certain. Thanks!

  6. kacper-mikolajczak commented on Aug 8, 2025

    @kacper-mikolajczak
    ContributorAuthor

    We are going to run the App tests when bumping Onyx, but that's a little bit late in the development pipeline and was wondering if we could run them earlier.

  7. kacper-mikolajczak commented on Aug 11, 2025

    @kacper-mikolajczak
    ContributorAuthor

    When locally running App tests, we've got two tests failing in DateUtilsTest.ts and UnreadIndicatorsTest.tsx but Fabio said they are known to be flaky. Shall we proceed @mountiny?

  8. mountiny commented on Aug 11, 2025

    @mountiny
    Contributor

    yeah thoe are sometimes flaky, I think we can proceed

  9. added
    External
    on Aug 11, 2025
  10. 30 remaining items

  11. added
    Awaiting Payment
    on Aug 28, 2025
  12. changed the title [-][$250] [Onyx] Fix infinite loading issue for empty collections in useOnyx[/-] [+][Due for payment 2025-09-04] [$250] [Onyx] Fix infinite loading issue for empty collections in useOnyx[/+] on Aug 28, 2025
  13. removed
    Reviewing
    on Aug 28, 2025
  14. melvin-bot commented on Aug 28, 2025

    @melvin-bot

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

  15. melvin-bot commented on Aug 28, 2025

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 9.1.99-11 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2025-09-04. 🎊

    For reference, here are some details about the assignees on this issue:

  16. melvin-bot commented on Aug 28, 2025

    @melvin-bot

    @DylanDylann @Christinadobrzyn The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed. Please copy/paste the BugZero Checklist from here into a new comment on this GH and complete it. If you have the K2 extension, you can simply click: [this button]

  17. added
    Daily
    and removed
    Weekly
    on Sep 3, 2025
  18. Christinadobrzyn commented on Sep 4, 2025

    @Christinadobrzyn
    Contributor

    Payment summary here - #68136 (comment)

    @DylanDylann do we need a regression test?

  19. DylanDylann commented on Sep 4, 2025

    @DylanDylann
    Contributor

    I think no, the PR is to fix UTs only (see here)

  20. melvin-bot commented on Sep 4, 2025

    @melvin-bot

    Payment Summary

    Upwork Job

    BugZero Checklist (@Christinadobrzyn)

    • (if NewFeature) I have created a PR for any necessary HelpDot updates, or I confirmed no updates are necessary.
    • I have verified the correct assignees and roles are listed above and updated the necessary manual offers
    • I have verified that there are no duplicate or incorrect contracts on Upwork for this job (https://www.upwork.com/ab/applicants/1954839604653186908/hired)
    • I have verified the PR was not reverted
    • I have applied any discounts due to bugs/regressions introduced by this PR
    • I have paid out the Upwork contracts or cancelled the ones that are incorrect
    • I have verified the payment summary above is correct
  21. Christinadobrzyn commented on Sep 5, 2025

    @Christinadobrzyn
    Contributor

    Payment summary here - #68136 (comment)

    @DylanDylann please make sure to request payment via ND. Closing, thanks everyone!

  22. garrettmknight commented on Oct 29, 2025

    @garrettmknight
    Contributor

    $250 approved for @DylanDylann

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

Metadata

Metadata

Labels

Awaiting PaymentBugDailyExternal

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions