Skip to content

Remove .end() calls, add handler notes#28160

Merged
elithrar merged 3 commits into
productionfrom
opencode/issue28159-20260206173606
Feb 6, 2026
Merged

Remove .end() calls, add handler notes#28160
elithrar merged 3 commits into
productionfrom
opencode/issue28159-20260206173606

Conversation

@ask-bonk

@ask-bonk ask-bonk Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

All changes are complete. Here's a summary of what was done:

Summary of changes

Problem

The Hyperdrive documentation incorrectly recommended calling client.end(), sql.end(), connection.end() (via ctx.waitUntil()) to clean up database clients. In reality, Workers-to-Hyperdrive connections are automatically cleaned up when the request/invocation ends. The docs also lacked guidance about not creating database clients in global scope.

Changes made across 13 files:

Conceptual documentation (2 files):

  1. src/content/docs/hyperdrive/concepts/connection-lifecycle.mdx - Rewrote the "Cleaning up client connections" section to clearly state .end() is not required. Updated the code example to remove the ctx.waitUntil(client.end()) call and added a comment explaining automatic cleanup. Added a :::note[Create database clients inside your handlers] callout warning against creating database clients or driver-level pools in global scope.

  2. src/content/docs/hyperdrive/concepts/query-caching.mdx - Wrapped the code snippets showing dual Hyperdrive configs inside proper fetch() handler context with comments reinforcing "not in global scope".

Overview and getting started (2 files):

  1. src/content/docs/hyperdrive/index.mdx - Removed ctx.waitUntil(client.end()) from the PostgreSQL example and ctx.waitUntil(connection.end()) from the MySQL example.

  2. src/content/docs/hyperdrive/get-started.mdx - Removed ctx.waitUntil(sql.end()) from the PostgreSQL example and ctx.waitUntil(connection.end()) from the MySQL example. Updated the numbered explanation text for both tabs.

Partials (reused across multiple pages) (4 files):

  1. src/content/partials/hyperdrive/use-node-postgres-to-make-query.mdx - Removed ctx.waitUntil(client.end()).
  2. src/content/partials/hyperdrive/use-postgres-js-to-make-query.mdx - Removed ctx.waitUntil(sql.end()).
  3. src/content/partials/hyperdrive/use-mysql2-to-make-query.mdx - Removed ctx.waitUntil(connection.end()).
  4. src/content/partials/hyperdrive/use-mysql-to-make-query.mdx - Removed connection.end() from the callback-style mysql driver example.

Driver/ORM examples (3 files):

  1. src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx - Removed ctx.waitUntil(client.end()).
  2. src/content/docs/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm.mdx - Removed ctx.waitUntil(connection.end()).
  3. src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/prisma-orm.mdx - Removed ctx.waitUntil(prisma.$disconnect()).

Tutorial (1 file):

  1. src/content/docs/hyperdrive/tutorials/serverless-timeseries-api-with-timescale.mdx - Removed both ctx.waitUntil(client.end()) calls (POST and GET branches).

Closes #28159

github run

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk
ask-bonk Bot requested a review from a team February 6, 2026 17:45
@github-actions github-actions Bot added product:hyperdrive Hyperdrive: https://developers.cloudflare.com/hyperdrive/ size/m labels Feb 6, 2026
@github-actions

github-actions Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

This PR requires additional review attention because it affects the following areas:

Partials

This PR updates partial files, which are pieces of content used across multiple files in our Render component.

@github-actions

github-actions Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/hyperdrive/ @elithrar, @thomasgauvin, @sejoker, @oxyjun, @cloudflare/pcx-technical-writing
/src/content/partials/hyperdrive/ @elithrar, @thomasgauvin, @sejoker, @oxyjun, @cloudflare/pcx-technical-writing

Comment thread src/content/docs/hyperdrive/concepts/connection-lifecycle.mdx
… page

Add exact error messages from node-postgres, postgres.js, mysql2, mysql,
and the Workers runtime so users (and agents) can search for these
strings and find the troubleshooting guidance. Covers errors thrown when
database clients are created in global scope or reused across requests.
@ask-bonk

ask-bonk Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor Author

Done. I've added a new "Stale connection and I/O context errors" section to src/content/docs/hyperdrive/observability/troubleshooting.mdx:71 with exact, searchable error strings organized by driver/runtime:

Workers runtime errors (2 errors):

  • Disallowed operation called within global scope... — I/O in global scope
  • Cannot perform I/O on behalf of a different request... — cross-request I/O

node-postgres (pg) (6 errors):

  • Connection terminated
  • Connection terminated unexpectedly
  • Client has encountered a connection error and is not queryable
  • Client was closed and is not queryable
  • Cannot use a pool after calling end on the pool
  • Client has already been connected. You cannot reuse a client.

Postgres.js (postgres) (3 errors):

  • write CONNECTION_ENDED <host>:<port>
  • write CONNECTION_DESTROYED <host>:<port>
  • write CONNECTION_CLOSED <host>:<port>

mysql2 (3 errors):

  • Can't add new command when connection is in closed state
  • Connection lost: The server closed the connection.
  • Pool is closed.

mysql (3 errors):

  • Cannot enqueue Query after fatal error.
  • Cannot enqueue Query after invoking quit.
  • Cannot enqueue Handshake after already enqueuing a Handshake.

Each error includes details about when it occurs and a recommended fix pointing users to create clients inside handlers, with a link back to the connection lifecycle docs.

github run

@ReppCodes

Copy link
Copy Markdown
Contributor

LGTM! Thank you, Matt!

Comment thread src/content/docs/hyperdrive/concepts/connection-lifecycle.mdx
@github-actions

github-actions Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor

Preview URL: https://dee5722b.preview.developers.cloudflare.com
Preview Branch URL: https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com

Files with changes (up to 15)

Original Link Updated Link
https://developers.cloudflare.com/hyperdrive/concepts/query-caching/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/concepts/query-caching/
https://developers.cloudflare.com/hyperdrive/observability/troubleshooting/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/observability/troubleshooting/
https://developers.cloudflare.com/hyperdrive/concepts/connection-lifecycle/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/concepts/connection-lifecycle/
https://developers.cloudflare.com/hyperdrive/get-started/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/get-started/
https://developers.cloudflare.com/hyperdrive/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/
https://developers.cloudflare.com/hyperdrive/tutorials/serverless-timeseries-api-with-timescale/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/tutorials/serverless-timeseries-api-with-timescale/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/prisma-orm/ https://opencode-issue28159-20260206173606.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/prisma-orm/

@ask-bonk

ask-bonk Bot commented Feb 6, 2026

Copy link
Copy Markdown
Contributor Author

Done. Here's a summary of the changes to src/content/docs/hyperdrive/concepts/connection-lifecycle.mdx:

  1. Added TypeScriptExample component import at the top of the file (line 8).

  2. Added a single TypeScriptExample block (lines 76-103) after the existing :::note[Create database clients inside your handlers] callout. The example shows both patterns in one code block:

    • Bad (global scope): A globalClient created at module level with comments explaining it will become stale and cause hard errors on subsequent requests.
    • Good (inside handler): A client created inside the fetch handler, with comments explaining Hyperdrive pools the underlying connection so per-request clients are fast and reliable.

This follows the same pattern used on the Durable Objects rules page with 🔴 Bad / ✅ Good annotations.

github run

@elithrar elithrar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The good/bad example = great.

@elithrar
elithrar enabled auto-merge (squash) February 6, 2026 18:13
@elithrar
elithrar merged commit af6ba0b into production Feb 6, 2026
9 checks passed
@elithrar
elithrar deleted the opencode/issue28159-20260206173606 branch February 6, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:hyperdrive Hyperdrive: https://developers.cloudflare.com/hyperdrive/ size/m

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hyperdrive: clarity client.end() / closing clients

5 participants