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

Listener randomly not triggered when turning on/off wifi or airplane mode #542

Closed
CptMaumau opened this issue Nov 26, 2021 · 24 comments · Fixed by #547
Closed

Listener randomly not triggered when turning on/off wifi or airplane mode #542

CptMaumau opened this issue Nov 26, 2021 · 24 comments · Fixed by #547
Labels
bug Something isn't working

Comments

@CptMaumau
Copy link

Environment

System:
OS: macOS 11.6
CPU: (8) x64 Apple M1
Memory: 17.72 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.5.0 - ~/.nvm/versions/node/v12.5.0/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v12.5.0/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v12.5.0/bin/npm
Watchman: 2021.08.30.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.11.0 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0
Android SDK: Not Found
IDEs:
Android Studio: 2020.3 AI-203.7717.56.2031.7784292
Xcode: 13.1/13A1030d - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_144 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: 0.63.3 => 0.63.3
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Platforms

Android

Versions

  • Android: 12
  • iOS:
  • react-native-netinfo: 7.1.2
  • react-native: 0.63.3
  • react: 16.13.1

Description

When only wifi connected, the network change listener is randomly not triggered when turning on/off wifi or airplane mode.

Reproducible Demo

  componentDidMount() {
    this.netListenerUnsub = NetInfo.addEventListener(this.handleNetworkChange);
  }

  handleNetworkChange = (state: NetInfoState) => {
    const { AppActions } = this.props;

    console.log('network state changed', state);
    AppActions.setNetwork(state.isConnected);
  };

turn on/off wifi or airplane mode.

@CptMaumau CptMaumau added the bug Something isn't working label Nov 26, 2021
@CptMaumau
Copy link
Author

With more testing it happens mostly when turning airplane mode on.
I am testing on a Pixel 6.

@mikehardy
Copy link
Contributor

With apologies I'm not aware of anyone currently that's responding to these issues that has time to dig in here on your behalf. To set expectations, you'll need to do the related work to diagnose, though I can collaborate on any possible fixes and get a PR merged+released.

If this were my project I would immediately want the answer to the question "what exactly are the native platform APIs being called with as parameters, and when exactly are the native platform APIs being called / what are they returning?"

That implies cracking open the module code in node_modules and adding logging all over at the Java level like System.err.println("this thing " + thing.toString()); etc, persisting it with patch-package and getting after it with reproductions and isolation of the problem.

Based on your detailed findings there I or someone else might be able to help

@cristianoccazinsp
Copy link
Contributor

Same issue, and possibly related to #537 ?

I believe on 6.2.1 something was overlooked and it stopped working (#510)

cristianoccazinsp pushed a commit to cristianoccazinsp/react-native-netinfo that referenced this issue Dec 2, 2021
@cristianoccazinsp
Copy link
Contributor

@CptMaumau can you test if the changes here resolve the issue for you? https://github.com/cristianoccazinsp/react-native-netinfo/tree/android-fix

@matyj
Copy link

matyj commented Dec 3, 2021

@cristianoccazinsp it works for me

@cristianoccazinsp
Copy link
Contributor

cristianoccazinsp commented Dec 3, 2021

Sadly, it doesn’t fix another probably more important bug. When the app goes to background for a while there’s a high chance that the library reports no connection and then never reports connection on app resume, something that worked fine with 6.2.0, so I wouldn’t take this change as an actual fix.

@matyj
Copy link

matyj commented Dec 3, 2021

@cristianoccazinsp Is there any version which works fine?

@mikehardy
Copy link
Contributor

So far, it seems like (if I understand this correctly @cristianoccazinsp ...) the only methods that work fine are the deprecated ones. Is that right?

@cristianoccazinsp
Copy link
Contributor

cristianoccazinsp commented Dec 3, 2021

Was 6.2.0 using a deprecated method? That one does not have this nor the other (#537) issue, but with 6.2.1 (#510) the way of getting the connection info (always querying it rather than using the semantics of the event, such as "on lost, make connection None") causes the app to randomly return wrong connection info.

So an immediate fix: downgrade to 6.2.0 until the changes in 6.2.1 can be fixed with a proper solution.

@mikehardy
Copy link
Contributor

@cristianoccazinsp I think at this point your the expert on it, and there was quite a bit of discussion related to the revert simply being best. If you want to propose something that just reverts the connection handling to 6.2.0 style I think that may be the best way. I thought was a step forward but clearly not

@cristianoccazinsp
Copy link
Contributor

I believe 6.2.1 fixed something related to Android 12, so I think the best would be to fix it and not revert it, but the issues are so tricky to debug that I've run out of ideas and time to keep debugging it.

@mikehardy
Copy link
Contributor

Android 12 currently 1-2% market share on my apps. Growing of course, but sometimes it's best of all evil

@cristianoccazinsp
Copy link
Contributor

@matyj @CptMaumau can you test the newest commit from https://github.com/cristianoccazinsp/react-native-netinfo/tree/android-fix ? I believe I got a fix for both this and the other issue.

mikehardy pushed a commit that referenced this issue Dec 7, 2021
* Test a possible fix for #542
For review.
* Combine 6.2.0 and 6.2.1 fixes into one.

Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
github-actions bot pushed a commit that referenced this issue Dec 7, 2021
## [7.1.4](v7.1.3...v7.1.4) (2021-12-07)

### Bug Fixes

* **android:** try async state fetch as stale state workaround ([#547](#547)) ([937cf48](937cf48)), closes [#542](#542)
@Rebsos
Copy link

Rebsos commented Dec 18, 2021

It didn't work.

  1. Start App with airplane mode
  2. Turn off airplane mode
  3. Netinfo: {"details": {}, "isConnected": false, "isInternetReachable": false, "isWifiEnabled": false, "type": "unknown"}

NetInfo: 7.1.6
RN: 0.64.2

@mikehardy
Copy link
Contributor

@Rebsos "PRs happily accepted", as are any really detailed things you can determine and report from your investigation of the issue as you go into node_modules and instrument the code. Fixes here will require troubleshooting and fixes from people with affected devices + use cases - I'm happy to collaborate but this is not a use case of mine professionally, and it's not something I feel like working on in personal time

@cristianoccazinsp
Copy link
Contributor

Does it happen all the time? I’m unable to reproduce it on a pixel 5 with android 12.

There’s also another issue (from long time before) when starting the app without wifi or airplane mode. At app startup, the connection always seem to be unknown or connected, but it eventually resolves itself.

@Rebsos
Copy link

Rebsos commented Dec 18, 2021

Yes all the time. It is 100% reproducible. Did you try it with RN 64.2?

I've setup two fresh projects, just with NetInfo and a button.

  1. kill app 2. turn airplane mode on 3. start app 4. turn airplane mode off 5. then NetInfo.fetch()...
  • RN 64.2 + NetInfo 7.1.6: works not (see above)
  • RN 66.4 + NetInfo 7.1.6: works

Devices: Samsung Galaxy S10+ Android 11. I think all devices are affected.

@cristianoccazinsp
Copy link
Contributor

That may explain it, I’m using RN 0.66.3.

Is it only fetch that fails? What about the listeners? Do they work as expected.

I don’t know what’s different between RN 0.64 and 0.66, and I don’t really have time to test right now.

@cristianoccazinsp
Copy link
Contributor

@Rebsos actually I know what's the difference.

This PR #548 added code that relies on addListener and removeListeners, two methods for Android that became important in RN 0.65 or 0.66 (and I think were not used before that). Perhaps this change makes it not compatible with RN 0.64.

What about using netinfo 7.1.5 ?

I'm sorry I cannot be of any further help, I would suggest just upgrading RN if 0.64 is the problem.

When I get some time, I will try to debug a bit more into the other issue where the network connectivity is not correctly reported at app startup until there's an actual network change.

@Rebsos
Copy link

Rebsos commented Dec 18, 2021

You found something

RN 64.2 + NetInfo 7.1.5 is working

I've tested it multiple times. Something breaks from 7.1.5 to 7.1.6

@cristianoccazinsp
Copy link
Contributor

It’s what I described above. 7.1.6 added a change that will only work from RN 0.65 and above.

@mikehardy @HatCloud thoughts on changing #548 so it works for all RN versions?

@mikehardy
Copy link
Contributor

Or just say, min supported version of rn for that version and higher is 65. 64 doesn't even build clean on xcode 13, it's hard to bring much attention to it. If someone needs 64, stay on older version of netinfo?

@cristianoccazinsp
Copy link
Contributor

I would be fine with that too. Although that version update should have been a major update then hehe, but I wouldn’t mind a simple note in the change log and read me about it.

@mikehardy
Copy link
Contributor

Agreed that's an unintentional breaking change, that happens sometimes and is always regrettable but occasionally is just life

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

Successfully merging a pull request may close this issue.

5 participants