Repository navigation
ADFA-5739: bind the build service to the application, not the editor #1831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
davidschachterADFA
wants to merge
4
commits into
stage
from
bugfix/ADFA-5739-editor-service-binding-leak
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
69cc06b
ADFA-5739: bind the build service to the application, not the editor
davidschachterADFA 8d2a1ca
ADFA-5739: pin the callback clearing with a regression test
davidschachterADFA 022fecf
ADFA-5739: build the bind Intent from the application context too
davidschachterADFA f96eed5
ADFA-5739: pin both destroy shapes, not just the recreation
davidschachterADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 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() { | ||
| 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, 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 | ||
| // 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 } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 2288
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 6890
🌐 Web query:
Android Context unbindService ServiceConnection same instance bindService official documentation💡 Result:
When using bindService and unbindService in Android, you must pass the exact same instance of the ServiceConnection object that was used to initiate the binding [1][2][3]. The system uses this specific instance to track the lifecycle of the binding [4][3]. If you attempt to unbind using a different instance, a null object, or if the service is already unbound, the system will throw an IllegalArgumentException: Service not registered [5][6]. Key technical considerations include: 1. Consistency of Context: You must call unbindService on the same Context instance (e.g., Activity or ApplicationContext) that was used to call bindService [6]. Using different contexts, even if they share the same base, can lead to the "Service not registered" error [6]. 2. Tracking State: It is a best practice to maintain an internal state flag (e.g., boolean isBound) to ensure you do not call unbindService multiple times on the same connection or call it when not bound [2][5][6]. 3. Mandatory Unbinding: You are required to call unbindService to release tracking resources, even if the service's onBindingDied or onNullBinding callbacks are invoked [4][7]. 4. Debugging Mismatched Calls: If you are encountering difficult-to-track unbind issues, you can use the BIND_DEBUG_UNBIND flag during bindService. This causes the system to retain the call stack of the binding, which will be printed if an incorrect unbind call is later made, helping you identify exactly where the mismatch originated [8][3]. Note that this should only be used for debugging, as it retains information for the lifetime of the app and can lead to memory leaks [8][3].
Citations:
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 8138
Preserve the application-context
ServiceConnectionacross configuration changes.buildServiceConnectionbelongs to each activity, butstartServices()binds it throughapplicationContext. After a configuration change,preDestroy()skips unbinding. The replacement activity has a differentGradleBuildServiceConnnection. Its finalpreDestroy()therefore passes the wrong instance tounbindService. Android throwsIllegalArgumentException, which this code catches, and the original binding remains active. This can keepGradleBuildServicealive.Move the connection and binding state into an application-scoped owner, or unbind the original connection during configuration teardown and bind the replacement activity again. Add a rotation-then-finish regression test that verifies the original connection is unbound.
🤖 Prompt for AI Agents
Source: Coding guidelines