bun icon indicating copy to clipboard operation
bun copied to clipboard

`ConnectionClosed` when making many requests at once to a server

Open gvilums opened this issue 1 year ago • 2 comments
trafficstars

What version of Bun is running?

1.1.9-debug+7add782c2

Also reproduces on latest stable (1.1.8)

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

let req_counter = 0;
const server = Bun.listen({
  port: 8080,
  hostname: "0.0.0.0",
  socket: {
    open(socket) {
      req_counter++;
      console.log(`request ${req_counter} received`);
      socket.end("HTTP/1.1 200 OK\nContent-Type: text/html\n\n<html><body>hello world</body></html>")
    },
    data(socket, data) {},
  }
})

const requests = [];
for (let i = 0; i < 180; i++) {
  requests.push(fetch("http://127.0.0.1:8080"));
}

await Promise.all(requests);
server.stop();

What is the expected behavior?

prints request {x} received 180 times

What do you see instead?

After a certain number of requests succeed, the following error shows up:

ConnectionClosed: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
 path: "http://127.0.0.1:8080/"

Additional information

It's unclear whether the problem is with fetch or Bun.listen

gvilums avatar May 21 '24 00:05 gvilums

Is the server emitting an error event? Curious if that's happening / what's the error

Electroid avatar May 21 '24 17:05 Electroid

This is an issue with the client - it still occurs when starting a node http server in a separate process

gvilums avatar May 21 '24 23:05 gvilums

Fixed in Bun v1.1.12

Jarred-Sumner avatar Jun 14 '24 18:06 Jarred-Sumner

Fwiw this generally works for me, but when using firebase-auth and verifyIdToken it still often fails. Downgrading to v1.0.9 works for me (anything above it has this issue)

smaccoun avatar Jun 24 '24 01:06 smaccoun