Skip to content

🐛 Bug Report — Closing TCP socket that didn't finish connection takes ~20 seconds, during which it still counts towards max outbound connection limit #2060

Open
@mornveig

Description

@mornveig

Hi team,

Closing TCP socket that didn't finish connection (for example host is unresponsive or port is closed) takes about ~21 seconds both locally and when deployed as cloudflare worker. During that time TCP socket does count towards outbound connection limit, and there seems to be no way currently to forcibly close the socket.

The following code could be used to check the time it takes to close the socket:

import { connect } from 'cloudflare:sockets';

export default {
	async fetch(request, env, ctx) {
		var start_time = Date.now();
		async function socket_connect_close() {
			var socket = connect({ hostname: "8.8.8.8", port: 9999 });
			await socket.close();
			return Date.now() - start_time;
		}
		return new Response(JSON.stringify({'close_time': await socket_connect_close()}), {headers: {"Content-Type": "application/json"}});
	},
};

If connection limit is reached, properly closing unresponsive connection delays all new connections by 21 seconds which sometimes is undesirable.
It'd be great if there was a way to control connect/close timeout for TCP sockets.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions