-
-
Notifications
You must be signed in to change notification settings - Fork 29
ADFA-4387: add global analysis lock for Kotlin analysis #1428
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
8 commits
Select commit
Hold shift + click to select a range
d8c32bd
fix: add global analysis lock for Kotlin analysis
itsaky dc471d3
tests: add test case
itsaky 90e5471
fix: address review findings (lifetime owner escape, write-lock race,…
itsaky-adfa 7bca4d7
fix: guard PSI/module resolution with read lock in notifyElementModif…
itsaky-adfa a73139a
Merge remote-tracking branch 'origin/stage' into fix/ADFA-4387
itsaky-adfa 31340d6
Merge remote-tracking branch 'origin/stage' into fix/ADFA-4387
itsaky-adfa e2f9c8e
Merge branch 'stage' into fix/ADFA-4387
itsaky-adfa 9904ba7
Merge branch 'stage' into fix/ADFA-4387
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
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
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
115 changes: 115 additions & 0 deletions
115
.../test/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisSerializationTest.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,115 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.compiler.modules | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import com.itsaky.androidide.lsp.kotlin.compiler.read | ||
| import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.coroutineScope | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Test | ||
| import java.util.Collections | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
| import kotlin.math.max | ||
|
|
||
| /** | ||
| * Regression tests for the `KaInaccessibleLifetimeOwnerAccessException: ... Called outside an | ||
| * `analyze` context.` reported in Sentry (APPDEVFORALL-VR / 7454434587). | ||
| * | ||
| * Root cause: indexing, diagnostics and completion all drove the stock Kotlin Analysis API | ||
| * concurrently from `Dispatchers.Default` threads (the modified-file indexer is debounced into | ||
| * independent coroutines), frequently against the same file. The Analysis API tracks its `analyze` | ||
| * lifetime context in a per-thread stack and is not safe to run concurrently without platform | ||
| * read-action coordination (which this LSP replaces with a shared read lock that does not serialize | ||
| * analysis). Overlapping `analyze` calls corrupted the lifetime/session lifecycle. | ||
| * | ||
| * Fix: [analyzeMaybeDangling] / [withAnalysisLock] hold a process-wide reentrant lock so analyses | ||
| * are mutually exclusive. | ||
| * | ||
| * Both tests fail before the fix (either by throwing the exception or by observing overlapping | ||
| * analyses) and pass after it. | ||
| */ | ||
| class AnalysisSerializationTest : KtLspTest() { | ||
|
|
||
| @Test | ||
| fun `concurrent analyzeMaybeDangling never throws lifetime exception`(): Unit = runBlocking { | ||
| val files = (0 until 8).map { i -> | ||
| createSourceFile( | ||
| "Concurrent$i.kt", | ||
| """ | ||
| class Klass$i { | ||
| fun member$i(p: Int): Int = p + $i | ||
| val prop$i: String = "v$i" | ||
| } | ||
|
|
||
| fun topLevel$i() = $i | ||
| """.trimIndent() | ||
| ) | ||
| } | ||
|
|
||
| val errors = Collections.synchronizedList(mutableListOf<Throwable>()) | ||
|
|
||
| // Many short, overlapping analyses on a high-parallelism dispatcher to reproduce the race. | ||
| coroutineScope { | ||
| repeat(240) { iter -> | ||
| launch(Dispatchers.IO) { | ||
| val file = files[iter % files.size] | ||
| try { | ||
| env.project.read { | ||
| analyzeMaybeDangling(file) { | ||
| // Touching declaration symbols is what triggered the lifetime check. | ||
| file.declarations.forEach { dcl -> | ||
| dcl.symbol | ||
| } | ||
| } | ||
| } | ||
| } catch (t: Throwable) { | ||
| errors.add(t) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assertThat(errors).isEmpty() | ||
| } | ||
|
|
||
| @Test | ||
| fun `analyzeMaybeDangling serializes overlapping analyses`(): Unit = runBlocking { | ||
| val files = (0 until 8).map { i -> | ||
| createSourceFile("Serialized$i.kt", "class S$i { fun f$i() = $i }") | ||
| } | ||
|
|
||
| val inFlight = AtomicInteger(0) | ||
| val maxObserved = AtomicInteger(0) | ||
| val errors = Collections.synchronizedList(mutableListOf<Throwable>()) | ||
|
|
||
| coroutineScope { | ||
| repeat(64) { iter -> | ||
| launch(Dispatchers.IO) { | ||
| val file = files[iter % files.size] | ||
| try { | ||
| env.project.read { | ||
| analyzeMaybeDangling(file) { | ||
| val concurrent = inFlight.incrementAndGet() | ||
| maxObserved.updateAndGet { max(it, concurrent) } | ||
| try { | ||
| file.declarations.forEach { it.symbol } | ||
| // Widen the window so any real overlap is observed. | ||
| Thread.sleep(2) | ||
| } finally { | ||
| inFlight.decrementAndGet() | ||
| } | ||
| } | ||
| } | ||
| } catch (t: Throwable) { | ||
| errors.add(t) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assertThat(errors).isEmpty() | ||
| // The shared analysis lock must prevent two analyses from running at once. | ||
| assertThat(maxObserved.get()).isEqualTo(1) | ||
| } | ||
| } |
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.