From 0d9356d6837ff8f1ad50d4d8827e301adc7503f5 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Thu, 6 Aug 2026 15:46:25 -0700 Subject: [PATCH 1/3] ADFA-5052: Defer JavaCompilerService/SourceFileManager construction until a real .java file is touched DefaultLanguageServerRegistry.onProjectInitialized dispatches setupWithProject to every registered language server unconditionally, regardless of project language. JavaLanguageServer.setupWithProject() referenced JavaCompilerService.NO_MODULE_COMPILER and called SourceFileManager.clearCache(), both of which trigger class-init that eagerly constructs real javac Context/ JavacFileManager machinery plus a full android.jar top-level-class scan -- on the first project open in the app's lifetime, Kotlin-only projects included. shutdown() had the same problem in reverse, on every project close. Same eager-load bug pattern ADFA-5010 fixed for the Kotlin Analysis API, and independently confirmed and sized (openjdk.tools.javac ~2,238 classes, ~3.7MB) while researching whether javac could get ADFA-5010's carrier-APK treatment. This fix is scoped to just the eager-construction bug -- no DexClassLoader/ carrier-APK split; javac/jdk-compiler stay in the main dex, just constructed lazily. setupWithProject() now only stashes the workspace; the actual reset (destroy NO_MODULE_COMPILER, clear file-manager/JAR-fs caches, index module classpaths) is deferred to ensureProjectReset(), called from getCompiler() and onContentChange() -- both already gated on DocumentUtils.isJavaFile(), so this now only runs on genuine Java-file interaction. shutdown() skips its javac-specific cleanup entirely if that never happened. Per-file LSP dispatch methods (complete/findReferences/findDefinition/expandSelection/signatureHelp) needed no changes: the editor's IDELanguage already resolves one language server per file before calling any of them, so they were never the source of the cross-language trigger. Verified: :lsp:java unit tests pass, :app:assembleV8Debug builds clean. --- .../androidide/lsp/java/JavaLanguageServer.kt | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt index fc94ebbc84..f8af6393f2 100644 --- a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt +++ b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt @@ -96,6 +96,15 @@ class JavaLanguageServer : ILanguageServer { private val timer = AnalyzeTimer { analyzeSelected() } private var cachedCompletion: CachedCompletion + // Set by setupWithProject(), consumed by ensureProjectReset() on the first real .java-file + // interaction after it -- deferred because setupWithProject() is called for every project + // open regardless of language (ADFA-5052). + @Volatile + private var pendingWorkspace: Workspace? = null + + @Volatile + private var javaCompilerInitialized = false + val settings: IServerSettings get() { return _settings ?: JavaServerSettings @@ -123,10 +132,10 @@ class JavaLanguageServer : ILanguageServer { val projectManager = ProjectManagerImpl.getInstance() projectManager.indexingServiceManager.register( - service = JvmLibraryIndexingService(context = BaseApplication.baseInstance) + service = JvmLibraryIndexingService(context = BaseApplication.baseInstance), ) projectManager.indexingServiceManager.register( - service = JvmGeneratedIndexingService(context = BaseApplication.baseInstance) + service = JvmGeneratedIndexingService(context = BaseApplication.baseInstance), ) JavaSnippetRepository.init() @@ -134,10 +143,12 @@ class JavaLanguageServer : ILanguageServer { override fun shutdown() { (this.debugAdapter as? AutoCloseable?)?.close() - JavaCompilerProvider.getInstance().destroy() - SourceFileManager.clearCache() - CacheFSInfoSingleton.clearCache() - clearCache() + if (javaCompilerInitialized) { + JavaCompilerProvider.getInstance().destroy() + SourceFileManager.clearCache() + CacheFSInfoSingleton.clearCache() + clearCache() + } EventBus.getDefault().unregister(this) timer.cancel() } @@ -163,10 +174,35 @@ class JavaLanguageServer : ILanguageServer { override fun setupWithProject(workspace: Workspace) { LSPEditorActions.ensureActionsMenuRegistered(JavaCodeActionsMenu) - (ProjectManagerImpl.getInstance() - .indexingServiceManager - .getService(JvmLibraryIndexingService.ID) as? JvmLibraryIndexingService?) - ?.refresh() + ( + ProjectManagerImpl + .getInstance() + .indexingServiceManager + .getService(JvmLibraryIndexingService.ID) as? JvmLibraryIndexingService? + )?.refresh() + + // Deferred to ensureProjectReset(), run on the first real .java-file interaction instead + // of here -- this method runs for every project open regardless of language + // (DefaultLanguageServerRegistry dispatches to all registered servers unconditionally), + // and JavaCompilerService.NO_MODULE_COMPILER / SourceFileManager.NO_MODULE eagerly + // construct real javac machinery plus a full android.jar scan at class-init, merely by + // being referenced (ADFA-5052, mirrors ADFA-5010's KotlinLanguageServer fix). + pendingWorkspace = workspace + } + + /** + * Runs the javac-specific project reset deferred by [setupWithProject], for the most + * recently opened project, the first time a real Java file is actually interacted with. + * No-ops if already up to date. + */ + private fun ensureProjectReset() { + if (pendingWorkspace == null) return + val workspace: Workspace + synchronized(this) { + workspace = pendingWorkspace ?: return + pendingWorkspace = null + javaCompilerInitialized = true + } // Once we have project initialized // Destory the NO_MODULE_COMPILER instance @@ -196,8 +232,7 @@ class JavaLanguageServer : ILanguageServer { override fun complete(params: CompletionParams?): CompletionResult { val compiler = getCompiler(params!!.file) - if (!settings.completionsEnabled() || !completionProvider.canComplete(params.file) - ) { + if (!settings.completionsEnabled() || !completionProvider.canComplete(params.file)) { return CompletionResult.EMPTY } @@ -265,8 +300,7 @@ class JavaLanguageServer : ILanguageServer { } } - override fun formatCode(params: FormatCodeParams?): CodeFormatResult = - CodeFormatProvider(settings).format(params) + override fun formatCode(params: FormatCodeParams?): CodeFormatResult = CodeFormatProvider(settings).format(params) override fun handleFailure(failure: LSPFailure?): Boolean { return when (failure!!.type) { @@ -285,6 +319,7 @@ class JavaLanguageServer : ILanguageServer { if (!DocumentUtils.isJavaFile(file)) { return JavaCompilerService.NO_MODULE_COMPILER } + ensureProjectReset() val module = ProjectManagerImpl.getInstance().findModuleForFile(file!!) ?: return JavaCompilerService.NO_MODULE_COMPILER @@ -314,6 +349,8 @@ class JavaLanguageServer : ILanguageServer { return } + ensureProjectReset() + // TODO Find an alternative to efficiently update changeDelta in JavaCompilerService instance JavaCompilerService.NO_MODULE_COMPILER.onDocumentChange(event) val module = From ad09be1372d13c91a2a8bd53c32b7aa14b503e6e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Thu, 6 Aug 2026 16:23:14 -0700 Subject: [PATCH 2/3] ADFA-5052: Serialize the deferred javac reset against concurrent access and shutdown The previous fix's synchronized(this) block only guarded the *decision* to run ensureProjectReset() (claiming pendingWorkspace, flipping javaCompilerInitialized to true) -- not the destroy/rebuild work that followed, which ran unsynchronized. A concurrent getCompiler()/ onContentChange() call on another thread could see javaCompilerInitialized already true and proceed to use JavaCompilerProvider/SourceFileManager while the first thread was still mid-destroy or mid-rebuild. shutdown() didn't synchronize on anything at all, so it could run its own destroy()/ clearCache() concurrently with an in-flight reset, racing two teardown/ rebuild sequences against each other. Replaces the two ad-hoc @Volatile fields with an explicit PENDING/RESETTING/INITIALIZED/SHUTDOWN state machine guarded by a single ReentrantLock (compilerLifecycleLock) held for the *entire* reset or shutdown, not just the state transition. Concurrent callers now genuinely block until an in-flight reset finishes (getCompiler()/onContentChange() already route through ensureProjectReset(), which now can't return early while another thread holds the lock), and shutdown() waits on the same lock before deciding whether there's anything to tear down. setupWithProject() also goes through the lock; if a new project arrives mid-reset, the in-progress reset's own finally block detects the newer pendingWorkspace and reverts to PENDING instead of incorrectly claiming INITIALIZED. Verified: :lsp:java unit tests pass, :app:assembleV8Debug builds clean. --- .../androidide/lsp/java/JavaLanguageServer.kt | 109 ++++++++++++------ 1 file changed, 71 insertions(+), 38 deletions(-) diff --git a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt index f8af6393f2..763080fc49 100644 --- a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt +++ b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt @@ -84,6 +84,8 @@ import org.slf4j.LoggerFactory import java.nio.file.Files import java.nio.file.Path import java.util.Objects +import java.util.concurrent.locks.ReentrantLock +import kotlin.concurrent.withLock class JavaLanguageServer : ILanguageServer { private val completionProvider: CompletionProvider = CompletionProvider() @@ -96,14 +98,19 @@ class JavaLanguageServer : ILanguageServer { private val timer = AnalyzeTimer { analyzeSelected() } private var cachedCompletion: CachedCompletion - // Set by setupWithProject(), consumed by ensureProjectReset() on the first real .java-file - // interaction after it -- deferred because setupWithProject() is called for every project - // open regardless of language (ADFA-5052). - @Volatile - private var pendingWorkspace: Workspace? = null + // Lifecycle of the javac-backed compiler state (NO_MODULE_COMPILER, SourceFileManager, + // JavaCompilerProvider), which setupWithProject() defers instead of building eagerly + // (ADFA-5052). All reads/writes of pendingWorkspace and compilerLifecycle go through + // compilerLifecycleLock, held for the *entire* reset/shutdown, not just the decision to + // run one -- otherwise a concurrent getCompiler()/onContentChange() could use a compiler + // mid-teardown, or shutdown() could destroy state a reset is still rebuilding. + private enum class CompilerLifecycle { PENDING, RESETTING, INITIALIZED, SHUTDOWN } + + private val compilerLifecycleLock = ReentrantLock() - @Volatile - private var javaCompilerInitialized = false + // Guarded by compilerLifecycleLock. + private var pendingWorkspace: Workspace? = null + private var compilerLifecycle = CompilerLifecycle.PENDING val settings: IServerSettings get() { @@ -143,11 +150,17 @@ class JavaLanguageServer : ILanguageServer { override fun shutdown() { (this.debugAdapter as? AutoCloseable?)?.close() - if (javaCompilerInitialized) { - JavaCompilerProvider.getInstance().destroy() - SourceFileManager.clearCache() - CacheFSInfoSingleton.clearCache() - clearCache() + compilerLifecycleLock.withLock { + // Blocks here if a reset is in flight (RESETTING can only be observed by another + // thread while the lock is held, never by us once we've acquired it), so this never + // races ensureProjectReset()'s own destroy/rebuild. + if (compilerLifecycle == CompilerLifecycle.INITIALIZED) { + JavaCompilerProvider.getInstance().destroy() + SourceFileManager.clearCache() + CacheFSInfoSingleton.clearCache() + clearCache() + } + compilerLifecycle = CompilerLifecycle.SHUTDOWN } EventBus.getDefault().unregister(this) timer.cancel() @@ -187,47 +200,67 @@ class JavaLanguageServer : ILanguageServer { // and JavaCompilerService.NO_MODULE_COMPILER / SourceFileManager.NO_MODULE eagerly // construct real javac machinery plus a full android.jar scan at class-init, merely by // being referenced (ADFA-5052, mirrors ADFA-5010's KotlinLanguageServer fix). - pendingWorkspace = workspace + compilerLifecycleLock.withLock { + pendingWorkspace = workspace + // Leave RESETTING alone: ensureProjectReset()'s own finally block re-checks + // pendingWorkspace once it re-acquires the lock, so a project switch mid-reset is + // picked up as another PENDING round rather than raced here. + if (compilerLifecycle != CompilerLifecycle.RESETTING) { + compilerLifecycle = CompilerLifecycle.PENDING + } + } } /** * Runs the javac-specific project reset deferred by [setupWithProject], for the most * recently opened project, the first time a real Java file is actually interacted with. - * No-ops if already up to date. + * No-ops if already up to date. Blocks concurrent callers (and [shutdown]) for the entire + * reset, not just the decision to run one. */ private fun ensureProjectReset() { - if (pendingWorkspace == null) return - val workspace: Workspace - synchronized(this) { - workspace = pendingWorkspace ?: return + compilerLifecycleLock.withLock { + if (compilerLifecycle != CompilerLifecycle.PENDING) return + val workspace = pendingWorkspace ?: return pendingWorkspace = null - javaCompilerInitialized = true - } + compilerLifecycle = CompilerLifecycle.RESETTING - // Once we have project initialized - // Destory the NO_MODULE_COMPILER instance - JavaCompilerService.NO_MODULE_COMPILER.destroy() + try { + // Once we have project initialized + // Destory the NO_MODULE_COMPILER instance + JavaCompilerService.NO_MODULE_COMPILER.destroy() - // Clear cached file managers - SourceFileManager.clearCache() + // Clear cached file managers + SourceFileManager.clearCache() - // Clear cached JAR file system for R.jar - // Using the cached instance will result in completions not being updated for updated resources - // TODO Clearing caches for JAR files ending with '/R.jar' is probably not a good idea - // Maybe this could be improved by using data from the AndroidModule project model - clearCachesForPaths { path: String -> path.endsWith("/R.jar") } + // Clear cached JAR file system for R.jar + // Using the cached instance will result in completions not being updated for updated resources + // TODO Clearing caches for JAR files ending with '/R.jar' is probably not a good idea + // Maybe this could be improved by using data from the AndroidModule project model + clearCachesForPaths { path: String -> path.endsWith("/R.jar") } - // Clear cached module-specific compilers - JavaCompilerProvider.getInstance().destroy() + // Clear cached module-specific compilers + JavaCompilerProvider.getInstance().destroy() - // Cache classpath locations - for (subModule in workspace.subProjects) { - if (subModule !is ModuleProject || subModule.path == workspace.rootProject.path) { - continue + // Cache classpath locations + for (subModule in workspace.subProjects) { + if (subModule !is ModuleProject || subModule.path == workspace.rootProject.path) { + continue + } + SourceFileManager.forModule(subModule) + } + startOrRestartAnalyzeTimer() + } finally { + // A newer setupWithProject() may have queued another workspace while we were + // resetting (see the RESETTING guard above); if so, go back to PENDING instead + // of claiming INITIALIZED for a project we didn't actually reset for. + compilerLifecycle = + if (pendingWorkspace != null) { + CompilerLifecycle.PENDING + } else { + CompilerLifecycle.INITIALIZED + } } - SourceFileManager.forModule(subModule) } - startOrRestartAnalyzeTimer() } override fun complete(params: CompletionParams?): CompletionResult { From e9a54b498aa2fecb8750528f5ed32fc4ac836c23 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Thu, 6 Aug 2026 17:01:06 -0700 Subject: [PATCH 3/3] ADFA-5052: Fix exception handling, analyze() bypass, and a narrow post-lock race Three issues from /code-review high, verified against the current code: 1. ensureProjectReset() nulled pendingWorkspace before the try block, so any exception during destroy/rebuild (e.g. a bad submodule's classpath) still let the finally claim INITIALIZED -- silently treating a half-torn-down compiler as ready, with no retry, for the rest of the session. Now an exception re-queues the workspace, reverts to PENDING, and rethrows. 2. analyze() never called ensureProjectReset() at all. diagnosticProvider .analyze() builds its own JavaCompilerService directly, bypassing getCompiler(), and analysis is often the *first* real .java-file interaction (auto-triggered on file open, ahead of any completion request) -- so the R.jar/file-manager cache clear this reset performs could be skipped for an entire session, leaving diagnostics resolving against a stale previous project's classpath. Now gated the same way getCompiler()/onContentChange() already are. 3. getCompiler() and onContentChange() released compilerLifecycleLock as soon as ensureProjectReset() returned, then used JavaCompilerProvider unlocked -- a concurrent reset for a newer project could destroy() those compilers in the gap. Both now hold the lock across the reset and the subsequent provider lookup/use (safe: ReentrantLock is reentrant, so ensureProjectReset()'s own internal withLock nests without deadlocking). Two other findings from the same pass were assessed and left as-is: - shutdown() blocking on an in-flight reset with no cancellation is real but performance-only (no crash/corruption), requires disproportionate cancellation plumbing through SourceFileManager/JavaCompilerService for a narrow, bounded-cost edge case. - KotlinLanguageServer's eager construction is a real observation about this branch's current state, but it's already fixed by the separate, not-yet- merged ADFA-5010 (PR #1635) -- out of scope here, not a gap in this PR. Verified: :lsp:java unit tests pass, :app:assembleV8Debug builds clean. --- .../androidide/lsp/java/JavaLanguageServer.kt | 75 ++++++++++++------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt index 763080fc49..096dc08eba 100644 --- a/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt +++ b/lsp/java/src/main/java/com/itsaky/androidide/lsp/java/JavaLanguageServer.kt @@ -249,17 +249,25 @@ class JavaLanguageServer : ILanguageServer { SourceFileManager.forModule(subModule) } startOrRestartAnalyzeTimer() - } finally { - // A newer setupWithProject() may have queued another workspace while we were - // resetting (see the RESETTING guard above); if so, go back to PENDING instead - // of claiming INITIALIZED for a project we didn't actually reset for. - compilerLifecycle = - if (pendingWorkspace != null) { - CompilerLifecycle.PENDING - } else { - CompilerLifecycle.INITIALIZED - } + } catch (e: Exception) { + // Re-queue the workspace so the next real .java-file interaction retries the + // reset, instead of a half-destroyed/half-rebuilt state being silently claimed as + // INITIALIZED (pendingWorkspace is already null by this point). + log.warn("Failed to reset javac project state; will retry on next interaction", e) + pendingWorkspace = workspace + compilerLifecycle = CompilerLifecycle.PENDING + throw e } + + // A newer setupWithProject() may have queued another workspace while we were + // resetting (see the RESETTING guard above); if so, go back to PENDING instead of + // claiming INITIALIZED for a project we didn't actually reset for. + compilerLifecycle = + if (pendingWorkspace != null) { + CompilerLifecycle.PENDING + } else { + CompilerLifecycle.INITIALIZED + } } } @@ -326,6 +334,13 @@ class JavaLanguageServer : ILanguageServer { return DiagnosticResult.NO_UPDATE } + // diagnosticProvider.analyze() builds its own JavaCompilerService directly (bypassing + // getCompiler()), and analysis is often the first real .java-file interaction in a + // session (auto-triggered on file open, ahead of any completion request) -- without this, + // the R.jar/file-manager caches this reset clears would never get cleared for this + // project, and diagnostics could resolve against a stale previous project's classpath. + ensureProjectReset() + return if (!settings.codeAnalysisEnabled()) { DiagnosticResult.NO_UPDATE } else { @@ -352,11 +367,17 @@ class JavaLanguageServer : ILanguageServer { if (!DocumentUtils.isJavaFile(file)) { return JavaCompilerService.NO_MODULE_COMPILER } - ensureProjectReset() - val module = - ProjectManagerImpl.getInstance().findModuleForFile(file!!) - ?: return JavaCompilerService.NO_MODULE_COMPILER - return JavaCompilerProvider.get(module) + // Held across ensureProjectReset() *and* the provider lookup (ReentrantLock is + // reentrant, so ensureProjectReset()'s own withLock nests fine): otherwise a concurrent + // reset for a newer project could destroy() the provider's compilers in the gap between + // this thread's reset finishing and its JavaCompilerProvider.get() call. + return compilerLifecycleLock.withLock { + ensureProjectReset() + val module = + ProjectManagerImpl.getInstance().findModuleForFile(file!!) + ?: return@withLock JavaCompilerService.NO_MODULE_COMPILER + JavaCompilerProvider.get(module) + } } private fun updateCachedCompletion(cachedCompletion: CachedCompletion) { @@ -382,16 +403,20 @@ class JavaLanguageServer : ILanguageServer { return } - ensureProjectReset() - - // TODO Find an alternative to efficiently update changeDelta in JavaCompilerService instance - JavaCompilerService.NO_MODULE_COMPILER.onDocumentChange(event) - val module = - getInstance() - .findModuleForFile(event.changedFile) - if (module != null) { - val compiler = JavaCompilerProvider.get(module) - compiler.onDocumentChange(event) + // See getCompiler(): held across the reset *and* the provider lookup/use so a concurrent + // reset can't destroy() these compilers in between. + compilerLifecycleLock.withLock { + ensureProjectReset() + + // TODO Find an alternative to efficiently update changeDelta in JavaCompilerService instance + JavaCompilerService.NO_MODULE_COMPILER.onDocumentChange(event) + val module = + getInstance() + .findModuleForFile(event.changedFile) + if (module != null) { + val compiler = JavaCompilerProvider.get(module) + compiler.onDocumentChange(event) + } } startOrRestartAnalyzeTimer() }