From 69cc06b82a61dc56e6988d69b5d6c34fbcd07223 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 11 Sep 2026 18:45:29 -0700 Subject: [PATCH 1/4] ADFA-5739: bind the build service to the application, not the editor A configuration change destroys EditorActivityKt without finishing it, so isDestroying (= isFinishing) is false and preDestroy skips unbindService. The connection stays in LoadedApk.mServices, which keys its ServiceDispatcher on the binding Context -- so the framework's own bookkeeping retained the dead activity. LeakCanary measured 775,579 bytes in 12,806 objects held that way. Unbinding on every destroy is not the fix: GradleBuildService is bind-only and its onDestroy shuts down the Tooling API server, so that would kill the Gradle daemon connection on every rotation. Binding through applicationContext keeps the binding alive across recreation exactly as before, while leaving the activity out of mServices entirely. Also clears buildServiceConnection.onConnected on every destroy rather than only when finishing: it is a bound reference to the activity, so a bind still pending at destroy would otherwise deliver into a dead instance. Verified on a Pixel 6 Pro (Android 17), six uiMode recreations: before, four destroyed activities produced "Found 4 objects retained" and a heap analysis reporting the leak; after, six destroyed activities produce no retained-object report, and no ServiceDispatcher in the heap is bound to an Activity context. Sibling swept: BaseEditorActivity's debuggerServiceConnection has the same isDestroying guard and is left alone deliberately -- it is an anonymous inner class that captures the activity, so moving it to applicationContext would not help, and unbinding it on every destroy would end a debug session on rotation. AppLogsCoordinator's bind is clean; it tracks bound state and nulls its context. Co-Authored-By: Claude Opus 5 (1M context) --- .../activities/editor/ProjectHandlerActivity.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index ca6f2e7add..f85af5b58a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -408,6 +408,10 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { super.preDestroy() + // On every destroy, not only a finishing one: onConnected is a bound reference to this + // activity, so leaving it set lets a still-pending bind deliver into a dead instance. + buildServiceConnection.onConnected = null + if (didCompleteLiveOnCreate && isDestroying) { try { stopLanguageServers() @@ -416,8 +420,7 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { } try { - unbindService(buildServiceConnection) - buildServiceConnection.onConnected = {} + applicationContext.unbindService(buildServiceConnection) } catch (_: Throwable) { log.error("Unable to unbind service") } finally { @@ -494,8 +497,12 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { buildServiceConnection.onConnected = this::onGradleBuildServiceConnected + // Bind through the application context, not the activity. LoadedApk.mServices keys its + // ServiceDispatcher on the binding Context, so an activity-scoped bind that outlives the + // instance -- a configuration change skips the unbind in preDestroy -- retains the dead + // activity through the framework's own bookkeeping. if ( - bindService( + applicationContext.bindService( Intent(this, GradleBuildService::class.java), buildServiceConnection, BIND_AUTO_CREATE or BIND_IMPORTANT, From 8d2a1ca7dea39f2c7e2221f6b6317f7edfe8516b Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 11 Sep 2026 20:12:05 -0700 Subject: [PATCH 2/4] ADFA-5739: pin the callback clearing with a regression test Review found the PR body's claim that a regression test was infeasible to be false: DebuggerServiceUnbindTest already builds an editor activity under Robolectric, which is the harness this needs. The claim went in unchecked. Adds that test for the half of the fix a unit test can reach: a non-finishing destroy must still clear buildServiceConnection.onConnected. Proven to fail without the fix -- with the clearing back inside the isDestroying guard it fails on the assertion at line 36, for the reason it is named for. Moves the clearing to the top of preDestroy so it runs before any teardown that can throw. That is also the stronger guarantee: nothing during teardown can deliver a connection callback into a half-destroyed activity. The other half -- that the bind is made on the application context -- stays covered by the device A/B only. Robolectric 4.11.1 keeps one global list of bound connections keyed by ServiceConnection (ShadowInstrumentation) and never records the binding Context, so no shadow assertion can tell an activity bind from an application bind. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012PKGs7vqdbmDSYofe5BXiS --- .../editor/ProjectHandlerActivity.kt | 9 ++-- .../editor/BuildServiceCallbackClearedTest.kt | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index f85af5b58a..40ac27913a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -391,6 +391,11 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { } override fun preDestroy() { + // First, and on every destroy rather than only a finishing one: onConnected is a bound + // reference to this activity, so leaving it set lets a bind that is still pending deliver + // into an instance that is already tearing down. + buildServiceConnection.onConnected = null + syncNotificationFlashbar?.dismiss() syncNotificationFlashbar = null @@ -408,10 +413,6 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { super.preDestroy() - // On every destroy, not only a finishing one: onConnected is a bound reference to this - // activity, so leaving it set lets a still-pending bind deliver into a dead instance. - buildServiceConnection.onConnected = null - if (didCompleteLiveOnCreate && isDestroying) { try { stopLanguageServers() diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt new file mode 100644 index 0000000000..7724f9a072 --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt @@ -0,0 +1,53 @@ +package com.itsaky.androidide.activities.editor + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.app.BaseApplication +import com.itsaky.androidide.services.builder.GradleBuildService +import com.itsaky.androidide.services.builder.GradleBuildServiceConnnection +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A configuration change destroys the editor without finishing it, so [isDestroying] is false and + * the finish-only teardown is skipped. The build service callback must be released anyway: it is a + * bound reference to this activity (`this::onGradleBuildServiceConnected`), so a bind still pending + * at destroy would otherwise deliver into a dead instance. + */ +@RunWith(RobolectricTestRunner::class) +@Config(application = BuildServiceCallbackClearedTest.TestApp::class) +class BuildServiceCallbackClearedTest { + open class TestApp : BaseApplication() + + @Test + fun givenANonFinishingDestroy_whenPreDestroyRuns_thenTheBuildServiceCallbackIsCleared() { + val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java).get() + val connection = connectionField.get(activity) as GradleBuildServiceConnnection + onConnected.set(connection, { _: GradleBuildService -> }) + isDestroying.set(activity, false) + + // preDestroy goes on to tear down members that a bare activity never initialised; the + // clearing under test happens first, so what it throws afterwards is not this test's + // concern. If the clearing ever moves back below that teardown, the assertion fails. + runCatching { preDestroy.invoke(activity) } + + assertThat(onConnected.get(connection)).isNull() + } + + private val connectionField = + ProjectHandlerActivity::class + .java + .getDeclaredField("buildServiceConnection") + .apply { isAccessible = true } + private val onConnected = + GradleBuildServiceConnnection::class + .java + .getDeclaredField("onConnected") + .apply { isAccessible = true } + private val isDestroying = + BaseEditorActivity::class.java.getDeclaredField("isDestroying").apply { isAccessible = true } + private val preDestroy = + ProjectHandlerActivity::class.java.getDeclaredMethod("preDestroy").apply { isAccessible = true } +} From 022fecf662e7d412fee27afd3514f4573c13a1b8 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 11 Sep 2026 21:01:10 -0700 Subject: [PATCH 3/4] ADFA-5739: build the bind Intent from the application context too The Intent was still constructed with the activity while the bind beside it was made on applicationContext, contradicting the comment directly above. No behaviour change -- Intent only keeps the derived ComponentName -- but it invites the next reader, or the next putExtra, to reintroduce the activity coupling the comment warns against. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012PKGs7vqdbmDSYofe5BXiS --- .../androidide/activities/editor/ProjectHandlerActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index 40ac27913a..25b0a93a9a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -504,7 +504,7 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { // activity through the framework's own bookkeeping. if ( applicationContext.bindService( - Intent(this, GradleBuildService::class.java), + Intent(applicationContext, GradleBuildService::class.java), buildServiceConnection, BIND_AUTO_CREATE or BIND_IMPORTANT, ) From f96eed5aa5d632e838e9b2183cf39920d98d7b46 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 11 Sep 2026 21:43:07 -0700 Subject: [PATCH 4/4] ADFA-5739: pin both destroy shapes, not just the recreation Review pointed out that isDestroying.set(activity, false) established nothing: the field already defaults to false on an activity that was only constructed, so the test's named precondition was never actually set, and the finishing destroy was not covered at all. The bug was an asymmetry between the two shapes -- the clearing ran when finishing and not on a recreation -- so pinning one shape cannot catch it coming back. Parameterises over both and sets the flag explicitly in each. The non-finishing case is the one proven to fail without the fix. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012PKGs7vqdbmDSYofe5BXiS --- .../editor/BuildServiceCallbackClearedTest.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt index 7724f9a072..f82785d7a6 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt @@ -23,10 +23,24 @@ class BuildServiceCallbackClearedTest { @Test fun givenANonFinishingDestroy_whenPreDestroyRuns_thenTheBuildServiceCallbackIsCleared() { + assertCallbackClearedWhenDestroying(false) + } + + @Test + fun givenAFinishingDestroy_whenPreDestroyRuns_thenTheBuildServiceCallbackIsCleared() { + assertCallbackClearedWhenDestroying(true) + } + + /** + * Both destroy shapes, because the bug was an asymmetry between them: the clearing used to sit + * inside the `isDestroying` guard, so it ran for a finishing destroy and not for a recreation. + * Pinning only one shape would not have caught that. + */ + private fun assertCallbackClearedWhenDestroying(destroying: Boolean) { val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java).get() val connection = connectionField.get(activity) as GradleBuildServiceConnnection onConnected.set(connection, { _: GradleBuildService -> }) - isDestroying.set(activity, false) + isDestroying.set(activity, destroying) // preDestroy goes on to tear down members that a bare activity never initialised; the // clearing under test happens first, so what it throws afterwards is not this test's