Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/integration/features/subscriptions/req-abort.feature
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions test/integration/features/subscriptions/req-abort.feature.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, any>>, 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<Record<string, any>>) {
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<Record<string, any>>) {
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)
})
Loading