[object Object]

← back to Rentv

test(pr): harden throwaway-DB reset against lingering connections (fix flaky suite)

2382432be839567ffa2e19e9c8400412647bad23 · 2026-08-08 08:42:45 -0700 · Steve Abrams

Root cause (TK-10366): the top-level execSync('dropdb; createdb rentv_pr_test')
throws when a pooled connection lingers on the test DB — Postgres refuses to DROP
a database 'being accessed by other users', then createdb fails 'already exists'
(exit 1), fast-failing the whole pr/integration suite intermittently (~8s).

Fix: pg_terminate_backend all stray backends on rentv_pr_test (scoped strictly by
datname, excludes own pid) BEFORE dropdb, so the drop always succeeds -> createdb
always clean. Proven: with a held pg_sleep(25) connection the OLD cmd exits 1, the
NEW cmd exits 0; full suite 88/88 across 1 hostile + 2 clean runs. Test-only,
prod-safe (never touches rentv_pr), reversible.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 2382432be839567ffa2e19e9c8400412647bad23
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Sat Aug 8 08:42:45 2026 -0700

    test(pr): harden throwaway-DB reset against lingering connections (fix flaky suite)
    
    Root cause (TK-10366): the top-level execSync('dropdb; createdb rentv_pr_test')
    throws when a pooled connection lingers on the test DB — Postgres refuses to DROP
    a database 'being accessed by other users', then createdb fails 'already exists'
    (exit 1), fast-failing the whole pr/integration suite intermittently (~8s).
    
    Fix: pg_terminate_backend all stray backends on rentv_pr_test (scoped strictly by
    datname, excludes own pid) BEFORE dropdb, so the drop always succeeds -> createdb
    always clean. Proven: with a held pg_sleep(25) connection the OLD cmd exits 1, the
    NEW cmd exits 0; full suite 88/88 across 1 hostile + 2 clean runs. Test-only,
    prod-safe (never touches rentv_pr), reversible.
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---
 test/pr/integration.test.js | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/test/pr/integration.test.js b/test/pr/integration.test.js
index ecc4c210..aa7cea40 100644
--- a/test/pr/integration.test.js
+++ b/test/pr/integration.test.js
@@ -10,7 +10,15 @@ const { execSync } = require('child_process');
 
 // Fresh throwaway DB every run — migrations reapply from scratch (fast), so letter
 // versions and settings mutations from a prior run can never leak into this one.
-execSync('dropdb --if-exists rentv_pr_test 2>/dev/null; createdb rentv_pr_test', { shell: '/bin/bash' });
+// Terminate any lingering backends BEFORE dropping: Postgres refuses to DROP a database
+// that still has active connections ("… is being accessed by other users"), which makes
+// this execSync throw at module load and intermittently fast-fails the whole suite when a
+// pooled connection from a prior run lingers (TK-10366 flake). Kill strays, then recreate.
+execSync(
+  `psql -d postgres -tAc "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='rentv_pr_test' AND pid <> pg_backend_pid()" 2>/dev/null; `
+  + 'dropdb --if-exists rentv_pr_test 2>/dev/null; createdb rentv_pr_test',
+  { shell: '/bin/bash' },
+);
 
 const db = require('../../src/pr/db');
 const organizations = require('../../src/pr/services/organizations');

← 29208e0f test+a11y: deal-registry regression suite (env-injectable pa  ·  back to Rentv  ·  chore: gitignore logs/ (runtime cron .out output leaked past cd8a9379 →