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

"net" and "Bun.connect" both seem to block aggressively, sequentially run and fail in uncatchable ways [TCP] #10711

Closed
ShanonJackson opened this issue Apr 30, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@ShanonJackson
Copy link

ShanonJackson commented Apr 30, 2024

What version of Bun is running?

1.1.6

What platform is your computer?

Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

(async () => {
	const sockets = new Array(100).fill({ ip: "10.10.10.10", port: 6881 }).map(async ({ ip, port }) => {
		return await Bun.connect({
			hostname: ip,
			port: port,
			socket: {
				data(socket, data) {
					console.log("data");
				},
				open(socket) {
					console.log("open");
				},
				close(socket) {
					// console.log("SOCKET CLOSED");
				},
				drain(socket) {
					// console.log("SOCKET DRAIN");
				},
				error(socket, error) {
					console.log("PEER TCP ERROR: ", { error });
					socket.end();
				},
				connectError(socket, error) {
					console.log("CONNECT ERROR", { error });
					socket.end();
				},
				end(socket) {
					console.log("END");
					socket.end();
				},
				handshake(socket) {
					// console.log("HANDSHAKE");
					// Bitfield message indicating that we have no pieces
					// const bitfieldMessage = Buffer.alloc(5);
					// bitfieldMessage.writeUInt32BE(1, 0); // Length prefix
					// bitfieldMessage.writeUInt8(5, 4); // Message ID for bitfield
					// socket.write(bitfieldMessage);
				},
				timeout(socket) {
					socket.end();
				},
			},
		}).catch((e) => {
			console.log({ e });
			return null;
		});
	});
	await Promise.all(sockets);
	console.log("finished");
})();

What is the expected behavior?

1: Near identical code using "net" fails in same ways (so not exclusive to Bun TCP)
2: Running near identical code with NODE runtime using node:net succeeds (so NodeJS handles this fine)
3: Code runs incredibly slowly when real ip / ports are used 300 sockets may take 1hr to all timeout (in node this is several seconds)
4: ECONNREFUSED crashing the program seems impossible to catch either with try/catch or .catch

What do you see instead?

  • [On WSL] - the program just hangs.
  • [On Windows] - Crashes in uncatchable way when invalid ip/port is used. (ECONNREFUSED)
  • [On All OS ] - Seems to resolve sequentially timing out slowly when 100's of sockets are created to valid ip/ports

Additional information

Use case for us:

  • We help run and maintain the nodes on the bittorrent network we need to be able to connect to many peers over TCP to exchange information.
  • In Node we can create 100's of sockets to many peers and close them once we have the information we need, in Bun it looks like a single socket works but many sockets seems to not work.
@nektro
Copy link
Contributor

nektro commented May 8, 2024

does this work better with the following change? having await in the .map feels at odds with later calling Promise.all which would then await in parallel for you.

-return await Bun.connect
+return Bun.connect

@ShanonJackson
Copy link
Author

@nektro

Sorry that's just an artifact of pasting some code around to get a smaller example.

No this doesn't help, as mentioned in post title this also breaks in same way using net.createConnection which is NodeJS's TCP API supported in Bun. 1 Connection is fine, many connections stalls the program or fails. Removing await here does nothing. net.createConnection doesn't even have a Promise based API. Program runs fine using Node (and swapping Bun related API's for their NodeJS counterpart).

This is preventing us from finishing our Bun migration.

@ShanonJackson
Copy link
Author

https://bun.sh/blog/bun-v1.1.10#fixed-regression-in-v1-1-9-with-hanging-bun-install-on-windows

This issue is now fixed. Still have many more so will log new issues.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants