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

fetch() hangs and fails randomly with HTTPS URLs #7260

Closed
oofdere opened this issue Nov 22, 2023 · 19 comments
Closed

fetch() hangs and fails randomly with HTTPS URLs #7260

oofdere opened this issue Nov 22, 2023 · 19 comments
Labels
bug Something isn't working web:fetch

Comments

@oofdere
Copy link

oofdere commented Nov 22, 2023

What version of Bun is running?

1.0.13+f5bf67bd1

What platform is your computer?

Linux 6.6.1-arch1-1 x86_64 unknown

What steps can reproduce the bug?

Making any fetch() call with an HTTPS request can cause the bug.

What is the expected behavior?

Instant fetch requests, like happens when using raw HTTP:

console.log(
  "http://example.com",
  await fetch("http://example.com").then((x) => x.status)
);
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.128s
user    0m0.015s
sys     0m0.018s
show/hide full console log
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.128s
user    0m0.015s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.123s
user    0m0.011s
sys     0m0.024s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[A
^[[Ahttp://example.com 200

real    0m1.123s
user    0m0.021s
sys     0m0.015s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.121s
user    0m0.022s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts^C
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.112s
user    0m0.009s
sys     0m0.022s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.126s
user    0m0.019s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.129s
user    0m0.013s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.142s
user    0m0.017s
sys     0m0.019s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.122s
user    0m0.027s
sys     0m0.007s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.157s
user    0m0.025s
sys     0m0.007s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.124s
user    0m0.015s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.133s
user    0m0.012s
sys     0m0.022s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.131s
user    0m0.014s
sys     0m0.020s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.124s
user    0m0.019s
sys     0m0.015s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.146s
user    0m0.017s
sys     0m0.016s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.147s
user    0m0.010s
sys     0m0.022s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.106s
user    0m0.015s
sys     0m0.019s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.150s
user    0m0.025s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.138s
user    0m0.018s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.145s
user    0m0.015s
sys     0m0.022s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.142s
user    0m0.020s
sys     0m0.010s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[A
http://example.com 200

real    0m1.138s
user    0m0.017s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.130s
user    0m0.024s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.131s
user    0m0.015s
sys     0m0.016s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.136s
user    0m0.012s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.130s
user    0m0.020s
sys     0m0.016s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.138s
user    0m0.014s
sys     0m0.024s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[A
http://example.com 200

real    0m3.492s
user    0m0.020s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.122s
user    0m0.019s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.125s
user    0m0.009s
sys     0m0.024s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.123s
user    0m0.015s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[Ahttp://example.com 200

real    0m0.407s
user    0m0.013s
sys     0m0.023s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.122s
user    0m0.009s
sys     0m0.024s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.123s
user    0m0.017s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[Ahttp://example.com 200

real    0m0.423s
user    0m0.023s
sys     0m0.013s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.138s
user    0m0.025s
sys     0m0.009s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.134s
user    0m0.021s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.147s
user    0m0.017s
sys     0m0.012s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.149s
user    0m0.013s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.132s
user    0m0.019s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
http://example.com 200

real    0m0.125s
user    0m0.020s
sys     0m0.012s

What do you see instead?

Fetch requests can be slow and fail after a while:

console.log(
  "https://example.com",
  await fetch("https://example.com").then((x) => x.status)
);
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    1m9.758s
user    0m0.023s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "https://example.com/"

real    2m14.251s
user    0m0.013s
sys     0m0.029s
show/hide full console log
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.924s
user    0m0.019s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "https://example.com/"

real    2m14.251s
user    0m0.013s
sys     0m0.029s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m2.152s
user    0m0.020s
sys     0m0.014s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.212s
user    0m0.020s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m2.502s
user    0m0.025s
sys     0m0.011s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.200s
user    0m0.017s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.204s
user    0m0.021s
sys     0m0.013s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.175s
user    0m0.015s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.169s
user    0m0.018s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.181s
user    0m0.017s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.174s
user    0m0.015s
sys     0m0.019s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.172s
user    0m0.020s
sys     0m0.015s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
^[[Ahttps://example.com 200

real    0m4.177s
user    0m0.025s
sys     0m0.015s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.682s
user    0m0.014s
sys     0m0.021s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.189s
user    0m0.017s
sys     0m0.019s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.176s
user    0m0.015s
sys     0m0.020s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.464s
user    0m0.016s
sys     0m0.017s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    1m9.758s
user    0m0.023s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.189s
user    0m0.021s
sys     0m0.013s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
https://example.com 200

real    0m0.169s
user    0m0.014s
sys     0m0.018s
[teo@koolaide crabrave]$ time bun run test/fetch.test.ts
ConnectionRefused: Unable to connect. Is the computer able to access the url?
 path: "https://example.com/"

real    2m12.943s
user    0m0.019s
sys     0m0.027s
[teo@koolaide crabrave]$ 

Additional information

No response

@oofdere oofdere added the bug Something isn't working label Nov 22, 2023
@Jarred-Sumner
Copy link
Collaborator

have you verified your internet does not have issues? are you able to reproduce similar issues in node.js?

@oofdere oofdere changed the title fetch() hangs and fails randomly fetch() hangs and fails randomly with HTTPS URLs Nov 22, 2023
@oofdere
Copy link
Author

oofdere commented Nov 22, 2023

have you verified your internet does not have issues? are you able to reproduce similar issues in node.js?

Yes, node works perfectly fine, as does Firefox and Chrome.

node tests
console.log(
  "https://example.com",
  await fetch("https://example.com").then((x) => x.status)
);
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.354s
user    0m0.145s
sys     0m0.055s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.598s
user    0m0.162s
sys     0m0.042s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.352s
user    0m0.167s
sys     0m0.040s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.333s
user    0m0.160s
sys     0m0.037s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.357s
user    0m0.166s
sys     0m0.044s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.435s
user    0m0.200s
sys     0m0.035s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.343s
user    0m0.154s
sys     0m0.051s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.601s
user    0m0.173s
sys     0m0.044s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.586s
user    0m0.157s
sys     0m0.050s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.928s
user    0m0.159s
sys     0m0.048s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.350s
user    0m0.163s
sys     0m0.041s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.343s
user    0m0.154s
sys     0m0.046s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.346s
user    0m0.158s
sys     0m0.054s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.858s
user    0m0.161s
sys     0m0.037s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.873s
user    0m0.176s
sys     0m0.045s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.396s
user    0m0.169s
sys     0m0.041s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.363s
user    0m0.157s
sys     0m0.049s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.383s
user    0m0.163s
sys     0m0.039s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.338s
user    0m0.166s
sys     0m0.035s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.403s
user    0m0.157s
sys     0m0.049s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.644s
user    0m0.167s
sys     0m0.038s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.386s
user    0m0.156s
sys     0m0.053s
[teo@koolaide crabrave]$ time node fetch.js
https://example.com 200

real    0m0.344s
user    0m0.145s
sys     0m0.051s
[teo@koolaide crabrave]$ 

@Electroid Electroid added the web-api Something that relates to a standard Web API label Nov 22, 2023
@zomchak-code
Copy link

#6233

@zomchak-code
Copy link

Also experiencing this

@zomchak-code
Copy link

zomchak-code commented Dec 13, 2023

@Jarred-Sumner this looks like a critical issue, let me know if I can help any way other than diving into bun code

To reproduce:

OS: MacOS 14.1 (M1 chip)
Bun version: 1.0.11

Host https endpoint that returns 200 with 6 minutes delay, for example:

const app = new Hono();
app.post('/delay/:secs', async (c) => {
  const secs = parseInt(c.req.param('secs'));
  await new Promise(res => setTimeout(res, secs * 1000));
  return c.json({ secs });
});

Run this code with bun:

const axios = require('axios');

axios.post(`https://endpoint/delay/360`, {})
.then((res) => {
    console.log(`statusCode: ${res.statusCode}`)
})
.catch((error) => {
    console.error(error)
});

Expected result

Status code logged out after 6 minutes

Actual result

No output

@aardvarkk
Copy link

aardvarkk commented Jan 15, 2024

I believe I'm seeing this too? I'm just trying to use axios to make a web request and it's just hanging every time I try to do it in bun. I swap over to just running the file with node and it succeeds immediately. Very bizarre. I'm running on an M2 Macbook.

What's super weird is that I think at first it succeeded a few times. Then as I continue to run the script several times it would lock up every other time or so. Now it just fails every time I run it.

I was initially running on v1.0.14. I tried upgrading to v1.0.22 just to see if it fixed anything, but it didn't. I also tried going all the way back to v1.0.4 and it still failed.

@gopricy
Copy link

gopricy commented Feb 5, 2024

We hit this bug many times on production and it made many npm library not useable like twilio

@thecrabcode
Copy link

Is there any update on this? This feels like a very basic need for most devs. We are also experincing the exact same problem when using an external library which relies on axios under the hood.

@anthonykrivonos
Copy link

anthonykrivonos commented Feb 16, 2024

+1, we're experiencing this too when hitting HTTPS endpoints with Bun's fetch. The underlying error is getting swallowed and all we're seeing on our end is:

TimeoutError: The operation timed out

Edit: this is only happening for GET requests on our EKS nodes, but not locally 🤔

@Jarred-Sumner
Copy link
Collaborator

I suspect it's something related to IPv6 vs IPv4. We haven't implemented "happy eyeballs" yet

@chadacious
Copy link

Yes, seems to be something with IPv6...

On our team, all of us were having issues with axios hanging intermittently. Then we switched to fetch and instead of hanging we were getting the ConnectionRefused errors intermittently, but less often. Except that two of us weren't. We discovered that our network adapters had their ipv6 disabled. So we had the others (some linux, some windows under wsl) disable theirs and the problems are gone!

Also fortunately, we aren't experiencing these errors in our live environment (digitalocean k8s cluster).

So I have no idea how that helps with the root cause of this but thought I would share in case it can help get some people unstuck.

@electic
Copy link

electic commented Mar 2, 2024

have you verified your internet does not have issues? are you able to reproduce similar issues in node.js?

We are also having this issue with axios. A random number of axios get requests will get through and then it will just not work anymore.

@jpoles1
Copy link

jpoles1 commented Mar 3, 2024

Likewise, seeing the same issue, resolved with switching back to node

@thecrabcode
Copy link

Not sure if this will help anyone but in our case we realised that the issue lied in the https agent which was actually injected by Sentry, related to what is reported in #5091.

In our case the solution was to amend the Sentry.init function by adding

Sentry.init({
  ...
  integrations: (int) => int.filter((i) => !['Http'].includes(i.name)),
 })

@aardvarkk
Copy link

Here's a workaround I'm using with axios to stop the hang from occurring:

import { Agent } from "http";
const { data } = await axios.get("http://myurl.com", { httpAgent: new Agent({ family: 4 }) });

@andenacitelli
Copy link

andenacitelli commented Mar 13, 2024

Unable to reproduce. Dropping some benchmarks. Here's various iterations of running the above snippet on a M2 Mac.

Normal run, no weird high outliers, hyperfine automatic run count:

➜  sandbox hyperfine "bun run index.ts" --warmup 1
Benchmark 1: bun run index.ts
  Time (mean ± σ):       9.8 ms ±   0.6 ms    [User: 5.9 ms, System: 4.4 ms]
  Range (min … max):     8.5 ms …  11.4 ms    229 runs

Disabled ipv6, verified https://ip.me/ reported an ipv4 address, and tried again w/ high hyperfine run count:

➜  sandbox hyperfine "bun run index.ts" --warmup 1 --runs 5000
Benchmark 1: bun run index.ts
  Time (mean ± σ):       9.7 ms ±   0.7 ms    [User: 6.0 ms, System: 4.4 ms]
  Range (min … max):     8.1 ms …  17.1 ms    5000 runs

Reenabled ipv6, verified https://ip.me reported an ipv6 address, and tried again w/ high hyperfine run count:

➜  sandbox hyperfine "bun run index.ts" --warmup 1 --runs 5000
Benchmark 1: bun run index.ts
  Time (mean ± σ):       9.5 ms ±   0.6 ms    [User: 5.9 ms, System: 4.4 ms]
  Range (min … max):     8.0 ms …  14.0 ms    5000 runs

No meaningful conclusions other than concluding that the local ipv4/ipv6 switch in MacOS network settings doesn't seem to be a viable means to reproduce.

@beikeni
Copy link

beikeni commented Mar 23, 2024

I'm also being affected by this. On a mac m1 using the twilio sdk.

@Destiner
Copy link

Able to reproduce with axios 1.6.8 and bun v1.0.11 (f7f6233e). Switching to fetch fixed it for me.

@Electroid Electroid added web:fetch and removed web-api Something that relates to a standard Web API labels Jun 12, 2024
@Jarred-Sumner
Copy link
Collaborator

Pretty sure this is fixed due to a combination of happy eyeballs and #11849 and related PRs

If you're still running into this after Bun v1.1.12 or so, please leave a comment and we'll re-open

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

No branches or pull requests