dart-lang / http

A composable API for making HTTP requests in Dart.
https://pub.dev/packages/http
BSD 3-Clause "New" or "Revised" License
1.01k stars 351 forks source link

[ios] [CupertinoClient] http connection issue: nw_path_necp_check_for_updates Failed to copy updated result (22) #972

Open delfme opened 1 year ago

delfme commented 1 year ago

We are facing http connection issue when using CupertinoClient.

CC: @brianquinlan would you be so kind as to take a quick look at this? Just noticed you might know why this occurs.

To reproduce:

Note: Issue occurs frequently if device is connected to Internet via a VPN.

It seems this issue occurred also in the past, maybe a regression? https://github.com/flutter/flutter/issues/111940

Code sample

Code is simple, as per instructions from http package Readme. Use below ConnectionService.client to call whatever rest api on iOS. Note: We needed to use CupertinoClient coz connectivity with standard dart client was pretty unstable.

class ConnectionService {
  static const String _debugPrefix = 'ConnectionService:';

  static final config = URLSessionConfiguration.ephemeralSessionConfiguration()
    ..allowsCellularAccess = false
    ..allowsConstrainedNetworkAccess = false
    ..allowsExpensiveNetworkAccess = false;

  // Initialize client. It will be CupertinoClient on iOS and IOClient on android
  static dynamic client = (Platform.isIOS) ? CupertinoClient.fromSessionConfiguration(config)
      : IOClient();

Logs

Error from console is: console nw_path_necp_check_for_updates Failed to copy updated result (22) Note: We have this issue with both cupertino_http: 1.0.0 and 0.1.2

Flutter doctor

[✓] Flutter (Channel master, 3.12.0-7.0.pre.22, on macOS 12.6.5 21G531 darwin-arm64, locale en-IT) [!] Android toolchain - develop for Android devices (Android SDK version 31.0.0) ✗ cmdline-tools component is missing Run path/to/sdkmanager --install "cmdline-tools;latest" See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run flutter doctor --android-licenses to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.2) [✓] Connected device (3 available) [✓] Network resources

brianquinlan commented 1 year ago

Hi @delfme ,

I'm not sure why this happens?

Does this happen while your app is in the foreground, background or both?

Do you have the com.apple.security.network.client entitlement set?

delfme commented 1 year ago

Hi @brianquinlan thx for the kind support!

It surely happens while app is in foreground. Couldn't check what happens with app in background.

I added com.apple.security.network.client to entitlement. Couldn't find it from capabilities panel (is that a config. issue?), so added it manually to Runner.entitlements files (pic below). Is that the same?

Screenshot 2023-07-01 at 08 55 09 Screenshot 2023-07-01 at 08 55 21

Will test things and revert.

brianquinlan commented 1 year ago

Actually, the entitlement is not relevant for your configuration.

There isn't really much for me to use to diagnose the problem here. Do you see an error if you use IOClient?

delfme commented 1 year ago

Issue with IOClient was slow connection time on iOS and being a bit unstable. That brought us to turn to CupertinoClient.

delfme commented 1 year ago

It would be great if CupertinoClient could be tested more under VPN and different network condition. The feeling is that there is a minor bug that is somehow triggered by these conditions.

brianquinlan commented 1 year ago

Since CupertinoClient relies on Apple APIs for network transport, any bugs related to network conditions would most likely be related to those APIs.

Regarding https://github.com/dart-lang/http/issues/972#issuecomment-1622546678 - I meant have you tested the scenario that demonstrates this bug using IOClient?

andynewman10 commented 1 year ago

I am having the same issue. No http request is failing properly speaking, and I also see that keep-alive is working as expected.

Still, I can see there is an important slowdown when running my application, at least in debug mode (USB debugging).

Not sure it is related but - more worrying perhaps - I am sometimes seeing lots (and I say lots) of

[Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection]

errors, as reported in the comments here:

https://stackoverflow.com/questions/76397964/client-called-nw-connection-copy-protocol-metadata-internal-on-unconnected-nw-co

My setup is described in the comments in the link above. I am using http 1.1.0.

I can't recall what triggered this new behavior.

andynewman10 commented 12 months ago

Since CupertinoClient relies on Apple APIs for network transport, any bugs related to network conditions would most likely be related to those APIs.

Regarding #972 (comment) - I meant have you tested the scenario that demonstrates this bug using IOClient?

@brianquinlan Yes, there is the bug when simply using IOClient on iOS. I can reproduce the bug on iOS 16.6 with the following setup:

To reproduce the problem, issue eg. 100 http requests with, say, a 1-second interval. From times to times, you will see the issue.