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

ConnectionRefused when fetch() is localhost #1425

Closed
Electroid opened this issue Oct 30, 2022 · 56 comments
Closed

ConnectionRefused when fetch() is localhost #1425

Electroid opened this issue Oct 30, 2022 · 56 comments
Labels
bug Something isn't working

Comments

@Electroid
Copy link
Contributor

What version of Bun is running?

0.2.2

What platform is your computer?

Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 20:57:23 PDT 2022; root:xnu-8020.121.3~2/RELEASE_ARM64_T8110 arm64 arm

What steps can reproduce the bug?

export default {
  port: 4281,
  fetch(request) {
    return new Response("Ok");
  }
}

console.log(await fetch("http://localhost:4281/hello"))

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

The above code fails with the error:

ConnectionRefused: fetch() failed
 path: "http://localhost:4281/hello"

Additional information

No response

@Electroid Electroid added the bug Something isn't working label Oct 30, 2022
@Jarred-Sumner
Copy link
Collaborator

I used to hardcode localhost to 127.0.0.1 as a workaround, but it's not great since technically the OS could have rewritten localhost to anything else

@uNetworking do you have any ideas?

@Jarred-Sumner
Copy link
Collaborator

Fixed in Bun v0.7.1

@rubenfiszel
Copy link

Still have this issue on 0.7.3 for localhost:8000

@Voldemat
Copy link
Contributor

Voldemat commented Sep 9, 2023

On 1.0.0 same error

@Carter-Apas
Copy link

I have the same error on 1.0.0

@Electroid Electroid reopened this Sep 11, 2023
@masnormen
Copy link

Could this be related with my Next.js dev with bun run --bun next dev? It says there's something regarding sockets, which could be used by Next for HMR, followed by a ConnectionRefused.

dyld[35100]: missing symbol called
ConnectionClosed: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
 path: "http://localhost:51789/"

ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://localhost:51789/"

@BobDenar1212
Copy link

BobDenar1212 commented Sep 12, 2023

I have the same issue with the following:
wsl2 Ubuntu-22.04 LTS bun-1.0.0
Create nextjs app with :
bunx create-next-app test --ts --tailwind --eslint --app --src-dir --use-bun --import-alias "@/*"
Run nextjs app with:
bun next dev

Gives the output:

$ bun next dev
- ready started server on localhost:3000, url: http://localhost:3000
- event compiled client and server successfully in 209 ms (20 modules)
- wait compiling...
- event compiled client and server successfully in 38 ms (20 modules)
- wait compiling /page (client and server)...
- event compiled client and server successfully in 2.2s (426 modules)
- wait compiling...
- event compiled successfully in 174 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:43275/"

- wait compiling /_error (client and server)...
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://localhost:43275/favicon.ico"

EDIT 2023-10-09
I gave a try to latest bun version 1.0.4, and it is working fine

@markstos
Copy link

Using 127.0.0.1 works as a workaround.

@paperdave
Copy link
Member

paperdave commented Sep 27, 2023

Edit: this is incorrect, the server will begin on the tick after it has started

the code snippet in the issue is technically wrong.

export default {
  port: 4281,
  fetch(request) {
    return new Response("Ok");
  }
}

console.log(await fetch("http://localhost:4281/hello"))

when you use export default to start a server, it will only start after the module is finished evaluating, so you cannot top-level-await a fetch here.

Bun.serve({
  port: 4281,
  fetch(request) {
    return new Response("Ok");
  },
});

console.log(await fetch("http://localhost:4281"));

This works for me on Linux and MacOS. Can someone test this on WSL?

@markstos
Copy link

I wonder if the issue is that Bun is defaulting to an IPv6 connection for localhost, but there's no entry for localhost for IPv6 in /etc/hosts.

You could try adding this to /etc/hosts if there's not something like it there:

::1             localhost

@kszpakowski
Copy link

@markstos I already have /etc/hosts entry for IPv6 localhost and unfortunately it still does not work.

@nCrafts
Copy link

nCrafts commented Oct 5, 2023

Having the same issue and can’t really use Bun on any of my projects because of this. Is there something I can do to help debug this?

@masnormen
Copy link

I think this has been solved with 1.0.4, at least for me. Give it a try

@nCrafts
Copy link

nCrafts commented Oct 5, 2023

Unfortunately not for me. It says, ConnectionRefused: Unable to connect. Is the computer able to access the url?

@markstos
Copy link

markstos commented Oct 6, 2023

@nCrafts You can checkout the Bun project and review the related test coverage and then add a new failing test which triggers this failure:

https://github.com/oven-sh/bun/blob/main/test/js/web/fetch/fetch.test.ts

There are already some tests for fetch() with localhost, including some with IPv6, so you'll to figure what specifically is triggering this failure.

@danny-avila
Copy link

In my case, it seems to happen intermittently when the express res object closes res.on('close', ...)

@Schrammel
Copy link

Schrammel commented Nov 10, 2023

Related issue on 1.0.11

  • on browser it works all time
  • on bun it works sometimes
  console.log("fetching projects");
  const request = await fetch("https://git.syonet.com/api/v4/projects", {
    headers: {
      "Private-Token": "< my private token >",
    },
  });
  const projects = await request.json();
  console.table(projects);

output:

ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "https://git.syonet.com/api/v4/projects"

@mathiasrw
Copy link
Contributor

Same problem on mac m1 cpu.

Took me ages to identify that this line works in Node but not in Bun.

image image

@prantlf
Copy link

prantlf commented Dec 21, 2023

I can confirm the problem with bun 0.1.18. But executing the same .js script containing await fetch('http://localhost'... with node works.

@VigilioYonatan
Copy link

any update?

@jameshfisher
Copy link

Still broken on 1.0.27.

Example of node connecting just fine:

$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> const response = await fetch("http://localhost:11434/api/chat")
undefined
> await response.text()
'404 page not found'
>

bun repl equivalent fails with ConnectionRefused: Unable to connect:

$ bun repl
Welcome to Bun v1.0.27
Type ".help" for more information.
[!] Please note that the REPL implementation is still experimental!
    Don't consider it to be representative of the stability or behavior of Bun overall.
> const response = await fetch("http://localhost:11434/api/chat")
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://localhost:11434/api/chat"

>
$ bun --version
1.0.27

@jameshfisher
Copy link

Can confirm replacing localhost with 127.0.0.1 works. Although of course this is not a general workaround

@Iuriiiii
Copy link

Iuriiiii commented Feb 21, 2024

Make requests to http://[::1]:3000/ worked for me.

Notes:
I'm using Windows, Bun is executing on WSL.

@didier
Copy link

didier commented Mar 21, 2024

Still happening on 1.0.33.

@ArthurTimofey
Copy link

Still happening 1.1.0 M1 Pro :(

1 similar comment
@ArthurTimofey
Copy link

Still happening 1.1.0 M1 Pro :(

@zpix1
Copy link
Contributor

zpix1 commented Apr 5, 2024

Same on 1.1.1:

ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://localhost:11434/api/generate"

Fixed by replacing localhost with 127.0.0.1

@iHaiduk
Copy link

iHaiduk commented Apr 10, 2024

Same on 1.1.x:

ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://logs.eu-central-1.amazonaws.com/"

@lukewlms
Copy link

lukewlms commented Apr 15, 2024

Oh wow. Connecting to localhost is very common, I spent close to an hour trying to debug this and confused about why I could GET a public URL but not localhost. Seems like this would be a high priority to fix. curl and the browser can connect to localhost, bun should be able to also. (I'm on latest, v1.1.3)

@omarpando
Copy link

Still happening.

@7heMech
Copy link

7heMech commented May 10, 2024

Happens on 127.0.0.1 as well

@ilya-tkachov
Copy link

ilya-tkachov commented May 14, 2024

Used the latest version 1.1.8 on M3 Pro and got this issue but only when trying to use a proxy in nextjs between two apps. Had to downgrade to 1.0.35 and it worked like a charm.

@7heMech
Copy link

7heMech commented May 14, 2024

Used the latest version 1.1.8 on M3 Pro and got this issue but only when trying to use a proxy in nextjs between two apps. Had to downgrade to 1.0.35 and it worked like a charm.

@Jarred-Sumner maybe you can check this out?

@schulzf
Copy link

schulzf commented May 19, 2024

is there a temporary workaround for this?

We've been experiencing this issue in production (under v1.1.8), the server randomly fails to resolve url and throws Unable to connect. Is the computer able to access the url?

@Jarred-Sumner
Copy link
Collaborator

@schulzf if you can use 127.0.0.1 or 0.0.0.0 as the hostname that should workaround this. Or otherwise, if you can disable IPv6. The problem is that localhost often resolves to both IPv6 and IPv4, but the server may only be listening on IPv4.

@schulzf
Copy link

schulzf commented May 19, 2024

@Jarred-Sumner in our case is a not localhost but a live url (and it doesn't support ipv6).
By disabling ipv6 the server would become ipv4 only for inbound as well, which wouldn't be the best...

Is there a way to disable ipv6 in bun? (we're running under Cloud Run, we can't access the underlying system configuration)

Edit:

Will try using axios with custom http agent

    import https from 'https';

    const agent = new https.Agent({ family: 4 });

    const client = axios.create({
      ...axios.defaults.transformRequest,
      ...axios.defaults.transformResponse,
      httpAgent: agent,
    });

@mathiasrw
Copy link
Contributor

I can confirm that this is now working as expected with bun v1.1.9

Nice work @teamBun

image

#bunUp

@Jarred-Sumner
Copy link
Collaborator

Fixed in Bun v1.1.9

@rogercbe
Copy link

rogercbe commented Jun 10, 2024

I am still having the same problem on Bun v.1.1.12 using localstack and trying to upload an image to s3

AWS_ENDPOINT=http://localhost:4566

Error:

ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "http://my-buckett.localhost:4566/3354167639216915979?uploads="

As comented in the thread, changing it to 0.0.0.0 or 127.0.0.1 makes it work, but not ideal

@stewones
Copy link

not sure what I'm doing wrong but I can't seem to make bun fetch, neither using 0.0.0.0 or 127.0.0.1. tried everything spoken in this thread so far 🙄

@stewones
Copy link

holy moly, I figured out my problem. the issue is that I needed to fetch between containers, so nothing worked until I tried host.docker.internal, which did the trick. so yeah, I can confirm fetch on Bun 1.1.15 works 🎉

@garraflavatra
Copy link

Has this been tested with local TLS (Caddy server) as well? I am able to visit this URL using cURL but not with Bun.

> await fetch('https://test.localhost/api/v2/portal/funnels/1/')
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "https://test.localhost/api/v2/portal/funnels/1/"

@garraflavatra
Copy link

TLS turns out not to be the issue... instead, localhost subdomains (test.localhost) are not handled correctly.

@msamia
Copy link

msamia commented Jul 28, 2024

Hello, if for some reason when you try to access a registry file the connection fails, it gives an error of connection refused and not installed. Connection errors will not be recovered. The only way out in this case is to persist. This error is more likely to be detected if the json file that tries to access and fails is different in each attempt, especially if many components are installed at once.

@MichaelYuhe
Copy link

Still happening at v1.1.21

@mathiasrw
Copy link
Contributor

@MichaelYuhe Please fill out a new issue referencing this one and providing information on how to replicate and the versions of bun and OS.

@lsyuanyuan
Copy link

I cannot determine whether the issue is with the server or with Bun, as I have encountered the same problem when using other URLs. I am using a URL similar to http://oa.xxxx.com, and I am in a production environment.

Error message: ConnectionRefused: Unable to connect. Is the computer able to access the URL?
Path: "http://oa.xxxx.com/"

My service information:

Linux localhost.localdomain 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

bun -v 1.1.21

@7heMech
Copy link

7heMech commented Aug 3, 2024

I cannot determine whether the issue is with the server or with Bun, as I have encountered the same problem when using other URLs. I am using a URL similar to http://oa.xxxx.com, and I am in a production environment.

Error message: ConnectionRefused: Unable to connect. Is the computer able to access the URL?
Path: "http://oa.xxxx.com/"

My service information:

Linux localhost.localdomain 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

bun -v 1.1.21

If you don't provide the exact url or another url at which this happens we can't help.

@lsyuanyuan
Copy link

I cannot determine whether the issue is with the server or with Bun, as I have encountered the same problem when using other URLs. I am using a URL similar to http://oa.xxxx.com,和I am in a production environment.

Error message: ConnectionRefused: Unable to connect. Is the computer able to access the URL?
Path: "http://oa.xxxx.com/"

My service information:

Linux localhost.localdomain 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed 12月22日 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

bun -v 1.1.21

If you don't provide the exact url or another url at which this happens we can't help.

I'm sorry, I should have considered that it might be a server issue, because normally there wouldn't be any mistakes. Thank you!

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Aug 5, 2024

If you're continuing to run into an issue like this, please open a new issue with a complete reproduction

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

No branches or pull requests