Skip to content

Allow network requests to finish when app is sent to background #31838

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maxkomarychev
Copy link
Contributor

@maxkomarychev maxkomarychev commented Jul 9, 2021

Summary

This change allows network requests to be executed in background safely.

In certain conditions iOS HTTP requests are being terminated with this
error:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

While working on this issue I discovered this happens when app is
brought to background (sometimes several times).

Changelog:

[iOS] [Fixed] - Fix random network errors happening when application is in background

Test Plan

  1. Create sample express server:

    const express = require('express');
    const app = express();
    const port = 9999;
    
    app.get('/', (req, res) => {
      setTimeout(() => {
        res.send('Hello World!');
      }, 20000);
    });
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
    });
  2. Run the following code in react-native app:

    const App = () => {
      return (
        <View style={{margin: 30}}>
          <Button
            onPress={() => {
              console.log('sending a request');
              axios
                .get('http://192.168.0.110:9999')
                .then(r => {
                  console.log('response', r.data);
                })
                .catch(e => {
                  console.log('error', e);
                });
            }}
            title="REQUEST"
          />
        </View>
      );
    };
    
  3. Hit "REQUEST" button and send app to background few times for a few seconds each. The request should succeed in the end.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 9, 2021
@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch 2 times, most recently from 91a1621 to 772e843 Compare July 9, 2021 08:29
@maxkomarychev maxkomarychev changed the title Allow network requests to finish when app send to background Allow network requests to finish when app is sent to background Jul 9, 2021
@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch from 772e843 to 163776f Compare July 9, 2021 08:42
@maxkomarychev maxkomarychev marked this pull request as ready for review July 9, 2021 12:37
@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch from 163776f to 4791093 Compare August 11, 2021 18:58
@analysis-bot
Copy link

analysis-bot commented Aug 11, 2021

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,969,468 +11
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 9,561,974 -13
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: eddefec
Branch: main

@yungsters yungsters added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 10, 2021
@react-native-bot react-native-bot added Bug Platform: iOS iOS applications. labels Oct 1, 2021
@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch 2 times, most recently from 7be1ace to 807ad5b Compare October 29, 2021 08:42
@pull-bot
Copy link

PR build artifact for 807ad5b is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch from 807ad5b to c2cd30e Compare September 13, 2023 13:57
@maxkomarychev
Copy link
Contributor Author

kind bump here 👋

@maxkomarychev
Copy link
Contributor Author

Looks like e2e jobs are failing for reasons unrelated to the code. What can be done to fix this?
image

Copy link

This PR 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.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 13, 2024
This change allows network requests to be executed in background safely.

In certain conditions iOS HTTP requests are being terminated with this
error:

```
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
```

While working on this issue I discovered this happens when app is
brought to background (sometimes several times). This is easy to
reproduce on a real device, but doesn't happen often in simulator.

1. Create sample express server:

    ```js
    const express = require('express');
    const app = express();
    const port = 9999;

    app.get('/', (req, res) => {
      setTimeout(() => {
        res.send('Hello World!');
      }, 20000);
    });

    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
    });
    ```

2. Run the following code in react-native app:

    ```
    const App = () => {
      return (
        <View style={{margin: 30}}>
          <Button
            onPress={() => {
              console.log('sending a request');
              axios
                .get('http://192.168.0.110:9999')
                .then(r => {
                  console.log('response', r.data);
                })
                .catch(e => {
                  console.log('error', e);
                });
            }}
            title="REQUEST"
          />
        </View>
      );
    };
    ```

3. Hit "REQUEST" button and send app to background few times for a few
seconds each. The request should succeed in the end.
@maxkomarychev maxkomarychev force-pushed the network-works-in-background branch from c2cd30e to 6c3d97f Compare March 13, 2024 21:46
@maxkomarychev
Copy link
Contributor Author

This PR 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.

This pr is still relevant. Can it be looked at please? I'm not sure how others are dealing with the fact that network requests hang up when the app goes to background. We have to carry patch with "patch package" in all our apps because of this problem.

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 14, 2024
@dodokw
Copy link

dodokw commented Jun 19, 2024

i am so sad about this problem..... hope this pr should merge....

@react-native-bot
Copy link
Collaborator

This PR 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.

@react-native-bot react-native-bot added Stale There has been a lack of activity on this issue and it may be closed soon. and removed Stale There has been a lack of activity on this issue and it may be closed soon. labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Platform: iOS iOS applications. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants