Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default connections to less than 10 in Cloudflare Workers environment to avoid deadlock #1023

Open
Cherry opened this issue Jan 31, 2025 · 3 comments · May be fixed by #1024
Open

Default connections to less than 10 in Cloudflare Workers environment to avoid deadlock #1023

Cherry opened this issue Jan 31, 2025 · 3 comments · May be fixed by #1024

Comments

@Cherry
Copy link
Contributor

Cherry commented Jan 31, 2025

With the following Cloudflare Worker, errors can be observed when deployed to production:

import postgres from 'postgres';

export default {
    async fetch(request, env, ctx): Promise<Response> {
        const connection = postgres('postgres://reader:[email protected]:5432/pfmegrnargs');

        const results = await Promise.all([
            connection`SELECT 1`,
            connection`SELECT 2`,
            connection`SELECT 3`,
            connection`SELECT 4`,
            connection`SELECT 5`,
            connection`SELECT 6`,
            connection`SELECT 7`,
            connection`SELECT 8`,
            connection`SELECT 9`,
            connection`SELECT 10`,
        ]);
        ctx.waitUntil(connection.end());

        return Response.json(results);
    },
} satisfies ExportedHandler<Env>;

While this works fine locally using wrangler and workerd, in production, they enforce 6 max connections and after deploying this worker in production, hitting it essentially results in a deadlock and errors like this:
Cloudflare documents the connection limit in their docs here: https://developers.cloudflare.com/workers/platform/limits/#simultaneous-open-connections

The solution is to simply reduce the number of max connections from 10 to 3 (to allow other things in the worker) in user config, but I imagine it would be a good idea if this is reduced by default when in Workers environment, to prevent other users hitting issues when using the default configuration in a Cloudflare Workers environment.

This has also been raised to folks at Cloudflare to see if there's anything they can improve on their end to help solve, or at least improve the debugging experience of this.

@Cherry Cherry linked a pull request Jan 31, 2025 that will close this issue
@porsager
Copy link
Owner

Very good point! I think a proper default for CloudFlare is a good idea, but perhaps we should wait and see if they might change something on their end instead?

@Cherry
Copy link
Contributor Author

Cherry commented Jan 31, 2025

They're updating the docs at cloudflare/cloudflare-docs#19616, so I'm not sure if there's any planned changes on the network/runtime end, but waiting a little while to see if they do is probably a good idea.

@ReppCodes
Copy link

Let me link this issue around internally with the Runtime folks, so it gets noticed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants