From 91f3baa83854bf0f9fcc8a6d285eb2dea922d007 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Thu, 28 Sep 2017 12:00:18 +0300 Subject: [PATCH] BackgroundTaskUtil.executeOnPooledThread: better disposable registration 1. Register the disposable in the EDT instead of just read action, to support even more clients. Require parent disposal to be in the EDT as well. 2. Don't swallow the IOE if the registration fails because the call was out-of-sync with the parent disposal process to let the caller fix the issue. Based on comments in IDEA-CR-24663. --- .../progress/util/BackgroundTaskUtil.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java b/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java index 0e41d05f596d..db19a831858b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/progress/util/BackgroundTaskUtil.java @@ -30,9 +30,9 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.Ref; import com.intellij.util.Consumer; import com.intellij.util.Function; -import com.intellij.util.IncorrectOperationException; import com.intellij.util.PairConsumer; import com.intellij.util.concurrency.AppExecutorUtil; import com.intellij.util.messages.MessageBus; @@ -201,6 +201,10 @@ public class BackgroundTaskUtil { * * This allows to stop a lengthy background activity by calling {@link ProgressManager#checkCanceled()} * and avoid Already Disposed exceptions (in particular, because checkCanceled() is called in {@link ServiceManager#getService(Class)}. + *

+ * + * The parent disposable should be disposed in the EDT, otherwise it is responsibility of the caller + * to synchronize the parent disposal and the call to this method. */ @NotNull @CalledInAny @@ -264,17 +268,17 @@ public class BackgroundTaskUtil { } private static boolean registerIfParentNotDisposed(@NotNull Disposable parent, @NotNull Disposable disposable) { - return ReadAction.compute(() -> { - if (Disposer.isDisposed(parent)) return false; - try { - Disposer.register(parent, disposable); - return true; + Ref result = Ref.create(false); + ApplicationManager.getApplication().invokeAndWait(() -> { + if (Disposer.isDisposed(parent)) { + result.set(false); } - catch(IncorrectOperationException ioe) { - LOG.error(ioe); - return false; + else { + Disposer.register(parent, disposable); + result.set(true); } }); + return result.get(); } @CalledInAny