Bug 16849 - http(s) requests do not detect network is down
Summary: http(s) requests do not detect network is down
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.12.0
Hardware: PC Windows
: Low normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2013-12-17 11:53 UTC by Gustavo Armenta
Modified: 2017-06-28 18:50 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Gustavo Armenta 2013-12-17 11:53:05 UTC
Start a http(s) request on Xamarin Android or iOS and the server side never returns a response. Disconnect network cable, or on a physical device change to Airplane Mode. The standing request does not trigger a disconnection immediatly.

Open attached solution
Run sample in an Android API 15 emulator
Let the sample run for 5 minutes, notice http requests are cancelled every 100 seconds
When a request has just started, disconnect network cable, notice the application does not detect the failure, the request is cancelled after 100 seconds.
Comment 1 Jonathan Pryor 2013-12-17 12:22:18 UTC
I'm not entirely sure what could be done to improve this. I see no events on Android to be notified of when the network is disconnected, which means we're left with POSIX socket(2)/connect(2).

POSIX could error out, e.g. write(2) could error with ENETDOWN, but there's not necessarily any timeliness guarantee for when (if ever) write(2) would error out.

Which is a longwinded way of saying that I have no idea how to solve this, and timeouts are likely the sanest solution anyway.
Comment 2 Tom Opgenorth 2017-06-28 18:50:23 UTC
It is possible to monitor the network connection in Android using the ConnectivityManager class (subject to some restrictions). See https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html.

An Android application may also register a BroadcastReceiver, listening for `android.net.conn.CONNECTIVITY_CHANGE` actions. Note that starting in Android 7.0 this broadcast receiver must be dynamically registered at runtime.

Finally, one could also use the Firebase Job Dispatcher. This library will allow a background job to execute when network changes happen. The job could then take the appropriate action so that the App can handle the disconnected network.