From f082cdc63bd0de90f8b3da1df6071e91ab545831 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Thu, 4 Jun 2026 19:50:02 +0200 Subject: [PATCH] fix(websocket): reset unmountedRef on each effect re-run so token refresh reconnects (#721) The effect cleanup sets unmountedRef.current = true to prevent reconnects after the provider unmounts. Without an inverse reset at the start of the effect, re-running the effect (e.g. when the auth token rotates) leaves the ref true, and connect() short-circuits at its unmounted guard. The socket then stays permanently disconnected for the lifetime of the provider. Co-authored-by: Claude Opus 4.7 (1M context) Co-authored-by: Haile <118998054+blackmammoth@users.noreply.github.com> --- src/contexts/WebSocketContext.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/contexts/WebSocketContext.tsx b/src/contexts/WebSocketContext.tsx index 116da6b9..456c0761 100644 --- a/src/contexts/WebSocketContext.tsx +++ b/src/contexts/WebSocketContext.tsx @@ -36,8 +36,12 @@ const useWebSocketProviderState = (): WebSocketContextType => { const { token } = useAuth(); useEffect(() => { + // The cleanup below sets unmountedRef = true. Without this reset, every + // re-run of the effect (e.g. on token refresh) would short-circuit connect() + // at its unmounted guard and leave the socket permanently disconnected. + unmountedRef.current = false; connect(); - + return () => { unmountedRef.current = true; if (reconnectTimeoutRef.current) {