forge
Explore
Log in

Don't you dare go Hollow

✨ auto-reconnect the exam room after a dropped socket

Browse files at this commit

1edb4f31d2328e06dc60a3f9944c2c684059a457Ahmet Kaan GümüşParent: bc3b5ce
  • feat
    • ✨ auto-reconnect the exam room after a dropped socket
Show full message
feat: ✨ auto-reconnect the exam room after a dropped socket

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.tsxmodified+50 -2
@@ -94,26 +94,72 @@ export function ExamRoomWS(props: { exam: Exam }) {
94 };94 };
95 95
96 let ws: WebSocket | null = null;96 let ws: WebSocket | null = null;
97+ // Auto-reconnect a socket that drops mid-sitting: a transient network blip
98+ // shouldn't freeze the live countdown/state at "disconnected". Rejoining also
99+ // clears the backend's `left_at` (which any socket close stamps) — so on an
100+ // `allow_rejoin` exam a blip recovers cleanly; on a locked exam the rejoin
101+ // 409s, which is the intended lockout. Backoff caps the churn; the counter
102+ // resets on a successful open.
103+ let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
104+ let reconnectAttempts = 0;
105+ let closedByUs = false;
106+ const MAX_RECONNECT = 6;
107+
108+ const clearReconnect = () => {
109+ if (reconnectTimer) {
110+ clearTimeout(reconnectTimer);
111+ reconnectTimer = null;
112+ }
113+ };
97 114
98 const wsUrl = () => {115 const wsUrl = () => {
99 const proto = window.location.protocol === "https:" ? "wss:" : "ws:";116 const proto = window.location.protocol === "https:" ? "wss:" : "ws:";
100 return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`;117 return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`;
101 };118 };
102 119
120+ // Retry only while the sitting is genuinely live and we didn't close on
121+ // purpose: a submitted/expired attempt (or a client-side expiry) is terminal,
122+ // and reconnecting there would only 409.
123+ const scheduleReconnect = () => {
124+ if (closedByUs || reconnectTimer) return;
125+ if (!roomOpen()) return;
126+ if (attempt()?.status !== "in_progress" || attemptStatus() !== "in_progress") return;
127+ if (reconnectAttempts >= MAX_RECONNECT) return;
128+ const delay = Math.min(1000 * 2 ** reconnectAttempts, 15000);
129+ reconnectAttempts += 1;
130+ reconnectTimer = setTimeout(() => {
131+ reconnectTimer = null;
132+ connectWs();
133+ }, delay);
134+ };
135+
103 const connectWs = () => {136 const connectWs = () => {
104- if (ws) ws.close();137+ clearReconnect();
138+ // Detach the old socket's handlers before closing it: an intentional close
139+ // must not look like a drop and trigger a reconnect.
140+ if (ws) {
141+ ws.onclose = null;
142+ ws.onerror = null;
143+ ws.onmessage = null;
144+ ws.close();
145+ }
105 setWsState("connecting");146 setWsState("connecting");
106 const socket = new WebSocket(wsUrl());147 const socket = new WebSocket(wsUrl());
107 ws = socket;148 ws = socket;
108 149
109- socket.onopen = () => setWsState("connected");150+ socket.onopen = () => {
151+ reconnectAttempts = 0;
152+ setWsState("connected");
153+ };
110 socket.onclose = () => {154 socket.onclose = () => {
111 setWsState("disconnected");155 setWsState("disconnected");
112 ws = null;156 ws = null;
157+ scheduleReconnect();
113 };158 };
114 socket.onerror = () => {159 socket.onerror = () => {
115 setWsState("disconnected");160 setWsState("disconnected");
116 ws = null;161 ws = null;
162+ scheduleReconnect();
117 };163 };
118 164
119 socket.onmessage = (event) => {165 socket.onmessage = (event) => {
@@ -306,6 +352,8 @@ export function ExamRoomWS(props: { exam: Exam }) {
306 const goNext = () => setActiveQuestionIndex((index) => Math.min(questions().length - 1, index + 1));352 const goNext = () => setActiveQuestionIndex((index) => Math.min(questions().length - 1, index + 1));
307 353
308 onCleanup(() => {354 onCleanup(() => {
355+ closedByUs = true;
356+ clearReconnect();
309 if (ws) ws.close();357 if (ws) ws.close();
310 });358 });
311 359