Skip to content

Fix IP pools -> pool detail error for quick draw mcgraws - #2109

Closed
david-crespo wants to merge 2 commits into
mainfrom
ip-pool-observer
Closed

david-crespo wants to merge 2 commits into
mainfrom
ip-pool-observer

Conversation

@david-crespo

@david-crespo david-crespo commented Mar 28, 2024 •

Copy link
Copy Markdown
Collaborator

Closes #2107

At moderate psychological expense, @charliepark and I were able to determine that the following sequence of events is happening:

  1. Load IP pools list page
  2. Render useQuery fetching utilization for ip-pool-1 (an async fetch with loading state in table cell)
  3. Click ip-pool-1 to go to IP pool detail page before utilization fetch is complete
  4. Loader runs for IP pool detail, also fetching utilization for ip-pool-1
  5. Table cells on list page unmount, causing useQuery to unmount and removeObserver to get called (confirmed by adding logging inside removeObserver)
    • Call chain from component unmount to removeObserver goes from useSyncExternalStore call to onUnsubscribe to destroy
    • Unsolved: why does unmount happen before the loader is complete? Page transition should only happen after loader is done.
  6. removeObserver sees that there are no remaining observers (because prefetchQuery is not an observer) on the utilization query and cancels it, which causes a CancelledError to be thrown inside our prefetchQuery and ends it
  7. Now the loader is complete, so the detail page for ip-pool-1 renders, and the hook that expects that utilization is prefetched fails its invariant, hence the error page

Mitigations

Pretty early on I figured out that if I call cancelQueries myself on the query in question before calling prefetchQuery, that kills all the previous page stuff before it has a chance to kill me. So that avoids the error. The problem with this is that it means we start the query over again, even if we've already gotten through 90% of the fetch by the time we start the loader. This is distasteful, but I could live with it.

Once we really figured out what was going on, we were able to come up with a sneaker solution, which currently what we're doing in the diff: instead of canceling the query, manually add an observer right before we call prefetchQuery so that removeObserver does not cancel the query. This works perfectly — if the request takes 2 seconds and we wait 1.5s to click the link, the next page loads very quick — but it is somewhat ridiculous.

Solutions

One thing we could do is figure out why the unmount is happening earlier than we expect. If we can make that not happen, I think this problem goes away, because the first query doesn't get canceled until after the prefetchQuery call is complete.

Another way would be to make all prefetches behave like observers for the duration of their fetch:

diff --git a/app/api/hooks.ts b/app/api/hooks.ts
index 7e826f08..ee6c74e6 100644
--- a/app/api/hooks.ts
+++ b/app/api/hooks.ts
@@ -240,11 +240,14 @@ export const wrapQueryClient = <A extends ApiClient>(api: A, queryClient: QueryC
     params: Params<A[M]>,
     options: FetchQueryOtherOptions<Result<A[M]>, ApiError> = {}
   ) => {
+    const obs = new QueryObserver(queryClient, { queryKey: [method, params] })
+    const unsub = obs.subscribe(() => {})
     await queryClient.prefetchQuery({
       queryKey: [method, params],
       queryFn: () => api[method](params).then(handleResult(method)),
       ...options,
     })
+    unsub()
   },
   /**
    * Loader analog to `useApiQueryErrorsAllowed`. Prefetch a query that can

I doubt this is something RQ should take as a PR, it's too specific. Only way I can think of to swing it would be that prefetchQuery takes an option that makes it act as a subscriber.

Next steps

I want to talk to the maintainer about this 😅

@vercel

vercel Bot commented Mar 28, 2024

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
console ✅ Ready (Inspect) Visit Preview Mar 28, 2024 11:01pm

@david-crespo

Copy link
Copy Markdown
Collaborator Author

Superseded by #2110.

@david-crespo
david-crespo deleted the ip-pool-observer branch March 29, 2024 04:40

This branch was successfully deployed

1 active deployment
Preview — 4d1b0ac2 Deployed Mar 28, 2024 by vercel[bot]
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.

Error on nav from IP pools to IP pool detail before utilization fetch completes

1 participant