Skip to content

Ref count misassumption #1631

Description

@willbowdler-cc
  • I've validated the bug against the latest version of DB packages

Describe the bug
A clear and concise description of what the bug is.

We are occasionally seeing our eagerly loaded collections cleared, but they will retain a ready state.

To Reproduce
Steps to reproduce the behavior:
I'm new to the codebase, so I don't have a comprehensive understanding of how everything works. This has been intermittent for us, but I believe what is happening is:

  1. Subscribers to the underlying collection query unsub to where there are no subs
  2. cleanupQueryIfIdle runs and sees that there is still a refcount of 1 since when you create an eager collection this code runs.
        queryRefCounts.set(
          hashedQueryKey,
          (queryRefCounts.get(hashedQueryKey) || 0) + 1,
        )
    
  3. Since there is a ref count and no listeners, db is removing the data from the collection under the assumption that this will prevent memory leaks.

Even if refcount > 0, we treat hasListeners as authoritative to prevent leaks. This can happen if subscriptions are GC'd without calling unloadSubset.

But refcount can also be 1 if you created the collection as an eager collection

  1. But for us this somehow leads to collection.status = 'ready' and collection.size = 0

Expected behavior
A clear and concise description of what you expected to happen.

I don't expect the collection to clear like this after sitting idle

Screenshots
If applicable, add screenshots to help explain your problem.

OS: [e.g. iOS] Tahoe 26.5
Browser [e.g. chrome, safari]
Brave 1.90.124 (Official Build) (arm64)
Chromium: 148.0.7778.179
Smartphone (please complete the following information):
N/A
Additional context
Add any other context about the problem here.

If it would be helpful, I had AI write up an explanation as well of what the suspected issue is. This may make more sense
https://gist.github.com/willbowdler-cc/3448aba0e8fd554f6c33c5fdd7cd80e1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions