KAPT: Dispose project disposables
diff --git a/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/Kapt4AnalysisHandlerExtension.kt b/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/Kapt4AnalysisHandlerExtension.kt
index 93d16bb..b1b1eb7 100644
--- a/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/Kapt4AnalysisHandlerExtension.kt
+++ b/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/Kapt4AnalysisHandlerExtension.kt
@@ -113,70 +113,71 @@
         )
 
         val projectDisposable = Disposer.newDisposable("K2KaptSession.project")
-        val projectEnvironment =
-            createProjectEnvironment(updatedConfiguration, projectDisposable, EnvironmentConfigFiles.JVM_CONFIG_FILES, messageCollector)
-        if (messageCollector.hasErrors()) {
-            return false
-        }
-
-        val diagnosticsReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
-
-        if (options.mode.generateStubs) {
-            val (analysisTime, analysisResults) = measureTimeMillis {
-                compileModuleToAnalyzedFir(
-                    compilerInput,
-                    projectEnvironment,
-                    emptyList(),
-                    null,
-                    diagnosticsReporter,
-                )
+        try {
+            val projectEnvironment =
+                createProjectEnvironment(updatedConfiguration, projectDisposable, EnvironmentConfigFiles.JVM_CONFIG_FILES, messageCollector)
+            if (messageCollector.hasErrors()) {
+                return false
             }
 
-            logger.info { "Initial analysis took $analysisTime ms" }
+            val diagnosticsReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
 
-            val (classFilesCompilationTime, codegenOutput) = measureTimeMillis {
-                // Ignore all FE errors
-                val cleanDiagnosticReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
-                val compilerEnvironment = ModuleCompilerEnvironment(projectEnvironment, cleanDiagnosticReporter)
-                val irInput = convertAnalyzedFirToIr(compilerInput, analysisResults, compilerEnvironment, skipBodies = true)
+            if (options.mode.generateStubs) {
+                val (analysisTime, analysisResults) = measureTimeMillis {
+                    compileModuleToAnalyzedFir(
+                        compilerInput,
+                        projectEnvironment,
+                        emptyList(),
+                        null,
+                        diagnosticsReporter,
+                    )
+                }
 
-                generateCodeFromIr(irInput, compilerEnvironment, skipBodies = true)
-            }
+                logger.info { "Initial analysis took $analysisTime ms" }
 
-            val builderFactory = codegenOutput.builderFactory
-            val compiledClasses = (builderFactory as OriginCollectingClassBuilderFactory).compiledClasses
-            val origins = builderFactory.origins
+                val (classFilesCompilationTime, codegenOutput) = measureTimeMillis {
+                    // Ignore all FE errors
+                    val cleanDiagnosticReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
+                    val compilerEnvironment = ModuleCompilerEnvironment(projectEnvironment, cleanDiagnosticReporter)
+                    val irInput = convertAnalyzedFirToIr(compilerInput, analysisResults, compilerEnvironment, skipBodies = true)
 
-            logger.info { "Stubs compilation took $classFilesCompilationTime ms" }
-            logger.info { "Compiled classes: " + compiledClasses.joinToString { it.name } }
+                    generateCodeFromIr(irInput, compilerEnvironment, skipBodies = true)
+                }
 
-            KaptContextForStubGeneration(
-                options, false, logger, compiledClasses, origins, codegenOutput.generationState,
-                analysisResults.outputs.flatMap { it.fir }
-            ).use { context ->
-                generateKotlinSourceStubs(context)
-            }
-        }
+                val builderFactory = codegenOutput.builderFactory
+                val compiledClasses = (builderFactory as OriginCollectingClassBuilderFactory).compiledClasses
+                val origins = builderFactory.origins
 
-        if (options.mode.runAnnotationProcessing) {
-            val (annotationProcessingTime) = measureTimeMillis {
-                KaptContext(
-                    options,
-                    false,
-                    logger
+                logger.info { "Stubs compilation took $classFilesCompilationTime ms" }
+                logger.info { "Compiled classes: " + compiledClasses.joinToString { it.name } }
+
+                KaptContextForStubGeneration(
+                    options, false, logger, compiledClasses, origins, codegenOutput.generationState,
+                    analysisResults.outputs.flatMap { it.fir }
                 ).use { context ->
-                    try {
-                        runProcessors(context, options)
-                    } catch (e: KaptBaseError) {
-                        return false
-                    }
+                    generateKotlinSourceStubs(context)
                 }
             }
 
-            logger.info { "Annotation processing took $annotationProcessingTime ms" }
-        }
+            if (options.mode.runAnnotationProcessing) {
+                val (annotationProcessingTime) = measureTimeMillis {
+                    KaptContext(
+                        options,
+                        false,
+                        logger
+                    ).use { context ->
+                        try {
+                            runProcessors(context, options)
+                        } catch (e: KaptBaseError) {
+                            return false
+                        }
+                    }
+                }
 
-        return true
+                logger.info { "Annotation processing took $annotationProcessingTime ms" }
+            }
+
+            return true
 
 //        val projectDisposable = Disposer.newDisposable("StandaloneAnalysisAPISession.project")
 //        try {
@@ -253,9 +254,9 @@
 //                logger.exception(e)
 //                false
 //            }
-//        } finally {
-//            Disposer.dispose(projectDisposable)
-//        }
+        } finally {
+            Disposer.dispose(projectDisposable)
+        }
     }
 
     private fun generateKotlinSourceStubs(kaptContext: KaptContextForStubGeneration) {
diff --git a/plugins/kapt4/test/org/jetbrains/kotlin/kapt4/Kapt4Facade.kt b/plugins/kapt4/test/org/jetbrains/kotlin/kapt4/Kapt4Facade.kt
index 3ded195..022cd11 100644
--- a/plugins/kapt4/test/org/jetbrains/kotlin/kapt4/Kapt4Facade.kt
+++ b/plugins/kapt4/test/org/jetbrains/kotlin/kapt4/Kapt4Facade.kt
@@ -53,41 +53,45 @@
         )
 
         val projectDisposable = Disposer.newDisposable("K2KaptSession.project")
-        val projectEnvironment =
-            createProjectEnvironment(configuration, projectDisposable, EnvironmentConfigFiles.JVM_CONFIG_FILES, messageCollector)
+        try {
+            val projectEnvironment =
+                createProjectEnvironment(configuration, projectDisposable, EnvironmentConfigFiles.JVM_CONFIG_FILES, messageCollector)
 
-        val diagnosticsReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
+            val diagnosticsReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
 
-        val analysisResults = compileModuleToAnalyzedFir(
-            compilerInput,
-            projectEnvironment,
-            emptyList(),
-            null,
-            diagnosticsReporter,
-        )
+            val analysisResults = compileModuleToAnalyzedFir(
+                compilerInput,
+                projectEnvironment,
+                emptyList(),
+                null,
+                diagnosticsReporter,
+            )
 
-        val cleanDiagnosticReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
-        val compilerEnvironment = ModuleCompilerEnvironment(projectEnvironment, cleanDiagnosticReporter)
-        val irInput = convertAnalyzedFirToIr(compilerInput, analysisResults, compilerEnvironment, skipBodies = true)
-        val codegenOutput = generateCodeFromIr(irInput, compilerEnvironment, skipBodies = true)
+            val cleanDiagnosticReporter = FirKotlinToJvmBytecodeCompiler.createPendingReporter(messageCollector)
+            val compilerEnvironment = ModuleCompilerEnvironment(projectEnvironment, cleanDiagnosticReporter)
+            val irInput = convertAnalyzedFirToIr(compilerInput, analysisResults, compilerEnvironment, skipBodies = true)
+            val codegenOutput = generateCodeFromIr(irInput, compilerEnvironment, skipBodies = true)
 
-        val builderFactory = codegenOutput.builderFactory
-        val compiledClasses = (builderFactory as OriginCollectingClassBuilderFactory).compiledClasses
-        val origins = builderFactory.origins
+            val builderFactory = codegenOutput.builderFactory
+            val compiledClasses = (builderFactory as OriginCollectingClassBuilderFactory).compiledClasses
+            val origins = builderFactory.origins
 
-        val options = testServices.kaptOptionsProvider[module]
+            val options = testServices.kaptOptionsProvider[module]
 
-        val logger = MessageCollectorBackedKaptLogger(
-            isVerbose = true,
-            isInfoAsWarnings = false,
-            messageCollector = testServices.messageCollectorProvider.getCollector(module)
-        )
+            val logger = MessageCollectorBackedKaptLogger(
+                isVerbose = true,
+                isInfoAsWarnings = false,
+                messageCollector = testServices.messageCollectorProvider.getCollector(module)
+            )
 
-        val context = KaptContextForStubGeneration(
-            options, true, logger, compiledClasses, origins, codegenOutput.generationState,
-            analysisResults.outputs.flatMap { it.fir }
-        )
-        return KaptContextBinaryArtifact(context, isFir = true)
+            val context = KaptContextForStubGeneration(
+                options, true, logger, compiledClasses, origins, codegenOutput.generationState,
+                analysisResults.outputs.flatMap { it.fir }
+            )
+            return KaptContextBinaryArtifact(context, isFir = true)
+        } finally {
+            Disposer.dispose(projectDisposable)
+        }
     }
 
     override fun shouldRunAnalysis(module: TestModule): Boolean {