Skip to content

fix(ui): give each query request its own abort controller and timeout - #536

Open
jsenecal wants to merge 1 commit into
mainfrom
fix/511-abort-controller
Open

jsenecal wants to merge 1 commit into
mainfrom
fix/511-abort-controller

Conversation

@jsenecal

Copy link
Copy Markdown
Collaborator

Closes #511

Problem

useLGQuery created one AbortController per hook instance (useMemo(() => new AbortController(), [])) and passed it to fetchWithTimeout on every request. fetchWithTimeout started setTimeout(() => controller.abort(), timeout) and never cleared it. Once requestTimeout had passed since the first request, the shared controller was aborted for good, even if that request had finished in a second. After that, the requery button or any refetch from the same result panel failed at once with AbortError.

fetchWithTimeout also had a smaller flaw: if a caller passed options.signal, that signal replaced the controller's, so the timeout no longer applied.

Change

  • util/common.ts: fetchWithTimeout(uri, options, timeout) creates its own AbortController for each call, clears the timer in a finally once fetch settles, and aborts the request when options.signal aborts (the listener is removed afterwards). The timeout still applies when a signal is passed, and it does not abort the caller's signal. The controller parameter is gone.
  • hooks/use-lg-query.ts: drops the hook-wide controller and its unmount effect, and passes React Query's ctx.signal as options.signal. React Query aborts that signal when the query is cancelled, which includes the last observer unmounting, so a running request is still aborted on unmount. The query key is unchanged.
  • hooks/use-wtf.ts, hooks/use-dns-query.ts: drop the new AbortController() argument. They already made one per call, so their behaviour does not change.
  • hooks/use-lg-query.test.tsx: the test(ui): cover the query form, form state and query hook #506 test "fails a refetch once the first request timeout has passed" asserted the bug; it is replaced by "refetches after the first request timeout has passed" and "does not abort a finished request when its timeout passes", both with fake timers (shouldAdvanceTime, so waitFor still runs). Adds "gives each request its own signal". The unmount test moves to its own describe and no longer needs the timer capture.
  • util/common.test.ts: new fetchWithTimeout tests with fake timers: timeout aborts at the configured delay, the timer is cleared on success and on failure, each call gets a new signal, a passed signal aborts the request (and an already-aborted one fails at once), and the timeout still fires when a signal is passed.

Tests

With origin/main's common.ts and use-lg-query.ts and the new tests, 11 tests fail:

 FAIL  hooks/use-lg-query.test.tsx > useLGQuery timeout > gives each request its own signal
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery timeout with a running clock > does not abort a finished request when its timeout passes
 FAIL  hooks/use-lg-query.test.tsx > useLGQuery timeout with a running clock > refetches after the first request timeout has passed
 FAIL  util/common.test.ts > fetchWithTimeout > aborts the request when the passed signal aborts
 FAIL  util/common.test.ts > fetchWithTimeout > aborts the request when the timeout passes
 FAIL  util/common.test.ts > fetchWithTimeout > clears the timer when the request fails
 FAIL  util/common.test.ts > fetchWithTimeout > clears the timer when the request succeeds
 FAIL  util/common.test.ts > fetchWithTimeout > fails at once when the passed signal has already aborted
 FAIL  util/common.test.ts > fetchWithTimeout > gives each request its own signal
 FAIL  util/common.test.ts > fetchWithTimeout > passes the URL and options to fetch
 FAIL  util/common.test.ts > fetchWithTimeout > still times out a request that has a passed signal
      Tests  11 failed | 44 passed (55)

(The fetchWithTimeout tests on main also fail because the old function requires the controller argument.)

Partial reverts, each with the full new test set:

  • Only the clearTimeout removed: 5 fail, including "does not abort a finished request when its timeout passes".
  • Only signal: ctx.signal removed from useLGQuery: 1 fails, "aborts a running request on unmount".
  • Only the forwarding of the passed signal removed: 2 fail, "aborts a running request on unmount" and "aborts the request when the passed signal aborts".

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

Changelog

Notes

  • The timer now covers fetch until the response headers arrive, not the reading of the body. The backend sends the JSON body with the response, so this matches the old behaviour in practice; a body that stalls after the headers would no longer be cut off by requestTimeout.
  • Unmount now aborts through React Query, which cancels a query only when its last observer goes away, and reverts the query to its state before the fetch instead of storing an AbortError. Each result panel has its own key (the key includes queryLocation), so this matches the old behaviour for the results view. A remounted panel with no data fetches again, as before (@tanstack/query-core 4.22 shouldLoadOnMount).
  • React Query's refetch() defaults to cancelRefetch: true, so a refetch while a request is running now aborts the running request on the network too; before, it kept running and its result was ignored. The requery button is disabled while a query runs (fix(ui): stop the copy and requery buttons acting while disabled #525), so the UI doesn't reach this.
  • Not changed here: a timed-out query still errors with AbortError, whose message does not start with "timeout", so the results view does not show messages.requestTimeout (documented by the existing "aborts the request after the configured request timeout" test).

useLGQuery kept one AbortController for the life of the hook, and
fetchWithTimeout started a timer on every request that aborted that
controller and was never cleared. Once requestTimeout had passed since
the first request, the controller stayed aborted, so the requery button
or any refetch from the same result failed at once with AbortError.

fetchWithTimeout now creates a controller per call, clears its timer
when fetch settles, and aborts the request when a signal passed in its
options aborts. useLGQuery passes React Query's signal, which React
Query aborts when the query is cancelled, such as when the last
component using it unmounts, so a running request is still aborted on
unmount. The DNS and IP lookups drop the controller argument.

Closes #511
@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 04:44
@jsenecal

Copy link
Copy Markdown
Collaborator Author

The last note above (a timed-out query not showing messages.requestTimeout) is fixed in #542, which maps the AbortError to that message in individual.tsx. Whichever of #536 and #542 merges second should update the now-stale comment in the "aborts the request after the configured request timeout" test in hooks/use-lg-query.test.tsx.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 85.357% (+0.002%) from 85.355% — fix/511-abort-controller into main

@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.

Reviewed at 4ae67fa7.

The fix for #511 is correct: each call gets its own controller, the timer is cleared when the request settles, and the caller's signal is forwarded. CI gate passes and the commit is verified.

Before approval:

  • A timed-out query is now retried. useLGQuery sets no retry, and both production clients (pages/_app.tsx, context/hyperglass-provider.tsx) use new QueryClient() with the default of three retries. On main each retry reused the aborted controller and failed at once. With this change each retry gets a fresh controller and a full requestTimeout, so a device that does not answer is queried four times and the error shows after about four times requestTimeout. The tests do not show it because __tests__/render.tsx sets retry: false. #563 fixes this. Please either set retry: false in this PR or make sure #563 reaches main in the same merge as this one.

Nits:

  • The body has a Changelog entry but CHANGELOG.md is not edited. Please add it under [Unreleased], here or in the release PR.

@WilhelmZA WilhelmZA added the reviewing Issue is being reviewed by maintainers label Nov 21, 2026
@jsenecal

Copy link
Copy Markdown
Collaborator Author

Thanks, agreed on the retries.

#563 (approved) is stacked on this branch and sets retry: false, refetchOnReconnect: false and retryOnMount: false in useLGQuery. I will merge #563 right after this one, in the same session, with nothing in between. If you'd rather main never has the retry behaviour even for a moment, I can move the retry: false line into this PR instead. Say so and I will do it.

CHANGELOG: this goes in the release PR with the other open PRs' entries, as on #538.

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.

Requery fails at once after requestTimeout because the abort controller is reused

3 participants