Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): answer an overtaken list read with the newer one
Yudhi Armyndharis85decef70a10
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 29b8d16de..f2f7f4da2 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -1080,6 +1080,51 @@ test('a disconnected push keeps the QR modal when the re-read fails', async () =}});+// Another card's push starts a newer read while the disconnect's read is in flight, and the older read+// answers first and is dropped. The close decision must still get a server answer, not the pushed rows.+test('a disconnect re-read overtaken by a newer read still closes the QR modal', async () => {+ const { screen, fireEvent, within, waitFor, act } = rtl;+ resetFetchCalls();+ window.sessionStorage.setItem('openwa_api_key', 'test-key');+ const row: Session = {+ ...SESSION_QR,+ id: 'sess-overtaken-1',+ name: 'overtaken',+ status: 'qr_ready',+ engineLoaded: true,+ };+ const other: Session = { ...SESSION_QR, id: 'sess-overtaker-1', name: 'overtaker', status: 'authenticating' };+ SESSIONS.push(row, other);+ try {+ renderSessions();+ const card = (await screen.findByText('overtaken')).closest('.session-card') as HTMLElement;+ fireEvent.click(within(card).getByRole('button', { name: 'Show QR' }));+ await screen.findByAltText('QR');+ const reads = () => fetchCalls.filter(c => c.method === 'GET' && c.path === '/api/sessions').length;++ let releaseDisconnect: () => void = () => {};+ listGate = new Promise<void>(resolve => (releaseDisconnect = resolve));+ Object.assign(row, { status: 'disconnected', engineLoaded: false });+ const before = reads();+ pushSessionStatus(row.id, 'disconnected');+ await waitFor(() => assert.equal(reads(), before + 1));++ let releaseReady: () => void = () => {};+ listGate = new Promise<void>(resolve => (releaseReady = resolve));+ Object.assign(other, { status: 'ready' });+ pushSessionStatus(other.id, 'ready');+ await waitFor(() => assert.equal(reads(), before + 2));++ releaseDisconnect();+ await act(() => new Promise<void>(resolve => setTimeout(resolve, 20)));+ releaseReady();+ await waitFor(() => assert.ok(!screen.queryByRole('dialog'), 'the QR modal stayed open with no engine left'));+ } finally {+ SESSIONS.pop();+ SESSIONS.pop();+ }+});+// Two reads of the list can be in flight at once, one per status push, and nothing makes them answer// in the order they were sent. The older one must not put its snapshot back over the newer.test('a list read that answers after a newer one does not overwrite it', async () => {diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex 31dd66051..d0dd92312 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -117,11 +117,13 @@ export function Sessions() {// read is applied only if no newer read has been applied and no row was written since it started;// `rowWrites` counts those writes. A read that loses to a write is sent again, unless a newer read// is already on its way, so what it would have brought (a restriction, the rows after a socket gap)- // still arrives. A dropped read returns the newest rows the page holds, which is what its callers- // must decide on.+ // still arrives. A read dropped for a newer applied one returns the rows the page holds; one dropped+ // while a newer read is in flight answers with that read (`latestList`), so a caller deciding on+ // server fields (the disconnect handler's engine check) never decides on rows a push wrote.const listRequest = useRef(0);const listApplied = useRef(0);const rowWrites = useRef(0);+ const latestList = useRef<Promise<Session[]>>(Promise.resolve([]));// Mirror the latest sessions in a ref so the WS handler can compare against the current status without// depending on `sessions` (which would churn the callback identity and re-subscribe the socket). Kept@@ -131,7 +133,7 @@ export function Sessions() {sessionsRef.current = sessions;}, [sessions]);- const fetchSessions = useCallback(async (): Promise<Session[]> => {+ const readSessions = useCallback(async (): Promise<Session[]> => {listReadFailed.current = false;let request = 0;try {@@ -146,7 +148,8 @@ export function Sessions() {data = await sessionApi.list();if (request < listApplied.current) return sessionsRef.current;if (rowWrites.current === writes) break;- if (request !== listRequest.current) return sessionsRef.current;+ // Only the most recently started read can hold the newest request, so this is never itself.+ if (request !== listRequest.current) return latestList.current;}listApplied.current = request;sessionsRef.current = data;@@ -175,6 +178,7 @@ export function Sessions() {setLoading(false);}}, [t, queryClient]);+ const fetchSessions = useCallback(() => (latestList.current = readSessions()), [readSessions]);const {qrData,