Repository navigation
Description
Summary
When a runner stops while its listening client's LISTEN "jobs:insert"; LISTEN "worker:migrate"; query is still in flight, the client is released twice, and pg-pool throws Error: Release called on client which has already been released to the pool. from inside an async function, so it surfaces as an unhandled rejection.
Seen on 0.18.0; src/main.ts on main (097ea49) and 0.18.1 have the same code.
Sequence
In listenForChanges (src/main.ts):
- Shutdown calls
unlistenForChanges(), which callschangeListener.release().release()removes theerrorlistener, triesUNLISTEN, and callsreleaseClient(). It never setserrorHandled. - The in-flight
LISTENthen fails (in our case the connection is terminated,terminating connection due to administrator command). Its rejection handler isonErrorReleaseClientAndTryAgain, passed directly to.then(…, onErrorReleaseClientAndTryAgain), so removing theerrorlistener does not detach it. errorHandledis stillfalse, so it callsrelease()a second time (andreconnectWithExponentialBackoffduring shutdown). The secondreleaseClient()makes pg-pool throw.
We traced it by instrumenting dist/main.js: release called from unlistenForChanges, then onErrorReleaseClientAndTryAgain with errorHandled=false, then release again and releaseClient twice.
Reproduction
It happens naturally about once in 150 runs under CI load (stop a runner shortly after terminating its backends so the reconnect's LISTEN is in flight). To make it deterministic, hold back the LISTEN on the wire, stop the runner, then terminate the runner's backends:
import pg from "pg";
import { run } from "graphile-worker";
const rejections: unknown[] = [];
process.on("unhandledRejection", (r) => rejections.push(r));
// Hold back the listening client's LISTEN so the stop lands while it is in flight.
const send = pg.Connection.prototype.query;
let listenHeld!: () => void;
const held = new Promise<void>((resolve) => (listenHeld = resolve));
pg.Connection.prototype.query = function (this: pg.Connection, text: string) {
if (text.startsWith("LISTEN")) listenHeld();
else send.call(this, text);
};
const runner = await run({
connectionString,
taskList: {},
pollInterval: 100,
// a distinctive application_name lets the test find the runner's backends
});
await held;
const stopping = runner.stop();
await admin.query(
"select pg_terminate_backend(pid) from pg_stat_activity where application_name = $1 and datname = current_database()",
[runnerApplicationName],
);
await stopping;
await new Promise((r) => setTimeout(r, 200));
// rejections now holds: Error: Release called on client which has already been released to the pool.This fails 5 of 5 times on 0.18.0 and passes 5 of 5 with the change below.
Suggested fix
Mark the error as handled when the client is released, so a later failure of the pending LISTEN cannot release it again or reconnect:
async function release() {
+ errorHandled = true;
// No need to call changeListener.release() because the client errored
changeListener = null;We are carrying this as a pnpm patch until a release includes a fix. Happy to open a PR with a test if that helps.
Activity