From 1edb4f31d2328e06dc60a3f9944c2c684059a457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 01:08:24 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20auto-reconnect=20the=20exam?= =?UTF-8?q?=20room=20after=20a=20dropped=20socket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A transient network blip closed the attempt WebSocket and nothing retried it: the student's live countdown and state froze at "disconnected" until they navigated away and back. Reconnect with bounded exponential backoff, gated on a genuinely live sitting (attempt still in_progress, room open, not an intentional close) so a submitted/expired attempt never triggers a pointless rejoin. A successful open resets the backoff; the old socket's handlers are detached before an intentional close so it can't masquerade as a drop. The backend already clears `left_at` on join under PRESENCE_LOCK, so on an allow_rejoin exam the blip recovers cleanly. On a locked exam the rejoin 409s (the intended lockout) and the bounded retries give up — left_at semantics stay as they are. --- src/components/exams/exam-room-ws.tsx | 52 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index b677822..c2a366c 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -94,26 +94,72 @@ export function ExamRoomWS(props: { exam: Exam }) { }; let ws: WebSocket | null = null; + // Auto-reconnect a socket that drops mid-sitting: a transient network blip + // shouldn't freeze the live countdown/state at "disconnected". Rejoining also + // clears the backend's `left_at` (which any socket close stamps) — so on an + // `allow_rejoin` exam a blip recovers cleanly; on a locked exam the rejoin + // 409s, which is the intended lockout. Backoff caps the churn; the counter + // resets on a successful open. + let reconnectTimer: ReturnType | null = null; + let reconnectAttempts = 0; + let closedByUs = false; + const MAX_RECONNECT = 6; + + const clearReconnect = () => { + if (reconnectTimer) { + clearTimeout(reconnectTimer); + reconnectTimer = null; + } + }; const wsUrl = () => { const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; }; + // Retry only while the sitting is genuinely live and we didn't close on + // purpose: a submitted/expired attempt (or a client-side expiry) is terminal, + // and reconnecting there would only 409. + const scheduleReconnect = () => { + if (closedByUs || reconnectTimer) return; + if (!roomOpen()) return; + if (attempt()?.status !== "in_progress" || attemptStatus() !== "in_progress") return; + if (reconnectAttempts >= MAX_RECONNECT) return; + const delay = Math.min(1000 * 2 ** reconnectAttempts, 15000); + reconnectAttempts += 1; + reconnectTimer = setTimeout(() => { + reconnectTimer = null; + connectWs(); + }, delay); + }; + const connectWs = () => { - if (ws) ws.close(); + clearReconnect(); + // Detach the old socket's handlers before closing it: an intentional close + // must not look like a drop and trigger a reconnect. + if (ws) { + ws.onclose = null; + ws.onerror = null; + ws.onmessage = null; + ws.close(); + } setWsState("connecting"); const socket = new WebSocket(wsUrl()); ws = socket; - socket.onopen = () => setWsState("connected"); + socket.onopen = () => { + reconnectAttempts = 0; + setWsState("connected"); + }; socket.onclose = () => { setWsState("disconnected"); ws = null; + scheduleReconnect(); }; socket.onerror = () => { setWsState("disconnected"); ws = null; + scheduleReconnect(); }; socket.onmessage = (event) => { @@ -306,6 +352,8 @@ export function ExamRoomWS(props: { exam: Exam }) { const goNext = () => setActiveQuestionIndex((index) => Math.min(questions().length - 1, index + 1)); onCleanup(() => { + closedByUs = true; + clearReconnect(); if (ws) ws.close(); });