Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-717]): guard useAsyncAction setRunning against post-unmount calls
[._.]/ Adam Eivyea3cffed1c36
diff --git a/client/src/hooks/useAsyncAction.js b/client/src/hooks/useAsyncAction.jsindex 7bffedab3d..05384446f2 100644--- a/client/src/hooks/useAsyncAction.js+++ b/client/src/hooks/useAsyncAction.js@@ -1,4 +1,4 @@-import { useState } from 'react';+import { useState, useRef, useEffect } from 'react';import toast from '../components/ui/Toast';/**@@ -15,16 +15,25 @@ import toast from '../components/ui/Toast';* error is toasted and `run` resolves to `null`. The hook only models a* single boolean in-flight flag — keyed/indexed loading states (e.g. "which* row is saving") need a different abstraction.+ *+ * The trailing `setRunning(false)` is gated on a `mountedRef` so an action+ * that resolves after the component unmounts (navigate-away mid-request)+ * doesn't call `setState` on an unmounted component — React warns about+ * that and it's a latent memory-leak signal.*/export function useAsyncAction(fn, { errorMessage } = {}) {const [running, setRunning] = useState(false);+ const mountedRef = useRef(true);+ // Never reset to `true` on re-mount — this handles dev-mode double-mount+ // cleanly (the cleanup runs once, the flag stays false for the dead tree).+ useEffect(() => () => { mountedRef.current = false; }, []);const run = async (...args) => {setRunning(true);const result = await fn(...args).catch((err) => {toast.error(err?.message || errorMessage || 'Action failed');return null;});- setRunning(false);+ if (mountedRef.current) setRunning(false);return result;};return [run, running];diff --git a/client/src/hooks/useAsyncAction.test.jsx b/client/src/hooks/useAsyncAction.test.jsxnew file mode 100644index 0000000000..cee2824286--- /dev/null+++ b/client/src/hooks/useAsyncAction.test.jsx@@ -0,0 +1,80 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';+import { renderHook, act } from '@testing-library/react';+import { useAsyncAction } from './useAsyncAction';++const errorSpy = vi.fn();+vi.mock('../components/ui/Toast', () => ({+ default: { error: (...args) => errorSpy(...args) },+}));++describe('useAsyncAction', () => {+ beforeEach(() => {+ errorSpy.mockClear();+ });++ it('toggles running around a successful action and resolves to the fn value', async () => {+ const { result } = renderHook(() => useAsyncAction(async (x) => x * 2));++ expect(result.current[1]).toBe(false);++ let resolved;+ await act(async () => {+ resolved = await result.current[0](21);+ });++ expect(resolved).toBe(42);+ expect(result.current[1]).toBe(false);+ });++ it('toasts and resolves to null when the action throws', async () => {+ const { result } = renderHook(() =>+ useAsyncAction(async () => { throw new Error('boom'); }, { errorMessage: 'fallback' })+ );++ let resolved = 'unset';+ await act(async () => {+ resolved = await result.current[0]();+ });++ expect(resolved).toBeNull();+ expect(errorSpy).toHaveBeenCalledWith('boom');+ expect(result.current[1]).toBe(false);+ });++ it('falls back to errorMessage then a default when the error has no message', async () => {+ const { result } = renderHook(() => useAsyncAction(async () => { throw {}; }, { errorMessage: 'fallback' }));+ await act(async () => { await result.current[0](); });+ expect(errorSpy).toHaveBeenCalledWith('fallback');++ const { result: bare } = renderHook(() => useAsyncAction(async () => { throw {}; }));+ await act(async () => { await bare.current[0](); });+ expect(errorSpy).toHaveBeenCalledWith('Action failed');+ });++ it('does not call setRunning after the component unmounts mid-request', async () => {+ const setStateSpy = vi.spyOn(console, 'error');+ let release;+ const pending = new Promise((resolve) => { release = resolve; });+ const { result, unmount } = renderHook(() => useAsyncAction(async () => { await pending; return 'late'; }));++ // Kick off the action so `running` flips true and the await suspends.+ let runPromise;+ act(() => { runPromise = result.current[0](); });+ expect(result.current[1]).toBe(true);++ // Unmount while the action is still pending, then let it resolve.+ unmount();+ await act(async () => {+ release();+ await runPromise;+ });++ // React logs an "update on an unmounted component" error if the guard is+ // missing — the guard keeps that console.error from ever firing.+ const sawUnmountWarning = setStateSpy.mock.calls.some((call) =>+ String(call[0]).includes('unmounted')+ );+ expect(sawUnmountWarning).toBe(false);+ setStateSpy.mockRestore();+ });+});