Repository navigation
ADFA-6731: Stop the Kotlin LSP analyzing against closed indexes when a project closes #2190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a3d2188
ADFA-6731: Refuse and close indexes registered after the registry closes
itsaky-adfa 6744c7a
ADFA-6731: Log use of a closed index at DEBUG once its session has ended
itsaky-adfa 940a314
ADFA-6731: Stop indexing setup quietly when the session closes, and r…
itsaky-adfa d536190
ADFA-6731: Record the indexing close before a new manager can be created
itsaky-adfa cd29e80
ADFA-6731: Bind each Kotlin project update to one indexing manager
itsaky-adfa d3c877b
ADFA-6731: End a Kotlin build quietly when its project closes, and re…
itsaky-adfa a324017
ADFA-6731: Stop language servers before destroying the project, from …
itsaky-adfa 0c9e56c
ADFA-6731: Document the project session teardown order
itsaky-adfa 8182007
ADFA-6731: Refuse new indexes as soon as the session ends, not when t…
itsaky-adfa 561360c
ADFA-6731: Tear down from preDestroy when the editor never recorded i…
itsaky-adfa 6aac117
ADFA-6731: Cancel the Kotlin index worker on close instead of drainin…
itsaky-adfa 70cfa88
Merge branch 'stage' into fix/ADFA-6731
itsaky-adfa 4ad9a2f
Merge branch 'stage' into fix/ADFA-6731
itsaky-adfa 7dc99c6
Merge branch 'stage' into fix/ADFA-6731
itsaky-adfa f0640b6
ADFA-6731: Cancel project initialization on close and refuse a sessio…
itsaky-adfa 1161c22
Merge remote-tracking branch 'origin/fix/ADFA-6731' into fix/ADFA-6731
itsaky-adfa f54fff7
Merge branch 'stage' into fix/ADFA-6731
itsaky-adfa db1bed9
Merge branch 'stage' into fix/ADFA-6731
itsaky-adfa 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
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
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/itsaky/androidide/activities/editor/ProjectSessionCloser.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,66 @@ | ||
| package com.itsaky.androidide.activities.editor | ||
|
|
||
| import org.slf4j.LoggerFactory | ||
|
|
||
| /** | ||
| * Runs an editor's project-session teardown at most once, and only for the session it opened. | ||
| * | ||
| * The teardown stops the language servers before it destroys the project: a stopped server can no | ||
| * longer pick up the next project's indexing manager, and the indexes it still holds close quietly. | ||
| * It runs from `onPause` while finishing, because `isFinishing` is reliable there and the next | ||
| * editor may open a project before this one's `onDestroy`. A configuration change never runs it: | ||
| * `isFinishing` is false then, and the servers must survive the recreation. `preDestroy` calls it | ||
| * too, for a `finish()` that arrives while already paused. | ||
| * | ||
| * Servers, the project manager and Quick Build are process-wide, so the teardown is skipped when the | ||
| * current session is not the one [record]ed, which means another editor already owns them. With | ||
| * nothing recorded (the project never finished opening, or a recreated editor skipped it) it tears | ||
| * down as before recording existed. The check only tells sessions apart when a new manager was | ||
| * created between them. | ||
| * | ||
| * [record] runs on the project initialization callback's thread and [close] on the main thread, so | ||
| * both hold the lock: a session recorded after the teardown would never be closed. | ||
| * | ||
| * @param currentSession Returns the current session without creating one. | ||
| * @param teardown Stops the servers and destroys the project. | ||
| */ | ||
| internal class ProjectSessionCloser<S : Any>( | ||
| private val currentSession: () -> S?, | ||
| private val teardown: () -> Unit, | ||
| ) { | ||
| private companion object { | ||
| private val log = LoggerFactory.getLogger(ProjectSessionCloser::class.java) | ||
| } | ||
|
|
||
| private var recorded: S? = null | ||
|
itsaky-adfa marked this conversation as resolved.
Resolved
|
||
| private var tornDown = false | ||
|
|
||
| /** | ||
| * Records the session [session] returns as the one this editor opened, and returns whether it did. | ||
| * | ||
| * Once the teardown has run it returns false without calling [session], since resolving a session | ||
| * may create one that nothing would close. | ||
| */ | ||
| @Synchronized | ||
| fun record(session: () -> S): Boolean { | ||
| if (tornDown) return false | ||
| recorded = session() | ||
| return true | ||
| } | ||
|
|
||
| /** Tears down when nothing was recorded or this editor still owns the current session. */ | ||
| @Synchronized | ||
| fun close() { | ||
| if (recorded == null || currentSession() === recorded) runTeardown() else skip() | ||
| } | ||
|
|
||
| private fun runTeardown() { | ||
| if (tornDown) return | ||
| tornDown = true | ||
| teardown() | ||
| } | ||
|
|
||
| private fun skip() { | ||
| if (!tornDown) log.info("Skipping project teardown: this editor does not own the current session") | ||
| } | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
app/src/test/java/com/itsaky/androidide/activities/editor/ProjectSessionCloserTest.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,97 @@ | ||
| package com.itsaky.androidide.activities.editor | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import org.junit.Test | ||
|
|
||
| class ProjectSessionCloserTest { | ||
| private val first = Any() | ||
| private val second = Any() | ||
| private var current: Any? = null | ||
| private var teardowns = 0 | ||
| private val closer = ProjectSessionCloser({ current }, { teardowns++ }) | ||
|
|
||
| @Test | ||
| fun `teardown runs once across repeated calls`() { | ||
| recordCurrent(first) | ||
|
|
||
| closer.close() | ||
| closer.close() | ||
| closer.close() | ||
|
|
||
| assertThat(teardowns).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `nothing recorded tears down as before`() { | ||
| closer.close() | ||
|
|
||
| assertThat(teardowns).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `skips when the current manager is not the recorded one`() { | ||
| recordCurrent(first) | ||
| current = second | ||
|
|
||
| closer.close() | ||
| closer.close() | ||
|
|
||
| assertThat(teardowns).isEqualTo(0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `an editor that still owns its session tears it down`() { | ||
| recordCurrent(first) | ||
|
|
||
| closer.close() | ||
|
|
||
| assertThat(teardowns).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a closer that never ran its teardown does nothing on recreation`() { | ||
| recordCurrent(first) | ||
|
|
||
| assertThat(teardowns).isEqualTo(0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `record after teardown is ignored`() { | ||
| closer.close() | ||
| recordCurrent(second) | ||
|
|
||
| closer.close() | ||
|
|
||
| assertThat(teardowns).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `record after teardown reports that it was refused`() { | ||
| closer.close() | ||
|
|
||
| assertThat(closer.record { second }).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `record after teardown never resolves the session`() { | ||
| var resolved = false | ||
| closer.close() | ||
|
|
||
| closer.record { | ||
| resolved = true | ||
| second | ||
| } | ||
|
|
||
| assertThat(resolved).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `record before teardown is accepted`() { | ||
| assertThat(closer.record { first }).isTrue() | ||
| } | ||
|
|
||
| private fun recordCurrent(session: Any) { | ||
| current = session | ||
| closer.record { session } | ||
| } | ||
| } |
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
21 changes: 21 additions & 0 deletions
21
lsp/indexing/src/main/kotlin/org/appdevforall/codeonthego/indexing/IndexSessionLifetime.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,21 @@ | ||
| package org.appdevforall.codeonthego.indexing | ||
|
|
||
| /** | ||
| * Marks whether the project session that owns a set of indexes has ended. | ||
| * | ||
| * Indexes of an ended session are expected to be used after close while the session's remaining | ||
| * work winds down, so they log that at DEBUG; a live session's use after close is a bug and logs ERROR. | ||
| */ | ||
| class IndexSessionLifetime { | ||
| @Volatile | ||
| private var ended = false | ||
|
|
||
| /** Whether [end] has been called. */ | ||
| val isEnded: Boolean | ||
| get() = ended | ||
|
|
||
| /** Ends the session; idempotent and never blocks. */ | ||
| fun end() { | ||
| ended = true | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.