From 943655cbdcc2ada61f550cc6f144a355ca83d915 Mon Sep 17 00:00:00 2001 From: Konstantin Nisht Date: Tue, 4 Nov 2025 16:18:07 +0100 Subject: [PATCH] [threading] IJPL-216787: Avoid deadlock with synchronous refresh invoked inside background write action GitOrigin-RevId: e4a3a51cf17acf430cce38165377a7d7d06126a7 --- .../intellij/openapi/vfs/newvfs/RefreshQueueImpl.kt | 2 +- .../openapi/vfs/newvfs/persistent/VfsRefreshTest.kt | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshQueueImpl.kt b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshQueueImpl.kt index dd00805b40fc..0b3024676da8 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshQueueImpl.kt +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshQueueImpl.kt @@ -70,7 +70,7 @@ class RefreshQueueImpl(coroutineScope: CoroutineScope) : RefreshQueue(), Disposa queueSession(session, session.modality) } } - else if (EDT.isCurrentThreadEdt()) { + else if (EDT.isCurrentThreadEdt() || ApplicationManager.getApplication().isWriteAccessAllowed) { (TransactionGuard.getInstance() as TransactionGuardImpl).assertWriteActionAllowed() val events = runRefreshSession(session, -1L) fireEvents(events, session) diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/newvfs/persistent/VfsRefreshTest.kt b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/newvfs/persistent/VfsRefreshTest.kt index 1da2e8a6278d..f6a044f0d31d 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/newvfs/persistent/VfsRefreshTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/newvfs/persistent/VfsRefreshTest.kt @@ -231,4 +231,17 @@ class VfsRefreshTest { } assertEquals(currentCounter, newCounter, "There should be no write action if there was nothing to refresh") } + + @Test + fun `synchronous refresh in a background write action can terminate successfully`() = timeoutRunBlocking { + val file = createTempFile() + val virtualFile = VirtualFileManager.getInstance().findFileByNioPath(file)!! + writeAction { + virtualFile.writeText("42") + } + backgroundWriteAction { + virtualFile.refresh(false, false) + } + // if this test terminates, there was no hanging + } } \ No newline at end of file