Skip to content

fix(ui): drop superseded useApiResource responses when the path changes mid-flight#7790

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
xfodev:fix/use-api-resource-stale-guard-7785
Jul 21, 2026
Merged

fix(ui): drop superseded useApiResource responses when the path changes mid-flight#7790
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
xfodev:fix/use-api-resource-stale-guard-7785

Conversation

@xfodev

@xfodev xfodev commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

useApiResource (apps/loopover-ui/src/lib/api/use-api-resource.ts) had no cancellation guard when its path dependency changed: load awaits apiFetch and then setState unconditionally, so an older request could resolve after a newer one and silently overwrite it. Reachable via pagination (dead-letter-queue-panel offset), free-text input (owner-panel repo field), and window selection (app.analytics) — the stale response wins the race while the surrounding UI (row counts, Prev/Next, inputs) reflects the newer request.

Fix: tag each load with an incrementing request id held in a ref; after the await, drop the response if a newer load has since superseded it. This is the shared hook, so the fix resolves the class for every consumer (dead-letter-queue, owner, analytics, notification-readiness, digest, maintainer, miner, commands panels, operator/index/runs routes). No behavior change for the normal single-request path; reload is unaffected.

Tests

Adds a stale-response regression test to use-api-resource.test.ts: two in-flight loads where the older resolves last must leave the newer page in place. Full loopover-ui suite (525 tests) green; both branches of the new guard covered.

Validation

  • @loopover/ui typecheck, lint, prettier, and the full vitest suite (525 pass incl. the new case) green; rebased on latest main. Zero-visual-diff (a data-race guard in a lib/api hook — no rendered-output change).

Closes #7785

…es mid-flight

useApiResource had no cancellation guard, so when path changes (pagination
offsets, free-text repo input, analytics window selection) an older request's
response could resolve after a newer one and silently overwrite it — showing the
wrong page/repo while the surrounding UI reflects the newer request. Tag each
load with an incrementing request id and drop the result after the await if a
newer load has superseded it. Fixes the class for every useApiResource consumer.

Closes JSONbored#7785
@xfodev
xfodev requested a review from JSONbored as a code owner July 21, 2026 12:53
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 21, 2026
@loopover-orb

loopover-orb Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-21 13:04:19 UTC

2 files · 1 AI reviewer · no blockers · readiness 98/100 · CI green · clean

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This is a correct, minimal fix for a classic out-of-order-response race in the shared useApiResource hook: an incrementing requestId held in a ref is set before the await and checked after it, dropping the response if a newer load has since started. The `reload` behavior and `enabled`/error paths are preserved since the id is bumped unconditionally at the top of `load`, and the disabled branch returns before hitting the guard so it's unaffected. The regression test genuinely drives the real race (two in-flight loads via separate unresolved promises, older resolving after the newer) rather than fabricating an unreachable state, and correctly asserts the newer page's data survives the stale resolution.

Nits — 4 non-blocking
  • use-api-resource.ts: the guard doesn't reset `state` back to loading-only nor cancel the in-flight fetch itself (e.g. via AbortController) — the stale request still completes and consumes network/CPU, it's just discarded on arrival; fine for this fix's scope but worth a follow-up if these panels see heavy rapid pagination.
  • use-api-resource.ts: the three added inline comments are fairly verbose for a single guard clause; consider trimming to one line since the PR description already explains the rationale.
  • Consider wiring an AbortController alongside the requestId so superseded requests are actually cancelled rather than just ignored, reducing wasted network calls under rapid pagination/typing.
  • The stale-response test in use-api-resource.test.ts is a solid pattern — worth linking to from the PR description as the canonical regression test for other hooks with similar risk, if any exist.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #7785
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 71 registered-repo PR(s), 42 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor xfodev; Gittensor profile; 71 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The PR adds a request-id ref guard to useApiResource that drops stale responses when path/enabled/token changes mid-flight, matching the required cancelled-flag idiom's intent, and includes a regression test simulating out-of-order pagination resolution asserting the newer page wins.

Review context
  • Author: xfodev
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: JavaScript, TypeScript
  • Official Gittensor activity: 71 PR(s), 0 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Keep the PR focused and include validation evidence before maintainer review.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask <question> answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat <question> answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@loopover-orb loopover-orb Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit 2840506 into JSONbored:main Jul 21, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared useApiResource hook has no stale-response guard - reachable via pagination and free-text input

1 participant