Skip to content

fix(admin): replace useEffect restart poll with useQuery pattern in KiloclawInstanceDetail#1881

Open
pandemicsyn wants to merge 1 commit intomainfrom
gt/birch/222ec28e
Open

fix(admin): replace useEffect restart poll with useQuery pattern in KiloclawInstanceDetail#1881
pandemicsyn wants to merge 1 commit intomainfrom
gt/birch/222ec28e

Conversation

@pandemicsyn
Copy link
Copy Markdown
Contributor

Summary

Replaced the useEffect-based polling approach for machine restart completion detection with a dedicated useQuery polling hook, matching the existing VolumeReassociatePanel pattern already used in the same file.

Changes in KiloclawInstanceDetail.tsx:

  • Removed awaitingRestartCompletion from the main data query's refetchInterval (only awaitingRestoreCompletion drives it now)
  • Added a separate 'machine-restart-poll' useQuery that polls every 3s while awaitingRestartCompletion is true, invalidating the get query on each tick
  • Replaced the useEffect + useRef (prevMachineStatus) approach with a render-time check: when awaitingRestartCompletion && data?.workerStatus?.status === 'running', sets state to false and re-invalidates controllerVersion and gatewayStatus

This ensures controllerVersion is only refetched after the machine is confirmed running, avoiding the race condition where a stale value could be cached for the full 5-minute staleTime.

Verification

  • No quality gates configured for this rig; code review performed manually.
  • Change is structurally identical to the VolumeReassociatePanel polling pattern at lines 412–431 of the same file.
  • The render-time setState pattern is consistent with existing codebase usage.

Visual Changes

N/A

Reviewer Notes

The original prevMachineStatus / wasRestarting guard prevented false positives on first render when the machine was already running. That guard is safely removed because awaitingRestartCompletion starts as false and is only set to true on mutation success — so the condition cannot fire spuriously on mount.

The poll query key includes awaitingRestartCompletion as a dependency (consistent with VolumeReassociatePanel's pattern), which causes the query to be remounted when polling stops. This is harmless and follows the established pattern.

…iloclawInstanceDetail

Replace the useEffect-based polling (which modified the main data query's
refetchInterval and used a useRef to track previous status) with a dedicated
useQuery polling hook, matching the VolumeReassociatePanel pattern.

The new approach:
- Adds a separate 'machine-restart-poll' useQuery that invalidates the get
  query every 3 seconds while isRestartPending (awaitingRestartCompletion)
- Uses a render-time check (not useEffect) to detect when the machine
  returns to 'running' and re-invalidate controllerVersion/gatewayStatus
- Removes awaitingRestartCompletion from the main data query's refetchInterval

This fixes the race condition where controllerVersion was refetched while the
machine was still mid-restart, caching a stale value for the full 5-minute
staleTime.
}),
});
}
if (awaitingRestartCompletion && data?.workerStatus?.status === 'running') {
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.

WARNING: This can stop polling before the restart actually begins

awaitingRestartCompletion is set to true in the mutation onSuccess handler before the invalidated get query has a chance to observe the machine leaving running. On the first rerender after that state update, data?.workerStatus?.status is often still the pre-restart cached value (running), so this branch clears awaitingRestartCompletion immediately and re-invalidates controllerVersion/gatewayStatus against the old machine state. The previous prevMachineStatus guard avoided this false positive; this version needs to wait until the status has been observed as non-running at least once before treating running as restart completion.

@kilo-code-bot
Copy link
Copy Markdown
Contributor

kilo-code-bot bot commented Apr 2, 2026

Code Review Summary

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
src/app/admin/components/KiloclawInstances/KiloclawInstanceDetail.tsx 1279 Restart completion can be detected immediately from stale running data, which stops polling before the restart is actually observed.

Fix these issues in Kilo Cloud

Other Observations (not in diff)

No additional issues found outside the diff.

Files Reviewed (1 files)
  • src/app/admin/components/KiloclawInstances/KiloclawInstanceDetail.tsx - 1 issue

Reviewed by gpt-5.4-20260305 · 133,002 tokens

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.

1 participant