Skip to content

Avoid module-scoped DB clients in Workers templates #935

@sethcarlton

Description

@sethcarlton

Issue
In Cloudflare Workers, using a module-level singleton DB client over TCP can fail on subsequent requests.

The first request succeeds, but after idle time the TCP connection is closed. A subsequent request reuses the stale client and hangs until Workers cancels the request with:

The Workers runtime canceled this request because it detected that your Worker's code had hung and would never generate a response.
Ref: https://developers.cloudflare.com/workers/observability/errors/

Fix
In Workers, a request-scoped TCP DB client should be created , so idle/stale connections are not reused and do not crash the worker.

Notes
The issue only surfaces when using TCP, but I think the sane default would be to always scope the instance to the request (in workers) and not worry about detecting the driver.

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