Skip to content

Listening client is released twice when the runner stops during an in-flight LISTEN #665

Backlog

Description

@capy-ai

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):

  1. Shutdown calls unlistenForChanges(), which calls changeListener.release(). release() removes the error listener, tries UNLISTEN, and calls releaseClient(). It never sets errorHandled.
  2. The in-flight LISTEN then fails (in our case the connection is terminated, terminating connection due to administrator command). Its rejection handler is onErrorReleaseClientAndTryAgain, passed directly to .then(…, onErrorReleaseClientAndTryAgain), so removing the error listener does not detach it.
  3. errorHandled is still false, so it calls release() a second time (and reconnectWithExponentialBackoff during shutdown). The second releaseClient() 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions