facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.37k stars 24.36k forks source link

Fetch stuck in (pending) iOS #43136

Open Johnsgaard opened 9 months ago

Johnsgaard commented 9 months ago

Description

https://github.com/apollographql/apollo-client/issues/11608

I have opened an issue with apollo/client as I was under the assumption that it was an issue with that library. It turns out that this might be something that is more on the react-native / iOS specific side of things.

Steps to reproduce

  1. git clone git@github.com:Johnsgaard/fogtown.git
  2. npm install
  3. npm run ios (assuming you have expo)

React Native Version

0.73.4

Affected Platforms

Runtime - iOS

Output of npx react-native info

System:
  OS: macOS 14.1.2
  CPU: (10) arm64 Apple M1 Pro
  Memory: 994.88 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.10.0
    path: ~/.nvm/versions/node/v20.10.0/bin/node
  Yarn: Not Found
  npm:
    version: 10.2.3
    path: ~/.nvm/versions/node/v20.10.0/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods: Not Found
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: Not Found
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java: Not Found
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

No stack trace. Network request on iOS is locked on (pending) when making a request.

Reproducer

https://github.com/Johnsgaard/fogtown

Screenshots and Videos

![Uploading Screenshot 2024-02-21 at 10.25.22 AM.png…]()

Johnsgaard commented 9 months ago
Screenshot 2024-02-21 at 10 25 22 AM
mekanics commented 8 months ago

I'm having the same issue, but with redux-toolkit & rtk query.

First I thought there was an issue with the pollingInterval of the queries, but at some point I was able to observe a request being sent, but never resolved in Flipper.

Reinstalling the app helps ... for a day or two.

Currently I'm using react-native v0.73.5 (upgraded while trying to solve that issue), but the problem already occurred with v0.72.7.

I wasn't able to see the request when using a proxy like proxyman.

Maybe it has something to do with:

mekanics commented 7 months ago

I was able to solve the issue in our project by replacing @babel/plugin-proposal-class-properties, which is deprecated, with @babel/plugin-transform-class-properties in the @react-native/babel-preset lib.

here is the patch I use

diff --git a/node_modules/@react-native/babel-preset/src/configs/main.js b/node_modules/@react-native/babel-preset/src/configs/main.js
index e921b1b..a45aafa 100644
--- a/node_modules/@react-native/babel-preset/src/configs/main.js
+++ b/node_modules/@react-native/babel-preset/src/configs/main.js
@@ -28,7 +28,7 @@ const defaultPlugins = [
   [require('@babel/plugin-syntax-flow')],
   [require('babel-plugin-transform-flow-enums')],
   [require('@babel/plugin-transform-block-scoping')],
-  [require('@babel/plugin-proposal-class-properties'), {loose}],
+  [require('@babel/plugin-transform-class-properties'), {loose}],
   [require('@babel/plugin-transform-private-methods'), {loose}],
   [require('@babel/plugin-transform-private-property-in-object'), {loose}],
   [require('@babel/plugin-syntax-dynamic-import')],
Johnsgaard commented 7 months ago

@mekanics Amazing 👏

I updated my @react-native/babel-preset but I still can't seem to get it to respond. I am wondering if I am doing something wrong when bundling the app for iOS. This will require an up-stream change I assume?

Are you using expo to bundle / simulate your app?

Johnsgaard commented 7 months ago

I also confirmed that the endpoint works inside the safari of the simulated iOS device:

Screenshot 2024-03-27 at 2 26 15 PM

I couldn't get the plugin change to work though 😕

mekanics commented 7 months ago

@mekanics Amazing 👏

I updated my @react-native/babel-preset but I still can't seem to get it to respond. I am wondering if I am doing something wrong when bundling the app for iOS. This will require an up-stream change I assume?

Are you using expo to bundle / simulate your app?

No I'm not using expo.

Is your issue always occurring? I'm asking because, I had a hard time testing/reproducing it. The problem only occurred after some time (mostly after a day or two).

Johnsgaard commented 7 months ago

Yeah, unfortunately I can't get it passed the (pending) step no matter what I do.

The strange thing is it works in android, curl requests are completely fine, but when I switch out the apollo-client to another api endpoint it returns in iOS. This makes me feel like it is a server configuration on my end but I can't figure it out for the life of me. I might just move my API to another host and see if that helps in anyway. Probably something to do with the SSL certs or something internal to apple that I have no idea how to resolve 😛.

rodrigodiasf1984 commented 5 months ago

Any update on this? I've the same problem at the RN074.1

hakonk commented 4 months ago

I recently fixed a data race in RCTNetworkTask related to the status of the task. Probably a long shot, but do you have any thoughts on whether this fix may affect the issue you are describing here: https://github.com/facebook/react-native/issues/44694 ?

hakonk commented 4 months ago

If not, it would be interesting to know how the mentioned example behaves with breakpoints enabled to see how it ends up being stuck in pending mode.

hakonk commented 4 months ago

@Johnsgaard FYI 👆

Johnsgaard commented 4 months ago

@hakonk I will take a look ASAP. Do you have a version number?

hakonk commented 4 months ago

@Johnsgaard it's not released yet, so I guess you need to fetch the branch in the PR I submitted

Johnsgaard commented 4 months ago

@hakonk I can't seem to install react-native#fix/data-race-RCTNetworkTask branch. Will you update me when this merges and I will try to update my react-native version and test this.

hakonk commented 4 months ago

@Johnsgaard did you set my fork of the repo instead of Facebook's?

hakonk commented 4 months ago

@Johnsgaard it's been merged into main now: https://github.com/facebook/react-native/commit/b1ec698dc4baf34ba2e31b7ad43dff97c229bf99

Johnsgaard commented 4 months ago

@hakonk thanks! I will give it a go.

jaaywags commented 3 months ago

Was this part of the 0.74.4 or 0.74.5 release?

I am having (I think) the same issue in my React Native Expo app.

"expo": "~51.0.22"
"react-native": "0.74.5", (just upgraded from 0.74.3)

I even tried expo version 51.0.26.

How can I try applying your PR changes to my app if they are not released yet?

UPDATE: I am sorry. IDK why but I just deleted my android emulator and created a new one and now all my api calls work. None of them were working before (android only). They just got "stuck" which is why I landed here. They worked fine on ios simulator and now on the new emulator.

hakonk commented 3 months ago

@jaaywags My PR only pertains to iOS. Not sure if it did resolve the issue @Johnsgaard experienced. Since then, I have had other PRs related to fixing data races merged. I believe 0.74.5 should contain all relevant to this issue. I have however not checked whether they fix this issue. I was merely curious whether the fixes could fix this as well.

jaepang commented 1 month ago

@hakonk Hey! Thank you for PR for solving issue. I reproduced this pending issue when request is redirected with http status code 307.

My version is this:

"expo": "~51.0.36",
"react-native": "0.74.5",

And my request is POST to POST redirection.

hakonk commented 1 month ago

@hakonk

Hey! Thank you for PR for solving issue. I reproduced this pending issue when request is redirected with http status code 307.

My version is this:


"expo": "~51.0.36",

"react-native": "0.74.5",

And my request is POST to POST redirection.

Hey @jaepang ! I'm happy it worked for you. Have you been able to pinpoint the fix to my PR?

Nehal-Zeller commented 2 weeks ago

@hakonk could you help me with a weird behaviour I'm facing on my react-native app (built using cli) When I put my application in background on ios, network calls from all 3-party sdks are I think getting blocked but network calls fired to our own backend server has request body size of 174.
Also, this happens only on production, so I'm clueless what's happening here exactly and how to reproduce it

expected behaviour: All network calls should go through once fired even if app is in background.

I have backgroundFetch and Remote notifications enabled under xcode >> app target >> background modes

Screenshot 2024-11-05 at 9 56 18 AM Screenshot 2024-11-05 at 11 13 32 AM

This has become a major production issue for us, your help would be really appreciated, Thank youu 🙏🏽

hakonk commented 2 weeks ago

@Nehal-Zeller hi! I'm not sure if the issue you are experiencing is related to my PR.

When you say third party sdks, have you verified through which network stack they make network requests? Are they using fetch as well?

Nehal-Zeller commented 2 weeks ago

@hakonk Thank you for your response :) yes, react-native-auth0 and LaunchDarkly uses fetch under the hood. Although it's might not be related to your PR but do you know, why these network calls are coming with 0 request_body_size and happens only on iOS and when app is in background. Thanks again 🙏

hakonk commented 2 weeks ago

@Nehal-Zeller sorry, I wouldn't know unless I specifically debugged your code. Good luck!