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 (show other bugs)
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)

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


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.

Note You need to log in before you can comment on or make changes to this bug.