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.
Crash in HTTP request when activity destroyed #2295
Following on from #1941 there is a crash in the HTTP request handling, related to HTTP requests that complete after an Activity and the corresponding MapView are destroyed.
DefaultFileSource needs an access token which we don't get until a MapView is created so I can't use it in JNI_OnLoad.
@kkaefer Is every MapView supposed to have it's own DefaultFileSource and cache? How can I correctly handle a MapView getting deleted before all pending HTTPRequest have completed?
I have been figuring out how DefaultFileSource works. When something inside Map wants to perform a HTTP request it calls a function on FileSource which then returns a Request object (this contains the HTTPRequest) which performs the request and eventually calls the given callback.
When MapView is deleted along with the native Map any pending request objects held have cancel() called on them and their pointer is set to nullptr. This means the request object is still alive and consuming memory etc.
Instead HTTPRequest is responsible for deleting itself and freeing up the memory somehow in response to cancel().
On Android the HTTPAndroidRequest::cancel() method simply calls cancel on the underlying Java OkHTTP Request object via JNI. This will result in either HTTPAndroidRequest::onResponse or HTTPAndroidRequest::onFailure getting called via JNI from OkHTTP.
Inside these an uv_async is used to call HTTPAndroidRequest::finish which is currently where it checks for a cancelled request and then performs delete this.
Currently it is crashing when it goes to trigger the uv_async object to call finish() and never makes it far enough to check if the request was canceled.
Only thing I can see why async might crash is that the loop it is created with becomes invalid. DefaultFileSource sets this to the current thread's RunLoop which could be deleted.
@kkaefer Do you have any insight into what is causing this crash and the related freezing in #2018#1941.
It seems to be related to the Android HTTP implementation as the app only crashes/freezes if the Activity is destroyed (e.g. by rotating the screen) while tiles are being loaded.
This is all good info @ljbade. I agree and think that a lot of this and the Database lock are being caused by the main Activity lifecyle methods not cleaning everything up as they should.
For those not as familiar with Android, when the device is rotated the Activity shuts itself down and then recreates itself. My theory is that the shutdown process is cleaning things up completely and / or fast enough before the activity is recreated (aka onCreate() is called).
Hit a new crash after giving android-curl a thrashing:
@ljbade Can you describe in more detail what you mean by "a thrashing"?
Reason being is that I just tried the android-curl branch out on an HTC M9 using the same testing that @incanus describes in #2295 (comment) and didn't see a crash. I was simultaneously mixing pans / flicks with rotations as well as turning GPS on and off. Didn't crash once (nor freeze).
@bleege I was rotating my phone back and forth as quickly as it would rotate while zooming and panning at the same time. Basically it would only crash after a large number of rotations.
I was thinking just before and I think @kkaefers nugget from yesterday might be the answer about why curl works but not OkHTTP.
@kkaefer said there was a known crash in uv_async that would happen very infrequently. However because of Android's Activity destruction/recreation cycle I think Android triggers this crash more frequently than the other platforms.
Now in Android's OkHTTP implementation we use uv_async as I modelled it on the NSUrl implementation which also uses uv_async.
However the curl implementation does not use uv_async. Hence why it does not crash.
So we have a few options:
a) switch back to curl
b) fix uv_async
c) refactor OkHTTP to not use uv_async
Per discussion with @ljbade@incanus we're going to leave android-curl branch as is (aka switch back to curl / Option A) and @ljbade is going to take a run at Option C on a separate branch today. If Option C works out then we'll go with it, but if not then we'll use Option A for 0.1.0 and keep working on Option C for future releases. Either way we'll have something to solve this issue for 23-Sept-2015 (North America).
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.
In the mean time I will merge the OkHTTP fixes (which includes a memory leak fix). We can then merge curl on top of this if required.
This is great to learn @ljbade. Thank you for looking into this and helping us all understand the issue as well as what needs to be done. Super helpful. 👍
Activity
ljbade commentedon Sep 10, 2015
First stab was to but an
if(!canceled)aroundmapbox-gl-native/platform/android/http_request_android.cpp
Line 304 in 532bbd5
I think issue might be we create a
DefaultFileSource(andHTTPContext) perMapViewin https://github.com/mapbox/mapbox-gl-native/blob/master/include/mbgl/android/native_map_view.hpp#L94 where it seems this should be a singleton.Going to create it in
JNI_OnLoadand share oneDefaultFileSourcefor allMapViewljbade commentedon Sep 10, 2015
DefaultFileSourceneeds an access token which we don't get until aMapViewis created so I can't use it inJNI_OnLoad.@kkaefer Is every
MapViewsupposed to have it's ownDefaultFileSourceand cache? How can I correctly handle aMapViewgetting deleted before all pendingHTTPRequesthave completed?ljbade commentedon Sep 10, 2015
I have been figuring out how
DefaultFileSourceworks. When something insideMapwants to perform a HTTP request it calls a function onFileSourcewhich then returns aRequestobject (this contains theHTTPRequest) which performs the request and eventually calls the given callback.When
MapViewis deleted along with the nativeMapany pending request objects held havecancel()called on them and their pointer is set tonullptr. This means the request object is still alive and consuming memory etc.Instead
HTTPRequestis responsible for deleting itself and freeing up the memory somehow in response tocancel().ljbade commentedon Sep 10, 2015
On Android the
HTTPAndroidRequest::cancel()method simply calls cancel on the underlying Java OkHTTPRequestobject via JNI. This will result in eitherHTTPAndroidRequest::onResponseorHTTPAndroidRequest::onFailuregetting called via JNI from OkHTTP.Inside these an
uv_asyncis used to callHTTPAndroidRequest::finishwhich is currently where it checks for a cancelled request and then performsdelete this.Currently it is crashing when it goes to trigger the
uv_asyncobject to callfinish()and never makes it far enough to check if the request was canceled.ljbade commentedon Sep 10, 2015
Came across a different JNI
nullcrash https://gist.github.com/ljbade/1aabde522c67549e2bccAlso trying to check for
cancelledat the start ofHTTPAndroidRequest::onResponseandHTTPAndroidRequest::finishstill results in theasynccrash.ljbade commentedon Sep 10, 2015
Only thing I can see why
asyncmight crash is that theloopit is created with becomes invalid.DefaultFileSourcesets this to the current thread'sRunLoopwhich could be deleted.ljbade commentedon Sep 11, 2015
Sometimes when rotating the app can also just freeze as in #2018 and #1941
Perhaps the crash and freeze are related somehow.
ljbade commentedon Sep 15, 2015
@kkaefer Do you have any insight into what is causing this crash and the related freezing in #2018 #1941.
It seems to be related to the Android HTTP implementation as the app only crashes/freezes if the Activity is destroyed (e.g. by rotating the screen) while tiles are being loaded.
ljbade commentedon Sep 15, 2015
Started digging into this via GDB debugging, but the Samsung S6 has broken NDK debug support.
Having to work from my Sony.
ljbade commentedon Sep 15, 2015
Got a backtrace for all threads when app is frozen - https://gist.github.com/ljbade/958c276e8000001c6926
ljbade commentedon Sep 15, 2015
There are a large number of threads and all of them are waiting in a syscall.
bleege commentedon Sep 15, 2015
This is all good info @ljbade. I agree and think that a lot of this and the Database lock are being caused by the main Activity lifecyle methods not cleaning everything up as they should.
For those not as familiar with Android, when the device is rotated the Activity shuts itself down and then recreates itself. My theory is that the shutdown process is cleaning things up completely and / or fast enough before the activity is recreated (aka
onCreate()is called).19 remaining items
ljbade commentedon Sep 22, 2015
Hit a new crash after giving
android-curla thrashing:Too many open filesmight be just a system limitation from too much thrashing of the rotation... or we have a file handle leak somewhere.ljbade commentedon Sep 22, 2015
Now I'm going to try
android-http-crashbranch.Hmm seems it freezes. But I'm out of time will pick up tomorrow.
bleege commentedon Sep 22, 2015
@ljbade Can you describe in more detail what you mean by "a thrashing"?
Reason being is that I just tried the
android-curlbranch out on an HTC M9 using the same testing that @incanus describes in #2295 (comment) and didn't see a crash. I was simultaneously mixing pans / flicks with rotations as well as turning GPS on and off. Didn't crash once (nor freeze).Am I monkey testing wrong?
ljbade commentedon Sep 22, 2015
@bleege I was rotating my phone back and forth as quickly as it would rotate while zooming and panning at the same time. Basically it would only crash after a large number of rotations.
I was thinking just before and I think @kkaefers nugget from yesterday might be the answer about why curl works but not OkHTTP.
ljbade commentedon Sep 22, 2015
@kkaefer said there was a known crash in
uv_asyncthat would happen very infrequently. However because of Android's Activity destruction/recreation cycle I think Android triggers this crash more frequently than the other platforms.Now in Android's OkHTTP implementation we use
uv_asyncas I modelled it on the NSUrl implementation which also usesuv_async.However the curl implementation does not use
uv_async. Hence why it does not crash.So we have a few options:
a) switch back to curl
b) fix
uv_asyncc) refactor OkHTTP to not use
uv_asyncincanus commentedon Sep 22, 2015
I am not seeing the crash under my previous test scenario as of d2d3df2.
Great work @ljbade!
bleege commentedon Sep 22, 2015
Per discussion with @ljbade @incanus we're going to leave
android-curlbranch as is (aka switch back to curl / Option A) and @ljbade is going to take a run at Option C on a separate branch today. If Option C works out then we'll go with it, but if not then we'll use Option A for0.1.0and keep working on Option C for future releases. Either way we'll have something to solve this issue for 23-Sept-2015 (North America).ljbade commentedon Sep 23, 2015
@kkaefer @bleege @incanus I started look at how to do Option C.
However I discovered:
HTTPRequesthandling happens in the same thread that createsHTTPContextas curl uses a loop pump to process everything (this is enforced viaMBGL_VERIFY_THREADmacro)HTTPRequestout to the system so we get the result back async on a different thread from the one that createdHTTPContextuv_asyncto hand the final request processing back to theHTTPContextthreadSo to remove
uv_asyncwe need an alternative way to get the request back to theHTTPContextthread.I think @kkaefer will need to help us here with Option C, or he might be able to persue Option B.
In the mean time I will merge the OkHTTP fixes (which includes a memory leak fix). We can then merge curl on top of this if required.
bleege commentedon Sep 23, 2015
This is great to learn @ljbade. Thank you for looking into this and helping us all understand the issue as well as what needs to be done. Super helpful. 👍
So, with all that in mind here's what we'll do:
android-curlin tomasteras the "fix" for Crash in HTTP request when activity destroyed #2295 for0.1.00.1.0on time0.2.0uv_asyncandOkHttpto play togetherbleege commentedon Sep 23, 2015
Option C is now documented and scheduled for work in
0.2.0at #2400.