From 1e9ea4dc548cc48a174b487a22c12bd94362e246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Fri, 16 Aug 2019 14:54:36 +0200 Subject: [android] check if the CustomGeometrySource's java peer is valid when the thread is shutting down --- .../mapboxsdk/testapp/style/CustomGeometrySourceTest.kt | 11 +++-------- .../android/src/style/sources/custom_geometry_source.cpp | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/CustomGeometrySourceTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/CustomGeometrySourceTest.kt index a6238ebf14..9c2eb3df81 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/CustomGeometrySourceTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/CustomGeometrySourceTest.kt @@ -2,7 +2,6 @@ package com.mapbox.mapboxsdk.testapp.style import android.support.test.espresso.Espresso.onView import android.support.test.espresso.matcher.ViewMatchers.isRoot -import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.THREAD_POOL_LIMIT import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.THREAD_PREFIX import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke @@ -15,7 +14,6 @@ import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.ID_GRID_LA import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.ID_GRID_SOURCE import com.mapbox.mapboxsdk.testapp.utils.TestingAsyncUtils import org.junit.Assert -import org.junit.Ignore import org.junit.Test class CustomGeometrySourceTest : BaseTest() { @@ -23,7 +21,6 @@ class CustomGeometrySourceTest : BaseTest() { override fun getActivityClass(): Class<*> = GridSourceActivity::class.java @Test - @Ignore fun sourceNotLeakingThreadsTest() { validateTestSetup() WaitAction.invoke(4000) @@ -38,7 +35,6 @@ class CustomGeometrySourceTest : BaseTest() { } @Test - @Ignore fun threadsShutdownWhenSourceRemovedTest() { validateTestSetup() invoke(mapboxMap) { uiController, mapboxMap -> @@ -48,13 +44,12 @@ class CustomGeometrySourceTest : BaseTest() { TestingAsyncUtils.waitForLayer(uiController, mapView) Assert.assertTrue("There should be no threads running when the source is removed.", Thread.getAllStackTraces().keys.filter { - it.name.startsWith(CustomGeometrySource.THREAD_PREFIX) + it.name.startsWith(THREAD_PREFIX) }.count() == 0) } } @Test - @Ignore fun threadsRestartedWhenSourceReAddedTest() { validateTestSetup() invoke(mapboxMap) { uiController, mapboxMap -> @@ -67,8 +62,8 @@ class CustomGeometrySourceTest : BaseTest() { TestingAsyncUtils.waitForLayer(uiController, mapView) Assert.assertTrue("Threads should be restarted when the source is re-added to the map.", Thread.getAllStackTraces().keys.filter { - it.name.startsWith(CustomGeometrySource.THREAD_PREFIX) - }.count() == CustomGeometrySource.THREAD_POOL_LIMIT) + it.name.startsWith(THREAD_PREFIX) + }.count() == THREAD_POOL_LIMIT) } } } \ No newline at end of file diff --git a/platform/android/src/style/sources/custom_geometry_source.cpp b/platform/android/src/style/sources/custom_geometry_source.cpp index 057f5c99ba..dccca4cf46 100644 --- a/platform/android/src/style/sources/custom_geometry_source.cpp +++ b/platform/android/src/style/sources/custom_geometry_source.cpp @@ -65,7 +65,12 @@ namespace android { static auto& javaClass = jni::Class::Singleton(*_env); static auto fetchTile = javaClass.GetMethod(*_env, "fetchTile"); - assert(javaPeer); + // The source is removed on the main thread, but it still exists on the Render thread until the frame is complete. + // This might cause fetchTile/cancelTile invocations when the Java thread is shutting down and the peer has already been released. + // See https://github.com/mapbox/mapbox-gl-native/issues/12551. + if(!javaPeer) { + return; + } auto peer = jni::Cast(*_env, javaClass, javaPeer); peer.Call(*_env, fetchTile, (int)tileID.z, (int)tileID.x, (int)tileID.y); @@ -77,7 +82,12 @@ namespace android { static auto& javaClass = jni::Class::Singleton(*_env); static auto cancelTile = javaClass.GetMethod(*_env, "cancelTile"); - assert(javaPeer); + // The source is removed on the main thread, but it still exists on the Render thread until the frame is complete. + // This might cause fetchTile/cancelTile invocations when the Java thread is shutting down and the peer has already been released. + // See https://github.com/mapbox/mapbox-gl-native/issues/12551. + if(!javaPeer) { + return; + } auto peer = jni::Cast(*_env, javaClass, javaPeer); peer.Call(*_env, cancelTile, (int)tileID.z, (int)tileID.x, (int)tileID.y); -- cgit v1.2.1