From 3b8b513936d1a62d293ffdd375de73b925b8b53e Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Thu, 7 Dec 2023 19:52:12 +0100 Subject: [PATCH] Fix tests and prevent bugs in the future: commit SDK under write action only GitOrigin-RevId: fda21f17f8a68005b0299fed33b9daecafa8bcc9 --- .../python/tools/sdkTools/PySdkTools.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/python/testFramework/src/com/jetbrains/python/tools/sdkTools/PySdkTools.java b/python/testFramework/src/com/jetbrains/python/tools/sdkTools/PySdkTools.java index 78cb0c518276..b8578cba84e0 100644 --- a/python/testFramework/src/com/jetbrains/python/tools/sdkTools/PySdkTools.java +++ b/python/testFramework/src/com/jetbrains/python/tools/sdkTools/PySdkTools.java @@ -114,19 +114,36 @@ public final class PySdkTools { addTestSdkRoot(modificator, path); } if (!addSkeletons) { - ApplicationManager.getApplication().invokeAndWait(modificator::commitChanges); + commitChangesObeyWriteAction(modificator); return; } final String skeletonsPath = PythonSdkUtil.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath()); addTestSdkRoot(modificator, skeletonsPath); - ApplicationManager.getApplication().invokeAndWait(modificator::commitChanges); + commitChangesObeyWriteAction(modificator); PySkeletonRefresher .refreshSkeletonsOfSdk(project, null, skeletonsPath, sdk); } + /** + * {@link SdkModificator#commitChanges()} is marked with {@link com.intellij.util.concurrency.annotations.RequiresWriteLock} and can't be called without it + */ + private static void commitChangesObeyWriteAction(@NotNull SdkModificator modificator) { + var app = ApplicationManager.getApplication(); + app.invokeAndWait(() -> { + if (app.isWriteAccessAllowed()) { + modificator.commitChanges(); + } + else { + WriteAction.run(() -> { + modificator.commitChanges(); + }); + } + }); + } + public static void addTestSdkRoot(@NotNull SdkModificator sdkModificator, @NotNull String path) { final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); if (file != null) {