Skip to content

Production void/db (Postgres/Hyperdrive) caches pg.Pool across requests → Worker hangs (1101) on every request after the first #74

Description

@joefairburn

With database: "pg", the generated prod void/db caches its pg.Pool in module scope and reuses it across requests. That doesn't work on workerd: a TCP socket belongs to the request that opened it and is dead by the next one, so any query after the first in a warm isolate hangs and Cloudflare kills the request with:

1101 — "The Workers runtime canceled this request because it detected that your Worker's code had hung and would never generate a response."

In practice every DB-backed route 500s in prod as soon as the isolate warms up. no-DB routes are fine; the first request after a cold start works, everything after it hangs.

The generated module (void/dist/index.mjs, the dialect === "postgresql" template):

let _prodInstance;

function getInstance() {
  if (getRuntimeBinding("DATABASE_URL")) {
    // local dev: fresh pool per chain, because workerd kills sockets between requests
    return drizzle(new pg.Pool({ connectionString: getRuntimeBinding("DATABASE_URL"), max: 1 }), { schema });
  }
  // prod (Hyperdrive): cached across requests
  _prodInstance ??= drizzle(getConnectionString(), { schema });
  return _prodInstance;
}

The local branch comment already mentions the problem ("workerd kills TCP sockets between requests, so pg.Pool's cached connections go stale and hang").

Fix is to drop the cache and do what the local branch does:

function getInstance() {
  return drizzle(new pg.Pool({ connectionString: getConnectionString(), max: 1 }), { schema });
}

Hyperdrive pools on the origin side anyway, so a fresh pool per chain is cheap. We're running this as a pnpm patch against void@0.9.2 and it fixes the outage.

Env: void@0.9.2, database: "pg" over Hyperdrive, deployed to Cloudflare Workers.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions