-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Android fetch
hangs indefinitely with IPv6 hosts on some devices (Happy Eyeballs)
#32730
Comments
Note that this doesn't repro on the emulator, this is the output on the emulator:
❗ The device where this is happening, and where I initially noticed it on is an Oppo A72 (CPH2067) running Android 11 |
I'm also not able to reproduce it on a Samsung Galaxy Tab A (SM-T290) device. |
fetch
hangs indefinitely with IPv6 hostsfetch
hangs indefinitely with IPv6 hosts on some devices
In my case, this issue also affected only some devices. I think it has something to do with the network stack or driver on the device. Or how they interact with IPv6 devices out on the network. |
Interesting. I left it running for a while and it actually seems to time out on the connection:
It took 4 minutes to time out. I just noticed that the device does not have IPv6 connectivity at all based on https://test-ipv6.com/ Might be a router problem on my end, usually I have IPv6. However, I'm not sure why it would even attempt to use an IPv6 address considering it does not have IPv6 connectivity at all. |
Very interesting, I rebooted the router and now I have IPv6 again. ❗ The Oppo now no longer has the error. It works perfectly:
I'm not sure what to make of this. 🤔 |
Yo mismo me encargo de resolver lo de sus vidas, ni hay ni habrá problema alguno, temor alguno o alguien que me agreda saludo sigan igual y cada vez mejoren más, ok. Si se puede. Atte: Jesus Francisco Urias García. |
Y nuevamente tu gol, esta vez artificial. |
Jajaja mediocres ellos, sobre ellos. |
The root cause of this issue seems to be the lack of "Happy Eyeballs" in the underlying okhttp library that react-native is using on Android. Here's the issue in okhttp that is tracking this: I think most of the random networking Android slowdowns reported all over the ecosystem are probably related. This is the one I ran into in particular: Possibly related issues: This thread also mentions this exact same issue: I believe a PR to implement Happy Eyeballs into okhttp shouldn't be too hard, if someone would be able to contribute one. From my understanding a TCP connection to both the IPv6 and IPv4 address needs to be attempted, and the first one to respond is used, while the other one is closed. This would solve this ubiquitous Android networking problem once and for all. |
@andreialecu would you be so kind to review my PR? |
@marcesengel I think this should be addressed in the underlying okhttp library. Perhaps you might want to contribute it there instead? There is some movement there that indicates this problem might finally get some attention. See: |
@andreialecu looking at the old issue regarding this a change on their end seemed unrealistic without a new major release (due to interceptors needing to be raced, see for example this comment, which reads "It's an invasive feature to add because there's extra complexity. If you need it, don't wait for us."), which in turn would mean that it probably takes some time to adapt this to react-native. I don't see any issue with implementing it for RN in the meantime. I'd have added it to okhttp if it didn't look like it'd take ages to catch on - in case it ever did. |
I see. That makes sense. Not sure who would need to take a look at this. I can't review it myself since I'm not familiar with the RN code dealing with this. Perhaps @yungsters would be able to take a look? |
Essentially this. Please take a look at my answer here: #33045 (comment) as I believe that's the most feasible solution we could take at this stage. |
I've built a patched version for use until OkHTTP 5.x is stable (now that since the first of February the implementation of Happy Eyeballs is confirmed). It's available as |
Cross-posting here as I realized I accidentally left the comment on the PR instead of the Issue: #33045 (comment) Would you be up for opening a draft PR with the OkHTTP bump to 5.x alpha? It could help us spot early breaking changes and we could test it against the internal infra to make sure nothing breaks. |
I'd be open to do the transition once major version 5 is stable, as for now there's a fix and I wouldn't want to change something multiple times in case more breaking changes are introduced before v5 hits stable. Do you know if there's an ETA for the stable release? |
fetch
hangs indefinitely with IPv6 hosts on some devicesfetch
hangs indefinitely with IPv6 hosts on some devices (Happy Eyeballs)
Nope but you can subscribe here to get updates |
Add below dependencies to
|
If you are using expo and your project is managed, you can not edit |
@hadeskers @lukawolf @andreialecu @cortinico I created an Expo app and run expo prebuild, how should I implement okhttp:5.0.0-alpha.11 version? is there any instruction? please help! |
It did not work for me, is there any other way to implement it? |
May I ask, having applied this, it is necessary to further modify the OkHttpClientProvider by adding |
fastFallback is the default in alpha.11 already |
Strange, I'm still having issues... |
Edit: Attempting to disable the Connection Pool did not eliminate the issue. As of now, I believe that the culprit is OkHttp's connection sharing somehow breaking things. For now, I removed my patch on OkHttpClientProvider.java, and instead applied a patch on NetworkingModule.java, which violently creates a separate OkHttpClient for each request and destroys the client after use (against all the advice of OkHttp), to eliminate any connection sharing. Nothing seems to hang randomly anymore so far (hopefully thats it...). i have to say, it frustrates me greatly that the network stack on React Native Android is causing me so much trouble and tinkering with network code, when all I'm using is plain fetches with JSON. I wonder if some of the default configurations should need further improvement for React Native Android - contrasting with the networking on iOS, which has worked flawlessly. Edit: For the record, both disabling the connection pool and violently preventing connection sharing were crucial to making network connections work reliably on low-end devices. Removing either of the patches reintroduces the issue. |
OkHttp 5 has a connection listener you can configure on the pool. It should expose all this so it's not surprising. |
How might the connection listener help? |
Maybe not solve the problem. But let you see what's going on, when connections are reused, released, fail. |
Good morning, my project bump on this issue since Android 14 officially come out and it took me one week before being able to connect the dots and realize it was an IPv6 problem and end up here, so I totally agree that this it's not an obvious issue and should be at least pointed out somewhere in the documentation (if there is, I could not find it) or on troubleshooting/common issues. Since Android 14, every user of our app based in the USA could not use the app since the first api call instead of taking less of a second it took more than 2 minutes, I have a reproducible repo with latest RN version here if you are interested. In order to reproduce the issue, the only thing you have to do is use an Android 14 phone with data connection and an ISP provider that supports IPv6, Verizon and AT&T for example The only thing that solved it, was leveraging okhttp3 okhttp:5.0.0-alpha.12 (alpha 11 did not solved for us) OR patching following danmaas's example here https://gist.github.com/danmaas/c60af5fed9f55d2bc616ce302696540d I really think this issue should be more visible, especially since Android 14 is out now and I think more and more people/apps will start experiencing this, let me know if there's something I can do to help with that, I was not sure opening a new issue on the repo was a good idea or not since there are already quite a few on the argument, like: |
For me it solved the problem for IPv6 support on android.
Add following dependencies to android/app/build.gradle to force use okhttp 5.0.0-alpha.12 "react": "18.2.0", |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
How to implement this in Expo project? |
@JKKholmatov never tried in the expo project. These changes are for bare react-native project. |
@JKKholmatov
const { withAppBuildGradle } = require('@expo/config-plugins');
module.exports = withAppBuildGradleDependencies = (config, customName) => {
return withAppBuildGradle(config, (config) => {
const initialIndex = config.modResults.contents.indexOf("dependencies {");
config.modResults.contents =
config.modResults.contents.slice(0, initialIndex) +
`dependencies {
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.11'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:5.0.0-alpha.11'` +
config.modResults.contents.slice(initialIndex + "dependencies {".length);
return config;
});
};
"plugins": [
"./internal/expo-config-plugins/appBuildGradleDependencies.js",
It resolved my issue with ipv6 latency on Android |
Switching to OkHttp version 5 did not resolve the issue. However, I found a solution by creating a custom module for API calls that only functions when running on Android and the required VPN is active. Below is the implementation:
This module ensures the API calls work specifically with IPv4, mitigating connectivity issues observed when using the VPN. |
I'd be interested to understand why it breaks on VPN. If you configure an event listener in the client, you should see what it's doing with DNS and connection attempts. This should work without this workaround, so it would be good to find the root cause. |
The issue is actually rooted in how Android handles connections with the Happy Eyeballs algorithm, which tries both IPv4 and IPv6 connections to improve speed. On certain devices, though, this approach can cause requests to hang indefinitely when there’s an IPv6 host involved—especially problematic when using a VPN that primarily supports IPv4. To work around this, I created a custom module that forces OkHttp to resolve only IPv4 addresses, which has been effective in preventing the hang. Ideally, we’d look deeper into how Android handles the Happy Eyeballs fallback in VPN contexts and see if we can prevent this hang without restricting to IPv4. |
OkHttp tries to
Which section is hanging with a VPN? |
@yschimke |
Description
This is a very bizarre issue that has been previously reported a bunch of times, and this is basically a continuation of:
#29608
I initially started running into this on RN 0.66 with AWS Cognito. Bumping to 0.66.3 didn't help.
I'm also pretty sure this used to work before and I'm not sure when it broke. It's on an app that has been shelved for a while.
The problem is very strange because the network request does not seem to be issued, but simply hitting CMD+S to save any file so that a hot-reload is issued will immediately dispatch the network request.
I discovered the promise hanging issue by adding some logs to the
fetch
calls the cognito library was doing:Notice how the
.then
is not executed.While troubleshooting I came across a mention here of a workaround: #29608 (comment) (courtesy of @danmaas) which seems to completely resolve the issue.
Here's the same
.then
correctly being executed after applying that patch:Version
0.66.3
Output of
react-native info
Steps to reproduce
I'm able to reproduce it with this:
Output:
After applying #29608 (comment):
Snack, code example, screenshot, or link to a repository
No response
Skip to this comment for the actual cause: #32730 (comment)
The text was updated successfully, but these errors were encountered: