Skip to content

fix(ui): don't resend a looking-glass query on its own - #563

Open
jsenecal wants to merge 2 commits into
fix/511-abort-controllerfrom
fix/546-query-retry
Open

jsenecal wants to merge 2 commits into
fix/511-abort-controllerfrom
fix/546-query-retry

Conversation

@jsenecal

Copy link
Copy Markdown
Collaborator

Stacked on #536 (#511); review that first. This PR is the commits after it.

Problem

Both production query clients are created with React Query's defaults (pages/_app.tsx, context/hyperglass-provider.tsx: new QueryClient()), and useLGQuery did not set retry. The default retries a query whose function throws 3 more times, waiting 1 s, 2 s and 4 s between attempts. useLGQuery's query function throws on a timeout (AbortError), a network error and a response that isn't JSON. Each retry is a new POST /api/query, and on a cache miss a new SSH or HTTP session to the device, so one failed submission could reach the device four times. A timed-out query also showed its error only after about four times requestTimeout.

A JSON error body from the backend (its 4xx/5xx answers) was never retried: the query function returns it as data.

Two other defaults also resent the query without a user action:

  • refetchOnReconnect (default true): when the browser fires online after a network drop, React Query refetches every mounted query (Query.onOnline() → observer.refetch(); with the default staleTime: 0 every query is stale). Every result panel on screen sent its query again, after a success as well as after an error.
  • retryOnMount (default true): shouldLoadOnMount fetches a query with no data again when a component using it mounts, even with refetchOnMount: false, unless the query has failed and retryOnMount is false. So a failed query would be resent when its panel remounted. No path in the app was found that remounts a failed result panel, so this one is defensive.

The existing tests did not show the retries because __tests__/render.tsx's makeQueryClient() turns retries off for every query, and no test covered reconnect or remount.

Change

  • hooks/use-lg-query.ts: sets retry: false, refetchOnReconnect: false and retryOnMount: false on the looking-glass query, and adds all three to the options a caller can't override, alongside refetchOnWindowFocus, refetchInterval and refetchOnMount. The only caller (components/results/individual.tsx) passes just onSuccess and onError. After a query has failed or finished, React Query no longer sends it again on its own; that is left to the user (the requery button, or a new submission).

  • hooks/use-lg-query.test.tsx: new useLGQuery with the default client options tests that use new QueryClient(), i.e. the production defaults, not the test helper's client. Each test checks that fetch is called once:

    • a query that times out, one whose fetch rejects, and one whose response is not JSON, with fake timers run far past requestTimeout and every default retry delay;
    • a failed query and a successful one after a window online event;
    • a failed query after its hook unmounts and mounts again on the same client.

    The retry tests read isError right after rendering, as a component does in render: React Query v4 re-renders only for result properties that have been read.

The settings are on the query, not on a client's defaultOptions, so they hold whichever client the hook runs under and leaves the other queries as they are.

The other queries on these clients

Query Reaches a device? Decision
useLGQuery (/api/query) Yes, on a cache miss retry, refetchOnReconnect and retryOnMount off (this PR)
Enrichment polling in useLGQuery (/api/query/{id}/enrichment) No; the route reads the cached query state from Redis Unchanged. It is a plain fetch in a 1 s interval, not a React Query query, so the retry default never applied. A failed poll is logged and the next tick polls again until the status is no longer pending or the panel unmounts.
useDNSQuery (configured DNS-over-HTTPS provider) No; a public resolver, 5 s timeout Unchanged. A retry (or a refetch on reconnect) is cheap and doesn't touch the device; it only resolves the target for display.
useWtf (myip.wtf, falling back to /api/myip) No; /api/myip answers from the request itself Unchanged. enabled: false, run only when the user clicks; a retry costs at most two small lookups.
useASNDetail No; returns a constant, enabled: false, and nothing calls it Unchanged.
useHyperglassConfig No Unchanged; nothing calls it (only commented-out code in components/debugger.tsx).

There are no useMutation calls.

Tests

With #536's use-lg-query.ts and the new tests, all 6 new tests fail:

 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query that times out once
AssertionError: expected "vi.fn()" to be called once, but got 4 times
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query whose request cannot be sent once
AssertionError: expected "vi.fn()" to be called once, but got 4 times
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query whose response is not JSON once
AssertionError: expected "vi.fn()" to be called once, but got 4 times
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when the browser comes back online
AssertionError: expected false to be true // Object.is equality
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a successful query when the browser comes back online
AssertionError: expected "vi.fn()" to be called once, but got 3 times
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when its panel mounts again
AssertionError: expected false to be true // Object.is equality
      Tests  6 failed | 20 passed (26)

(expected false to be true: with retries on, the query does not reach the error state while the test waits.)

Removing one option at a time, with the full new test set:

  • Only retry: false removed: 5 fail.

     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query that times out once
    AssertionError: expected "vi.fn()" to be called once, but got 4 times
     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query whose request cannot be sent once
    AssertionError: expected "vi.fn()" to be called once, but got 4 times
     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > sends a query whose response is not JSON once
    AssertionError: expected "vi.fn()" to be called once, but got 4 times
     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when the browser comes back online
    AssertionError: expected false to be true // Object.is equality
     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when its panel mounts again
    AssertionError: expected false to be true // Object.is equality
          Tests  5 failed | 21 passed (26)
    
  • Only refetchOnReconnect: false removed: 2 fail.

     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when the browser comes back online
    AssertionError: expected "vi.fn()" to be called once, but got 2 times
     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a successful query when the browser comes back online
    AssertionError: expected "vi.fn()" to be called once, but got 2 times
          Tests  2 failed | 24 passed (26)
    
  • Only retryOnMount: false removed: 1 fails.

     FAIL  hooks/use-lg-query.test.tsx > useLGQuery with the default client options > does not resend a failed query when its panel mounts again
    AssertionError: expected "vi.fn()" to be called once, but got 2 times
          Tests  1 failed | 25 passed (26)
    

With the fix: biome format, biome lint, tsc --noEmit and test pass; Tests 220 passed (220).

Changelog

  • A looking-glass query that times out or fails on the network is no longer retried three times, and its error shows after requestTimeout. Result panels no longer send their query again when the browser comes back online after a network drop, or when a failed result mounts again. Once a query has failed or finished, it is sent again only when the user requeries or submits again (Failed looking-glass queries are retried three times, reaching the device four times #546).

Notes

  • Two query clients: _app.tsx wraps the app in a QueryClientProvider with its own new QueryClient(), and HyperglassProvider, inside it, wraps its children in a second QueryClientProvider with the exported queryClient. React context resolves to the nearest provider, and every query hook runs below HyperglassProvider (Meta, Layout and the pages are its children; HyperglassProvider itself calls no query hook), so the app uses only hyperglass-provider.tsx's client. use-form-state.ts imports that same client to remove cached queries. The client in _app.tsx is never used. Not changed here, since the fix doesn't depend on which client runs the query; removing the unused one would be a separate cleanup.
  • Not changed here: a timed-out query still errors with AbortError, so the results view does not show messages.requestTimeout (noted in fix(ui): give each query request its own abort controller and timeout #536).

Closes #546

Both production query clients use React Query's defaults, so a
looking-glass query whose request timed out, failed on the network or
returned a body that isn't JSON was retried three more times with
backoff. Each attempt is a new request to the backend and a new session
on the device, so one failed submission could reach the device four
times, most often when it was already slow or unreachable.

Set `retry: false` on the query and remove `retry` from the options a
caller can pass, like the other refetch settings. Requerying stays a
user action.

The new tests use a client with the production defaults rather than the
test helper's, which turns retries off for every query.
React Query's `refetchOnReconnect` defaults to true: when the browser
fires `online` after a network drop, every mounted result panel sent its
query to the backend again, after a success as well as after an error,
with no user action.

`retryOnMount` also defaults to true, so a failed query is fetched again
when a component using it mounts again, even with `refetchOnMount: false`.
Nothing remounts a failed result panel today, so this one is defensive.

Turn both off and keep callers from overriding them, like `retry`.
@jsenecal jsenecal added bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build labels Nov 21, 2026
@jsenecal jsenecal self-assigned this Nov 21, 2026
@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 05:47
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage is 85.357% — fix/546-query-retry into fix/511-abort-controller. No base build found for fix/511-abort-controller.

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at c641ec32.

I read the diff against fix/511-abort-controller. useLGQuery now sets retry, refetchOnReconnect and retryOnMount to false and callers can no longer override them. The only caller passes onSuccess and onError, and the requery button still works through refetch(). The new tests use a QueryClient with the production defaults. CI gate passes and both commits are verified.

The base is not main, so Closes #546 does not take effect on this merge. Please retarget this PR to main after #536 merges, or close #546 by hand once the fix is on main. #536 on its own makes a timed-out query retry three times, so the two should reach main together.

Nits:

  • hyperglass/ui/hooks/use-lg-query.ts:89: the one-line // Don't retry a failed query. follows the three-line comment directly, so it is not clear that the longer comment covers all three settings. A blank line, or dropping the one-liner, would fix that.
  • The body has a Changelog entry but CHANGELOG.md is not edited. Please add it under [Unreleased], here or in the release PR.

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

Labels

bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants