Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Crash in HTTP request when activity destroyed #2295

Closed

Description

@ljbade

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.

Here is the crash log:

09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ Build fingerprint: 'samsung/zerofltedv/zeroflte:5.1.1/LMY47X/G920IDVU2COGA:user/release-keys'
09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ Revision: '11'
09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ ABI: 'arm'
09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ pid: 14785, tid: 14911, name: Tkj2y_5pUcsWvCQ  >>> com.mapbox.mapboxgl.testapp <<<
09-10 10:22:53.491    2964-2964/? I/DEBUG﹕ signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x3c
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ r0 00000000  r1 00000000  r2 00000026  r3 0001b7a6
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ r4 55f1761d  r5 db7ff8a4  r6 db7ff898  r7 db7ff8d4
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ r8 000000c8  r9 fffff508  sl db7ff8c8  fp ee9cc7e0
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ ip e14055e8  sp db7ff7a8  lr e118502c  pc e12e8068  cpsr 600e0010
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ backtrace:
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ #00 pc 00337068  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (uv_async_send+8)
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ #01 pc 001d4028  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (mbgl::HTTPAndroidRequest::onResponse(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::_
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ #02 pc 001d1550  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (mbgl::nativeOnResponse(_JNIEnv*, _jobject*, long long, int, _jstring*, _jstring*, _jstring*, _jstring*, _jstring*, _jbyteArray*)+1620)
09-10 10:22:53.501    2964-2964/? I/DEBUG﹕ #03 pc 00213d7f  /data/dalvik-cache/arm/data@[email protected]@[email protected] (void com.mapbox.mapboxgl.http.HTTPContext$HTTPRequest.nativeOnResponse(long, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, byte[])+258)

/cc @bleege

Activity

  1. added
    Android
    on Sep 10, 2015
  2. added this to the android-v0.1.0 milestone on Sep 10, 2015
  3. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    First stab was to but an if(!canceled) around

    but just moved crash elsewhere.

    I think issue might be we create a DefaultFileSource (and HTTPContext) per MapView in 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_OnLoad and share one DefaultFileSource for all MapView

  4. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    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?

  5. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    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().

  6. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    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.

  7. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    Came across a different JNI null crash https://gist.github.com/ljbade/1aabde522c67549e2bcc

    Also trying to check for cancelled at the start of HTTPAndroidRequest::onResponse and HTTPAndroidRequest::finish still results in the async crash.

  8. ljbade commented on Sep 10, 2015

    @ljbade
    ContributorAuthor

    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.

  9. ljbade commented on Sep 11, 2015

    @ljbade
    ContributorAuthor

    Sometimes when rotating the app can also just freeze as in #2018 and #1941

    Perhaps the crash and freeze are related somehow.

  10. ljbade commented on Sep 15, 2015

    @ljbade
    ContributorAuthor

    @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.

  11. ljbade commented on Sep 15, 2015

    @ljbade
    ContributorAuthor

    Started digging into this via GDB debugging, but the Samsung S6 has broken NDK debug support.

    Having to work from my Sony.

  12. ljbade commented on Sep 15, 2015

    @ljbade
    ContributorAuthor
  13. ljbade commented on Sep 15, 2015

    @ljbade
    ContributorAuthor

    There are a large number of threads and all of them are waiting in a syscall.

  14. bleege commented on Sep 15, 2015

    @bleege
    Contributor

    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).

    activity_lifecycle

  15. 19 remaining items

  16. ljbade commented on Sep 22, 2015

    @ljbade
    ContributorAuthor

    Hit a new crash after giving android-curl a thrashing:

    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ Build fingerprint: 'samsung/zerofltedv/zeroflte:5.1.1/LMY47X/G920IDVU2DOI2:user/release-keys'
    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ Revision: '11'
    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ ABI: 'arm'
    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ pid: 788, tid: 1950, name: NativeMapView::  >>> com.mapbox.mapboxgl.testapp <<<
    09-22 17:19:34.861    2966-2966/? I/DEBUG﹕ signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ Abort message: 'sart/runtime/indirect_reference_table.cc:77] Check failed: table_mem_map_.get() != nullptr ashmem_create_region failed for 'indirect ref table': Too many open files'
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ r0 00000000  r1 0000079e  r2 00000006  r3 00000000
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ r4 a820bdb8  r5 00000006  r6 00000002  r7 0000010c
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ r8 00000001  r9 f486f580  sl a4257800  fp a3041e80
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ ip 0000079e  sp a8206b90  lr f70c8cf9  pc f70eb6a8  cpsr 60070010
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ backtrace:
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #00 pc 0003a6a8  /system/lib/libc.so (tgkill+12)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #01 pc 00017cf5  /system/lib/libc.so (pthread_kill+52)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #02 pc 00018907  /system/lib/libc.so (raise+10)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #03 pc 000151a5  /system/lib/libc.so (__libc_android_abort+36)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #04 pc 00012fec  /system/lib/libc.so (abort+4)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #05 pc 002424cf  /system/lib/libart.so (art::Runtime::Abort()+170)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #06 pc 000ad7f1  /system/lib/libart.so (art::LogMessage::~LogMessage()+1360)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #07 pc 0017444d  /system/lib/libart.so (art::IndirectReferenceTable::IndirectReferenceTable(unsigned int, unsigned int, art::IndirectRefKind)+432)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #08 pc 001b4ba1  /system/lib/libart.so (art::JNIEnvExt::JNIEnvExt(art::Thread*, art::JavaVMExt*)+32)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #09 pc 002528cf  /system/lib/libart.so (art::Thread::Init(art::ThreadList*, art::JavaVMExt*)+354)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #10 pc 002574e1  /system/lib/libart.so (art::Thread::Attach(char const*, bool, _jobject*, bool)+128)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #11 pc 00240681  /system/lib/libart.so (art::Runtime::AttachCurrentThread(char const*, bool, _jobject*, bool)+16)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #12 pc 001d7c11  /system/lib/libart.so (art::JII::AttachCurrentThread(_JavaVM*, _JNIEnv**, void*)+216)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #13 pc 000c743b  /system/lib/libart.so (art::CheckJII::AttachCurrentThread(_JavaVM*, _JNIEnv**, void*)+110)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #14 pc 0009efb3  /system/lib/libandroid_runtime.so (android::JNISurfaceTextureContext::getJNIEnv(bool*)+50)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #15 pc 0009f0c9  /system/lib/libandroid_runtime.so (android::JNISurfaceTextureContext::onFrameAvailable(android::BufferItem const&)+12)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #16 pc 0002fcb5  /system/lib/libgui.so (android::ConsumerBase::onFrameAvailable(android::BufferItem const&)+108)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #17 pc 0002b80b  /system/lib/libgui.so (android::BufferQueue::ProxyConsumerListener::onFrameAvailable(android::BufferItem const&)+26)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #18 pc 0002edef  /system/lib/libgui.so (android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)+574)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #19 pc 0003b179  /system/lib/libgui.so (android::Surface::queueBuffer(ANativeWindowBuffer*, int)+412)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #20 pc 0003a31b  /system/lib/libgui.so (android::Surface::hook_queueBuffer(ANativeWindow*, ANativeWindowBuffer*, int)+10)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #21 pc 0022f214  /system/vendor/lib/egl/libGLES_mali.so
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #22 pc 002bd4f0  /system/vendor/lib/egl/libGLES_mali.so
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #23 pc 002bde8c  /system/vendor/lib/egl/libGLES_mali.so (eglSwapBuffers+1796)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #24 pc 00013b0b  /system/lib/libEGL.so (eglSwapBuffers+322)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #25 pc 00115bc4  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (mbgl::android::NativeMapView::afterRender()+28)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #26 pc 001cdd00  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (mbgl::MapContext::renderSync(mbgl::TransformState const&, mbgl::FrameData const&)+592)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #27 pc 001ded8c  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (std::__1::packaged_task<bool ()>::operator()()+104)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #28 pc 001decd4  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (_ZN4mbgl4util7RunLoop7InvokerINSt3__113packaged_taskIFbvEEENS3_5tupleIJEEEEclEv+44)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #29 pc 0019638c  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (mbgl::util::RunLoop::process()+352)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #30 pc 003ce74c  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #31 pc 003ce978  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #32 pc 003da2e0  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #33 pc 003cee38  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (uv_run+340)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #34 pc 001e4fd0  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (_ZN4mbgl4util6ThreadINS_10MapContextEE3runINSt3__15tupleIJRNS_4ViewERNS_10FileSourceERNS_7MapDataEEEEJLj0ELj1ELj2EEEEvNS0_13ThreadContextEOT_NS5_16integer_sequenceIjJXspT0_EEEE+144)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #35 pc 001e4ecc  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (_ZZN4mbgl4util6ThreadINS_10MapContextEEC1IJRNS_4ViewERNS_10FileSourceERNS_7MapDataEEEERKNS0_13ThreadContextEDpOT_ENKUlvE_clEv+220)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #36 pc 001e4da0  /data/app/com.mapbox.mapboxgl.testapp-1/lib/arm/libmapbox-gl.so (_ZNSt3__114__thread_proxyINS_5tupleIJZN4mbgl4util6ThreadINS2_10MapContextEEC1IJRNS2_4ViewERNS2_10FileSourceERNS2_7MapDataEEEERKNS3_13ThreadContextEDpOT_EUlvE_EEEEEPvSM_+88)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #37 pc 000174e7  /system/lib/libc.so (__pthread_start(void*)+30)
    09-22 17:19:34.911    2966-2966/? I/DEBUG﹕ #38 pc 00015503  /system/lib/libc.so (__start_thread+6)
    09-22 17:19:37.101    2966-2966/? I/DEBUG﹕ Tombstone written to: /data/tombstones/tombstone_08
    

    Too many open files might be just a system limitation from too much thrashing of the rotation... or we have a file handle leak somewhere.

  17. ljbade commented on Sep 22, 2015

    @ljbade
    ContributorAuthor

    Now I'm going to try android-http-crash branch.

    Hmm seems it freezes. But I'm out of time will pick up tomorrow.

  18. bleege commented on Sep 22, 2015

    @bleege
    Contributor

    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).

    Am I monkey testing wrong?

  19. ljbade commented on Sep 22, 2015

    @ljbade
    ContributorAuthor

    @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.

  20. ljbade commented on Sep 22, 2015

    @ljbade
    ContributorAuthor

    @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

  21. incanus commented on Sep 22, 2015

    @incanus
    Contributor

    I am not seeing the crash under my previous test scenario as of d2d3df2.

    Great work @ljbade!

  22. bleege commented on Sep 22, 2015

    @bleege
    Contributor

    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).

  23. ljbade commented on Sep 23, 2015

    @ljbade
    ContributorAuthor

    @kkaefer @bleege @incanus I started look at how to do Option C.

    However I discovered:

    • 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.

  24. bleege commented on Sep 23, 2015

    @bleege
    Contributor

    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:

  25. assigned and unassigned on Sep 23, 2015
  26. bleege commented on Sep 23, 2015

    @bleege
    Contributor

    Option C is now documented and scheduled for work in 0.2.0 at #2400.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Androidcrash

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions