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

bun dev server fails with ConnectionClosed: The socket connection was closed unexpectedly #5363

Closed
evertonspader-tomtom opened this issue Sep 14, 2023 · 25 comments · Fixed by #10239
Assignees
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@evertonspader-tomtom
Copy link

evertonspader-tomtom commented Sep 14, 2023

What version of Bun is running?

1.0.1

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

I have a small project with NextJS and I am testing Bun. When starting the dev server with bun --bun run dev, I have a fetch() call to fetch some data on the home page that fails.

What is the expected behavior?

The dev server should start as it does with yarn and npm and load the home page with the data fetched populated in the fields.

What do you see instead?

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

The port in the path is always different and fetch doesn't allow verbose: true in the second argument

Additional information

This is the snippet that is making the call to an API in the project that just returns a JSON with data. It works with npm and yarn

async function getHelperData() {
  const res = await fetch(`${BASE_URL}/api/helper-data`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json'
    }
  });
  const data = await res.json();
  return data.data;
}

useEffect(() => {
    getHelperData()
      .then((data) => {
        setCategoriesData(data);
      })
      .catch((err) => {
        alert('Failed to fetch categories: ' + err);
      });
  }, []);
@evertonspader-tomtom evertonspader-tomtom added the bug Something isn't working label Sep 14, 2023
@nivoc
Copy link

nivoc commented Sep 14, 2023

I've the same issue. MacOS/M1 with Bun 1.0.1

What steps can reproduce the bug?

bun create next-app
bun --bun run dev

The unmodified next-app template fails.

Error

$ bun --bun run dev
- ready started server on localhost:3000, url: http://localhost:3000
- event compiled client and server successfully in 191 ms (20 modules)
- wait compiling...
- event compiled client and server successfully in 19 ms (20 modules)
- wait compiling /page (client and server)...
- event compiled client and server successfully in 1120 ms (417 modules)
- wait compiling...
- event compiled successfully in 70 ms (235 modules)
ConnectionClosed: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
 path: "http://localhost:49324/"

- wait compiling /_error (client and server)...

@underclockd
Copy link

underclockd commented Sep 15, 2023

This was resolved for me today with the latest release, possibly fixed by #5126?

edit: I'm on MacOS Intel, not ARM, but I was experiencing this exact issue prior to updating.

I ran the dev server without the --bun flag before updating as well, which actually prompted me to give the process permission to use sockets. I'm not sure if that may have something to do with the fix, but I was still receiving the error up until I updated Bun and swapped to a fresh shell.

@cybermelons
Copy link

cybermelons commented Sep 15, 2023

I ran into this error too on my nextjs project. Will check if it's fixed with latest release

EDIT: Ran this on bun 1.0.2.

When I run bun --bun run dev it throws me this error when I visit http://ocalhost:3000/

- error Failed to handle request for /
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:64086/"

@omrta-dev
Copy link

Can anyone running into this issue try to use: bun run dev instead of bun --bun run dev?

That seems to have fixed the issue for me, but i can't find documentation on the --bun flag that may be causing this issue.

@cybermelons
Copy link

Can anyone running into this issue try to use: bun run dev instead of bun --bun run dev?

That seems to have fixed the issue for me, but i can't find documentation on the --bun flag that may be causing this issue.

I use bun run dev and it works perfectly fine. My issue is with the --bun flag as well

@nivoc
Copy link

nivoc commented Sep 15, 2023

@omrta-dev, @cybermelons

Running bun run dev without the --bun flag says: Execute the script "next" with parameter "dev" as specified in package.json. But that script (node_modules/.bin/next) says in it's first line "#!/usr/bin/env node".
So in nextjs case, Node.js is the runtime, which is why there are no errors. Essentially, running without the flag is equivalent to running without Bun.

When you include the --bun flag, it means that #!/usr/bin/env node gets ignored and executed with bun as the runtime.

So, the --bun flag is essential to ensure Bun is the runtime for your environment.

https://bun.sh/docs/cli/run#bun

@nivoc
Copy link

nivoc commented Sep 15, 2023

Looks like it's not only a macos-Issue: #5494

@nivoc
Copy link

nivoc commented Sep 15, 2023

Update: I just tested with Bun1.0.2. It looks better but now I get a "error: Failed to construct 'Request': signal is not of type AbortSignal."

@PawelDuplaga
Copy link

Update: I just tested with Bun1.0.2. It looks better but now I get a "error: Failed to construct 'Request': signal is not of type AbortSignal."

Same here. I think it may be connected to using App Router, since i dont get that error using old "Pages"

@evertonspader-tomtom
Copy link
Author

Update: I just tested with Bun1.0.2. It looks better but now I get a "error: Failed to construct 'Request': signal is not of type AbortSignal."

I just updated to 1.0.2 and the original error message is gone but I'm seeing the same error as mentioned above

@The-Code-Monkey
Copy link

Yeah getting this on M2 Mac Pro too.

@omrta-dev
Copy link

Getting this on both linux and WSL, so i don't think it's platform specific. @PawelDuplaga mentioned that it's related to the app router and I can confirm that I am also using the App router and facing this issue.

@StefanoVe
Copy link

StefanoVe commented Sep 23, 2023

I'm getting the same issue trying to spin up a proxy server using express with express-http-proxy inside a Docker (20.10.17) container running under CentOS 8 64 bit

@peacefulotter
Copy link

peacefulotter commented Jan 11, 2024

Still getting this issue with the --bun flag AND without, on Bun 1.0.22+b400b36ca. I am trying to send a big JSON through a POST request. Oddly, it works when the JSON is smaller, and, from my testings, it seems correlated.

@jellli
Copy link

jellli commented Feb 20, 2024

I'm getting this error when I use hono.js on M1 Mac.

@mlitvinav
Copy link

mlitvinav commented Feb 21, 2024

I want to add that this error is in Bun version 1.0.28 and not only related to localhost or dev mode.
I have the same error in fetch, axios and superagent when using Bun against some targets. The request is sent to the server but the connection is cut for some reason.

Minimal example:

// test.js
fetch("https://www.saferpay.com/api/health/1", { verbose: true }).catch(console.error)
bun run --bun test.js

Stdout:

Request: GET /api/health/1
        Connection: keep-alive
        User-Agent: Bun/1.0.28
        Accept: */*
        Host: www.saferpay.com
        Accept-Encoding: gzip, deflate, br

ConnectionClosed: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
 path: "https://www.saferpay.com/api/health/1"

@adhocore
Copy link

adhocore commented Mar 4, 2024

recently facing this same issue repeatedly. bun v1.0.29. after the error the server dies!

@adhocore
Copy link

rolled to 1.0.20 and issue is not there

@kevinm416
Copy link

I'm seeing this issue on bun 1.1.12 apple M2

@siarhei-semikau
Copy link

This issue is active for windows 11, bun 1.1.17!

@kirankunigiri
Copy link

Still seeing this issue on macOS M1, bun 1.1.20

@polRk
Copy link

polRk commented Jul 31, 2024

presents in modern clouds

@Anyoks
Copy link

Anyoks commented Aug 12, 2024

I have this issue too in bun v 1.1.20. using axios to make requests particularly via proxy

const config = {
    method: 'post',
    url: 'http://api.restful-api.dev/objects',
    data: {
      name: 'value1',
      data: 'value2',
    },
    proxy: {
      host: 'proxy.host',
      protocol: 'http',
      port: port,
      auth: {
        username: username,
        password: password
      }
    },
  
  };

  axios(config)
    .then((response) => {
      console.log(response.headers);
      console.log('Response:', response.data);
    })
    .catch((error) => {
      console.error('Error:', error.message);
    });
Error: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()

@devcaeg
Copy link

devcaeg commented Aug 31, 2024

This error still occurs in the latest versions.

@Jarred-Sumner
Copy link
Collaborator

If you continue to run into this issue, please open a new issue and include a reproduction

Sockets closing can have many different unrelated causes (the server disconnected, received invalid data, sent invalid data, various edgecases involving TLS). This issue was originally closed after addressing an issue with TLS client renegotiation from #10166 and #5363 (comment):

fetch("https://www.saferpay.com/api/health/1", { verbose: true }).catch(console.error)

@oven-sh oven-sh locked as resolved and limited conversation to collaborators Aug 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

Successfully merging a pull request may close this issue.