From ae53390ee56d06949f633eb98c4b5e387fbf6deb Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Fri, 5 Jul 2024 22:50:17 +0200 Subject: [PATCH] Do not break acquiring the write lock if the application is already being shut down. Otherwise, this could lead to an inconsistent state and a deadlock: 1) startWrite() throws 2) endWrite() is never called 3) subsequent read waits forever IJ-MR-139088 GitOrigin-RevId: b59e0ebbf04bb0230b2267b88de6e04fa5956766 --- .../impl/AnyThreadWriteThreadingSupport.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/application/impl/AnyThreadWriteThreadingSupport.kt b/platform/platform-impl/src/com/intellij/openapi/application/impl/AnyThreadWriteThreadingSupport.kt index a6cfecdbb52a..691b15fc9459 100644 --- a/platform/platform-impl/src/com/intellij/openapi/application/impl/AnyThreadWriteThreadingSupport.kt +++ b/platform/platform-impl/src/com/intellij/openapi/application/impl/AnyThreadWriteThreadingSupport.kt @@ -9,6 +9,7 @@ import com.intellij.diagnostic.PluginException import com.intellij.ide.IdeBundle import com.intellij.openapi.application.* import com.intellij.openapi.application.ex.ApplicationUtil +import com.intellij.openapi.diagnostic.ControlFlowException import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.progress.* import com.intellij.openapi.progress.util.PotemkinProgress @@ -501,7 +502,16 @@ internal object AnyThreadWriteThreadingSupport: ThreadingSupport { val t = System.currentTimeMillis() val permit = acquisitor() val elapsed = System.currentTimeMillis() - t - WriteDelayDiagnostics.registerWrite(elapsed) + try { + WriteDelayDiagnostics.registerWrite(elapsed) + } + catch (thr: Throwable) { + // we can be canceled here, it is an expected behavior + if (thr !is ControlFlowException) { + // Warn instead of error to avoid breaking acquiring the lock + logger.warn("Failed to register write lock in diagnostics service", thr) + } + } if (logger.isDebugEnabled) { if (elapsed != 0L) { logger.debug("Write action wait time: $elapsed")