Repository navigation
Conversation
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
|
The last note above (a timed-out query not showing |
WilhelmZA
left a comment
There was a problem hiding this comment.
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.
useLGQuerysets noretry, and both production clients (pages/_app.tsx,context/hyperglass-provider.tsx) usenew QueryClient()with the default of three retries. Onmaineach retry reused the aborted controller and failed at once. With this change each retry gets a fresh controller and a fullrequestTimeout, so a device that does not answer is queried four times and the error shows after about four timesrequestTimeout. The tests do not show it because__tests__/render.tsxsetsretry: false. #563 fixes this. Please either setretry: falsein this PR or make sure #563 reachesmainin the same merge as this one.
Nits:
- The body has a Changelog entry but
CHANGELOG.mdis not edited. Please add it under[Unreleased], here or in the release PR.
|
Thanks, agreed on the retries. #563 (approved) is stacked on this branch and sets CHANGELOG: this goes in the release PR with the other open PRs' entries, as on #538. |
Closes #511
Problem
useLGQuerycreated oneAbortControllerper hook instance (useMemo(() => new AbortController(), [])) and passed it tofetchWithTimeouton every request.fetchWithTimeoutstartedsetTimeout(() => controller.abort(), timeout)and never cleared it. OncerequestTimeouthad 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 withAbortError.fetchWithTimeoutalso had a smaller flaw: if a caller passedoptions.signal, that signal replaced the controller's, so the timeout no longer applied.Change
util/common.ts:fetchWithTimeout(uri, options, timeout)creates its ownAbortControllerfor each call, clears the timer in afinallyoncefetchsettles, and aborts the request whenoptions.signalaborts (the listener is removed afterwards). The timeout still applies when a signal is passed, and it does not abort the caller's signal. Thecontrollerparameter is gone.hooks/use-lg-query.ts: drops the hook-wide controller and its unmount effect, and passes React Query'sctx.signalasoptions.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 thenew 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, sowaitForstill runs). Adds "gives each request its own signal". The unmount test moves to its owndescribeand no longer needs the timer capture.util/common.test.ts: newfetchWithTimeouttests 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'scommon.tsanduse-lg-query.tsand the new tests, 11 tests fail:(The
fetchWithTimeouttests onmainalso fail because the old function requires thecontrollerargument.)Partial reverts, each with the full new test set:
clearTimeoutremoved: 5 fail, including "does not abort a finished request when its timeout passes".signal: ctx.signalremoved fromuseLGQuery: 1 fails, "aborts a running request on unmount".With the fix:
biome format,biome lint,tsc --noEmitandtestpass;Tests 214 passed (214).Changelog
requestTimeouthas passed since the first query (Requery fails at once after requestTimeout because the abort controller is reused #511).Notes
fetchuntil 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 byrequestTimeout.AbortError. Each result panel has its own key (the key includesqueryLocation), so this matches the old behaviour for the results view. A remounted panel with no data fetches again, as before (@tanstack/query-core4.22shouldLoadOnMount).refetch()defaults tocancelRefetch: 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.AbortError, whose message does not start with "timeout", so the results view does not showmessages.requestTimeout(documented by the existing "aborts the request after the configured request timeout" test).