You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
While investigating #2295@ljbade discovered that OkHttp and uv_async were not working properly together. The result was the decision to ship 0.1.0 using curl instead. This ticket will be about restoring OkHttp by working out the issues with uv_async as documented by @ljbade. Specifically:
on curl all completed HTTPRequest handling happens in the same thread that creates HTTPContext as curl uses a loop pump to process everything (this is enforced via MBGL_VERIFY_THREAD macro)
on NSUrl and OkHTTP we farm HTTPRequest out to the system so we get the result back async on a different thread from the one that created HTTPContext
that is why both NSUrl and OkHTTP use uv_async to hand the final request processing back to the HTTPContext thread
So to remove uv_async we need an alternative way to get the request back to the HTTPContext thread.
I think @kkaefer will need to help us here with Option C, or he might be able to persue Option B.
uv_async is a fine way to communicate results back to the originating thread, and "remove use of uv_async" is not the correct long-term fix for "undiagnosed crash connected to uv_async use". Instead, we should diagnose the root cause of the crash.
That line does the actual IO work of downloading the HTTP request. If something happens during that function either because of a network failure, or a cancellation request that function will throw an IOException
You can see that the onReponse callback has a throws IOException thus we are expected to forward the exception after we handle it. It we don't rethrow the IOException, OkHTTP might not correctly clean up when a request fails.
I've been running some scenarios and I've added a new application interceptor to OkHttp to help us with debugging: #2905 (comment)
To clarify the situation, we are not seeing the "callback gets called twice" situation anymore, instead, when we find an exception in onReponse we catch it and call onFailure manually to handle it. This is because OkHttp wouldn't do it otherwise -- it'd only call one ("signal the callback" in their terminology).
Another approach could be not to rethrow it and do a response.body().close() and nativeOnResponse() call only if no exception was found in onResponse. Otherwise, the SDK user will see exceptions logs potentially believing an exception was unhandled, when it was by our own onFailure.
@zugaldia@ljbade This is sounding like OkHttp is on pretty firm ground here. There's been no crashes reported on the internal testing app so far. If you two are both feeling good about this (I assume you both are?), let's polish the code up and get it into master.
@zugaldia Thanks for digging into. Since it seems like everyone gets confused by OkHTTP logging the exception despite it being harmless, perhaps we should just swallow the exception? If you compare master to 2400-lock you will see I used to just return. However I suspect that might have been what caused the double callback somehow.
Agreed. As the exception is directed to onFailure I don't think there's any need for the extra logging. Also, it's consistent with the way OkHttp works and therefore it'd match the dev expectations.
@ljbade , @tobrun , @bleege Is it possible to measure the performance overhead of OkHttp as well? In this case we will be copying 25K to 100K+ bytes for each tile from java to native using JNI versus the older one where this copy and the additional overhead of managed code(GC, etc.) is missing.
@zugaldia Excellent I think we are ready to merge, except for figuring out how to benchmark the perf improvements. However we could do this after merge.
Activity
jfirebaugh commentedon Sep 23, 2015
uv_asyncis a fine way to communicate results back to the originating thread, and "remove use ofuv_async" is not the correct long-term fix for "undiagnosed crash connected touv_asyncuse". Instead, we should diagnose the root cause of the crash.ljbade commentedon Sep 23, 2015
@jfirebaugh Yes let's fix the
uv_asyncbug.[-]Refactor OkHttp To Not Use uv_async (aka Option C)[/-][+]Fix uv_async crash (aka Option B)[/+]ljbade commentedon Sep 23, 2015
@kkaefer Where else have you noticed
uv_asynccrashing before? iOS/OSX/Linux?ljbade commentedon Oct 24, 2015
I should revisit this since @mikemorris updated the libuv version.
34 remaining items
ljbade commentedon Nov 10, 2015
@bleege I realise I should have not used
read()The line in question is
mapbox-gl-native/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPContext.java
Line 99 in c66aec9
That line does the actual IO work of downloading the HTTP request. If something happens during that function either because of a network failure, or a cancellation request that function will throw an
IOExceptionYou can see that the
onReponsecallback has athrows IOExceptionthus we are expected to forward the exception after we handle it. It we don't rethrow the IOException, OkHTTP might not correctly clean up when a request fails.@zugaldia Do you agree with my assesment?
zugaldia commentedon Nov 11, 2015
I've been running some scenarios and I've added a new application interceptor to OkHttp to help us with debugging: #2905 (comment)
To clarify the situation, we are not seeing the "callback gets called twice" situation anymore, instead, when we find an exception in
onReponsewe catch it and callonFailuremanually to handle it. This is because OkHttp wouldn't do it otherwise -- it'd only call one ("signal the callback" in their terminology).What I'm not sure at this point is if whether we should rethrow the
IOExceptionwithinonReponse. If I understand correctly OkHttp's behavior, that exception will simply get "swallowed" (see lysine-dev/okhttp#1335) meaning that it just gets logged without further cleaning (the relevant method is here: https://github.com/square/okhttp/blob/master/okhttp/src/main/java/com/squareup/okhttp/Call.java#L159). That's why @bleege saw #2400 (comment) which is in turn harmless.Another approach could be not to rethrow it and do a
response.body().close()andnativeOnResponse()call only if no exception was found inonResponse. Otherwise, the SDK user will see exceptions logs potentially believing an exception was unhandled, when it was by our ownonFailure.bleege commentedon Nov 12, 2015
@zugaldia @ljbade This is sounding like OkHttp is on pretty firm ground here. There's been no crashes reported on the internal testing app so far. If you two are both feeling good about this (I assume you both are?), let's polish the code up and get it into
master.Great job @zugaldia @ljbade for making this happen! 🚀
incanus commentedon Nov 12, 2015
[android] Bring back OkHTTP
ljbade commentedon Nov 12, 2015
@zugaldia Thanks for digging into. Since it seems like everyone gets confused by OkHTTP logging the exception despite it being harmless, perhaps we should just swallow the exception? If you compare master to 2400-lock you will see I used to just
return. However I suspect that might have been what caused the double callback somehow.tobrun commentedon Nov 12, 2015
👍
Seems logical
zugaldia commentedon Nov 12, 2015
Agreed. As the exception is directed to
onFailureI don't think there's any need for the extra logging. Also, it's consistent with the way OkHttp works and therefore it'd match the dev expectations.mb12 commentedon Nov 12, 2015
@ljbade , @tobrun , @bleege Is it possible to measure the performance overhead of OkHttp as well? In this case we will be copying 25K to 100K+ bytes for each tile from java to native using JNI versus the older one where this copy and the additional overhead of managed code(GC, etc.) is missing.
ljbade commentedon Nov 12, 2015
@zugaldia I just pushed a commit with return instead of rethrow... can you check it doesnt bring back double onresponse/onfailure?
zugaldia commentedon Nov 16, 2015
@ljbade FWIW I just did a fresh build with the latest code and I couldn't find the double callback call.
ljbade commentedon Nov 16, 2015
@zugaldia Excellent I think we are ready to merge, except for figuring out how to benchmark the perf improvements. However we could do this after merge.