Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c115f13
feat: Phase 1 - Terminal reconnection foundation
blink-so[bot] Jul 1, 2025
c10349b
fix: Improve useRetry hook logic
blink-so[bot] Jul 1, 2025
3f45b74
fix: Complete useRetry hook implementation and tests
blink-so[bot] Jul 1, 2025
834b2e5
style: Apply biome formatting fixes to useRetry hook
blink-so[bot] Jul 1, 2025
d398265
fix: Use window.setTimeout/setInterval for browser compatibility
blink-so[bot] Jul 1, 2025
dd7adda
refactor: consolidate useRetry state with useReducer
blink-so[bot] Jul 1, 2025
5766fc0
Reset TerminalPage files to main branch state
blink-so[bot] Jul 2, 2025
b1e453b
Add useWithRetry hook for simplified retry functionality
blink-so[bot] Jul 2, 2025
d4326fb
Clean up useWithRetry hook implementation
blink-so[bot] Jul 2, 2025
8323192
Remove useRetry hook and replace with useWithRetry
blink-so[bot] Jul 2, 2025
3022566
Refactor useWithRetry hook according to specifications
blink-so[bot] Jul 2, 2025
bde014c
Preserve attemptCount when max attempts reached
blink-so[bot] Jul 2, 2025
cb363db
Fix formatting
BrunoQuaresma Jul 2, 2025
55036a4
Fix hook and tests
BrunoQuaresma Jul 2, 2025
000f0e4
feat(hooks): remove max attempts limit from useWithRetry hook
BrunoQuaresma Jul 3, 2025
f9832c0
refactor(hooks): remove attemptCount from useWithRetry state and rena…
BrunoQuaresma Jul 3, 2025
4fd4885
fix(hooks): update useWithRetry tests for nextRetryAt API and add use…
BrunoQuaresma Jul 3, 2025
7b14acd
fix(hooks): prevent race condition in useWithRetry after unmount
BrunoQuaresma Jul 3, 2025
323f6ba
Fix formatting
BrunoQuaresma Jul 3, 2025
062bfa5
fix(hooks): prevent duplicate calls to useWithRetry while loading
BrunoQuaresma Jul 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(hooks): prevent race condition in useWithRetry after unmount
Add mountedRef to track component mount state and prevent:
- setState calls after component unmount
- Scheduling new retry timeouts when async operations complete after unmount

This fixes a memory leak where in-flight async operations could schedule
new retries even after the component was unmounted.

Changes:
- Add mountedRef.current checks before all setState calls
- Add mountedRef.current checks before scheduling timeouts
- Set mountedRef.current = false in cleanup
- Add test to verify fix prevents retries after unmount

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
  • Loading branch information
BrunoQuaresma and claude committed Jul 3, 2025
commit 7b14acda642a73ba3a8d382046057e239dd1c375
34 changes: 34 additions & 0 deletions site/src/hooks/useWithRetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,38 @@ describe("useWithRetry", () => {
// Function should not have been called again
expect(mockFn).toHaveBeenCalledTimes(1);
});

it("should prevent scheduling retries when function completes after unmount", async () => {
let rejectPromise: (error: Error) => void;
const promise = new Promise<void>((_, reject) => {
rejectPromise = reject;
});
mockFn.mockReturnValue(promise);

const { result, unmount } = renderHook(() => useWithRetry(mockFn));

// Start the call - this will make the function in-flight
act(() => {
result.current.call();
});

expect(result.current.isLoading).toBe(true);

// Unmount while function is still in-flight
unmount();

// Function completes with error after unmount
await act(async () => {
rejectPromise!(new Error("Failed after unmount"));
await promise.catch(() => {}); // Suppress unhandled rejection
});

// Advance time to ensure no retry timers were scheduled
await act(async () => {
jest.advanceTimersByTime(5000);
});

// Function should only have been called once (no retries after unmount)
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
15 changes: 14 additions & 1 deletion site/src/hooks/useWithRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
});

const timeoutRef = useRef<number | null>(null);
const mountedRef = useRef(true);

const clearTimeout = useCallback(() => {
if (timeoutRef.current) {
Expand All @@ -43,15 +44,23 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
clearTimeout();

const executeAttempt = async (attempt: number): Promise<void> => {
if (!mountedRef.current) {
return;
}
setState({
isLoading: true,
nextRetryAt: undefined,
});

try {
await stableFn();
setState({ isLoading: false, nextRetryAt: undefined });
if (mountedRef.current) {
setState({ isLoading: false, nextRetryAt: undefined });
}
} catch (error) {
if (!mountedRef.current) {
return;
}
const delayMs = Math.min(
DELAY_MS * MULTIPLIER ** attempt,
MAX_DELAY_MS,
Expand All @@ -63,6 +72,9 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
});

timeoutRef.current = window.setTimeout(() => {
if (!mountedRef.current) {
return;
}
setState({
isLoading: false,
nextRetryAt: undefined,
Expand All @@ -77,6 +89,7 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {

useEffect(() => {
return () => {
mountedRef.current = false;
clearTimeout();
};
}, [clearTimeout]);
Expand Down
Loading