From 3d7888f3bfd6323e2d4b50a26ff7a989ce8fa5e5 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sat, 28 Nov 2026 12:42:29 -0500 Subject: [PATCH] test(integration): the abort storm scenario, on unfixed code Throwaway branch: proves the scenario fails without the #1243 fix before it is added to that pull request. Deleted once the evidence is recorded. --- .../features/subscriptions/req-abort.feature | 13 ++++ .../subscriptions/req-abort.feature.ts | 60 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 test/integration/features/subscriptions/req-abort.feature create mode 100644 test/integration/features/subscriptions/req-abort.feature.ts diff --git a/test/integration/features/subscriptions/req-abort.feature b/test/integration/features/subscriptions/req-abort.feature new file mode 100644 index 000000000..6b0dcb166 --- /dev/null +++ b/test/integration/features/subscriptions/req-abort.feature @@ -0,0 +1,13 @@ +Feature: Aborting a subscription does not strand a database connection + + A REQ's stored-events read is a knex stream over a pg cursor, so ending it early (CLOSE, or a + socket that goes away mid-read) has to give its pooled connection back. When it did not, each + aborted read cost the worker a connection and the pool filled until every subscription hung + (#1243). Unit tests cover the wrapper's control flow against a fake of knex; this scenario is + the property itself, against a real pool. + + Scenario: Subscriptions closed before their stored events arrive + Given someone called Alice + When Alice opens and closes 25 subscriptions at once + Then no pooled database connection is in use + And no database backend is left active diff --git a/test/integration/features/subscriptions/req-abort.feature.ts b/test/integration/features/subscriptions/req-abort.feature.ts new file mode 100644 index 000000000..b83401e68 --- /dev/null +++ b/test/integration/features/subscriptions/req-abort.feature.ts @@ -0,0 +1,60 @@ +import { Then, When, World } from '@cucumber/cucumber' +import { expect } from 'chai' +import { WebSocket } from 'ws' + +import { getMasterDbClient } from '../../../../src/database/client' + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +/** + * The abort storm behind #1243: every aborted read used to cost the worker a pooled connection, + * and a pool of 2 (the integration stack's) is full after a couple of them. Aborts are spread + * across two positions — a CLOSE sent straight after the REQ, and one sent a tick later — because + * the connection was lost in different windows depending on how far the read had got. + */ +When( + /^(\w+) opens and closes (\d+) subscriptions at once$/, + async function (this: World>, name: string, count: string) { + const ws = this.parameters.clients[name] as WebSocket + const total = Number(count) + + for (let index = 0; index < total; index += 1) { + const subscription = `abort-${index}` + ws.send(JSON.stringify(['REQ', subscription, { kinds: [1] }])) + if (index % 2 === 0) { + ws.send(JSON.stringify(['CLOSE', subscription])) + } else { + await new Promise((resolve) => setImmediate(resolve)) + ws.send(JSON.stringify(['CLOSE', subscription])) + } + } + + // Give the aborted reads time to finish; a stranded connection stays stranded. + await sleep(250) + }, +) + +Then(/^no pooled database connection is in use$/, async function (this: World>) { + const dbClient = getMasterDbClient() + + const pool = (dbClient as any).client.pool + expect(pool.numUsed()).to.equal(0) + + // And the pool still works: a stranded connection showed up as a query that never answered. + const result: any = await dbClient.raw('SELECT 1 AS ok') + expect(result.rows[0].ok).to.equal(1) +}) + +Then(/^no database backend is left active$/, async function (this: World>) { + const dbClient = getMasterDbClient() + + const result: any = await dbClient.raw( + `SELECT count(*)::int AS active + FROM pg_stat_activity + WHERE datname = current_database() + AND pid <> pg_backend_pid() + AND state = 'active'`, + ) + + expect(result.rows[0].active).to.equal(0) +})