Skip to content

Commit d4d270b

Browse files
authored
Merge pull request #2086 from digma-ai/ensure-no-read-access-in-backgroundable
ensue no read access in Backgroundable Closes #2084
2 parents c12d59c + 47f0360 commit d4d270b

File tree

13 files changed

+68
-20
lines changed

13 files changed

+68
-20
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsServiceStarter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public class AnalyticsServiceStarter implements StartupActivity.DumbAware {
1212

1313
@Override
1414
public void runActivity(@NotNull Project project) {
15-
Backgroundable.ensureBackground(project, "initializing analytics service", () -> AnalyticsService.getInstance(project));
15+
Backgroundable.ensureBackgroundWithoutReadAccess(project, "initializing analytics service", () -> AnalyticsService.getInstance(project));
1616
}
1717
}

ide-common/src/main/java/org/digma/intellij/plugin/analytics/Environment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void setCurrent(@NotNull String newEnv, @Nullable Runnable taskToRunAfter
9898
}
9999
};
100100

101-
Backgroundable.ensureBackground(project, "Digma: environment changed " + newEnv, task);
101+
Backgroundable.ensureBackgroundWithoutReadAccess(project, "Digma: environment changed " + newEnv, task);
102102

103103
} catch (Throwable e) {
104104
ErrorReporter.getInstance().reportError(project, "Environment.setCurrent", e);
@@ -123,7 +123,7 @@ public List<String> getEnvironmentsNames() {
123123
public void refreshNowOnBackground() {
124124

125125
Log.log(LOGGER::trace, "Refreshing Environments on background thread.");
126-
Backgroundable.ensureBackground(project, "Refreshing Environments", () -> {
126+
Backgroundable.ensureBackgroundWithoutReadAccess(project, "Refreshing Environments", () -> {
127127
envChangeLock.lock();
128128
try {
129129
//run both refreshEnvironments and updateCurrentEnv under same lock

ide-common/src/main/java/org/digma/intellij/plugin/common/Backgroundable.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.intellij.openapi.application.ApplicationManager;
44
import com.intellij.openapi.diagnostic.Logger;
5-
import com.intellij.openapi.progress.ProgressIndicator;
6-
import com.intellij.openapi.progress.Task;
5+
import com.intellij.openapi.progress.*;
76
import com.intellij.openapi.project.Project;
7+
import com.intellij.util.concurrency.FutureResult;
88
import org.digma.intellij.plugin.errorreporting.ErrorReporter;
99
import org.digma.intellij.plugin.log.Log;
1010
import org.jetbrains.annotations.NotNull;
1111

12-
import java.util.concurrent.Callable;
13-
import java.util.concurrent.Future;
12+
import java.util.concurrent.*;
1413

1514
public class Backgroundable {
1615

@@ -20,6 +19,7 @@ private Backgroundable() {
2019
}
2120

2221

22+
//it's better to use ensureBackgroundWithoutReadAccess
2323
public static void ensureBackground(Project project, String name, Runnable task) {
2424

2525
Log.log(LOGGER::trace, "Request to call task '{}'", name);
@@ -39,6 +39,25 @@ public void run(@NotNull ProgressIndicator indicator) {
3939
}
4040

4141

42+
public static void ensureBackgroundWithoutReadAccess(Project project, String name, Runnable task) {
43+
44+
Log.log(LOGGER::trace, "Request to call task '{}'", name);
45+
46+
if (EDT.isEdt() || ReadActions.isReadAccessAllowed()) {
47+
Log.log(LOGGER::trace, "Executing task '{}' in background thread", name);
48+
new Task.Backgroundable(project, name) {
49+
@Override
50+
public void run(@NotNull ProgressIndicator indicator) {
51+
runWithErrorReporting(project, name, task);
52+
}
53+
}.queue();
54+
} else {
55+
Log.log(LOGGER::trace, "Executing task '{}' in current thread", name);
56+
runWithErrorReporting(project, name, task);
57+
}
58+
}
59+
60+
4261

4362

4463
public static void runInNewBackgroundThread(Project project, String name, Runnable task) {
@@ -52,6 +71,7 @@ public void run(@NotNull ProgressIndicator indicator) {
5271

5372
}
5473

74+
//it's better to use ensurePooledThreadWithoutReadAccess
5575
public static void ensurePooledThread(@NotNull Runnable action){
5676
if (EDT.isEdt()) {
5777
executeOnPooledThread(action);
@@ -60,7 +80,24 @@ public static void ensurePooledThread(@NotNull Runnable action){
6080
}
6181
}
6282

83+
public static void ensurePooledThreadWithoutReadAccess(@NotNull Runnable action) {
84+
if (EDT.isEdt() || ReadActions.isReadAccessAllowed()) {
85+
executeOnPooledThread(action);
86+
} else {
87+
runWithErrorReporting(action);
88+
}
89+
}
90+
91+
public static <T> Future<T> ensurePooledThreadWithoutReadAccess(@NotNull Callable<T> action) {
92+
if (EDT.isEdt() || ReadActions.isReadAccessAllowed()) {
93+
return executeOnPooledThread(action);
94+
} else {
95+
return runWithErrorReporting(action);
96+
}
97+
}
98+
6399

100+
//can use the future to wait for the thread to finish if necessary
64101
public static Future<?> executeOnPooledThread(@NotNull Runnable action) {
65102
return ApplicationManager.getApplication().executeOnPooledThread(() -> runWithErrorReporting(action));
66103
}
@@ -85,7 +122,7 @@ private static void runWithErrorReporting(Project project, String name, Runnable
85122
task.run();
86123
} catch (Throwable e) {
87124
Log.warnWithException(LOGGER, e, "Exception in task {}",name);
88-
ErrorReporter.getInstance().reportError(project, "Backgroundable.runWithErrorReporting." + name, e);
125+
ErrorReporter.getInstance().reportError(project, "Backgroundable.runWithErrorReporting(Project,Runnable)" + name, e);
89126
}
90127
}
91128

@@ -94,7 +131,18 @@ private static void runWithErrorReporting(Runnable task) {
94131
task.run();
95132
} catch (Throwable e) {
96133
Log.warnWithException(LOGGER, e, "Exception in action");
97-
ErrorReporter.getInstance().reportError("Backgroundable.runWithErrorReporting", e);
134+
ErrorReporter.getInstance().reportError("Backgroundable.runWithErrorReporting(Runnable)", e);
135+
}
136+
}
137+
138+
private static <T> Future<T> runWithErrorReporting(@NotNull Callable<T> action) {
139+
140+
try {
141+
return new FutureResult<>(action.call());
142+
} catch (Exception e) {
143+
Log.warnWithException(LOGGER, e, "Exception in action");
144+
ErrorReporter.getInstance().reportError("Backgroundable.runWithErrorReporting(Callable)", e);
145+
return new FutureResult<>();
98146
}
99147
}
100148

ide-common/src/main/kotlin/org/digma/intellij/plugin/codelens/CodeLensService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class CodeLensService(private val project: Project) : Disposable {
221221
selectedEditor?.caretModel?.moveToOffset(it.textOffset)
222222
}
223223
}
224-
Backgroundable.ensurePooledThread {
224+
Backgroundable.ensurePooledThreadWithoutReadAccess {
225225
if (lens.scopeCodeObjectId.startsWith("span:")) {
226226
ScopeManager.getInstance(project).changeScope(SpanScope(lens.scopeCodeObjectId))
227227
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/progress/ProgressUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fun runInvisibleBackgroundTaskInProgressWithRetry(task: RetryableTask.Invisible)
1818
if (task.reuseCurrentThread) {
1919
//even if reuseCurrentThread is true , still make sure it's a background thread. these tasks
2020
// are not meant to run on EDT.
21-
Backgroundable.ensurePooledThread {
21+
Backgroundable.ensurePooledThreadWithoutReadAccess {
2222
invisibleRetryableTask.run()
2323
}
2424
} else {

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/navigation/AbstractNavigationDiscovery.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ abstract class AbstractNavigationDiscovery(protected val project: Project) : Dis
286286
return
287287
}
288288

289-
Backgroundable.ensurePooledThread {
289+
Backgroundable.ensurePooledThreadWithoutReadAccess {
290290
if (virtualFile != null) {
291291
buildLock.lock()
292292
try {
@@ -304,7 +304,7 @@ abstract class AbstractNavigationDiscovery(protected val project: Project) : Dis
304304
return
305305
}
306306

307-
Backgroundable.ensurePooledThread {
307+
Backgroundable.ensurePooledThreadWithoutReadAccess {
308308
buildLock.lock()
309309
try {
310310
removeDiscoveryForPath(path)

python/src/main/java/org/digma/intellij/plugin/psi/python/PythonSpanNavigationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public void fileDeleted(VirtualFile virtualFile) {
226226
return;
227227
}
228228

229-
Backgroundable.ensurePooledThread(() -> {
229+
Backgroundable.ensurePooledThreadWithoutReadAccess(() -> {
230230
if (virtualFile != null) {
231231
buildSpansLock.lock();
232232
try {

src/main/kotlin/org/digma/intellij/plugin/ui/navigation/CodeButtonCaretContextService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CodeButtonCaretContextService(private val project: Project) : CaretContext
5555
}
5656

5757

58-
Backgroundable.ensurePooledThread {
58+
Backgroundable.ensurePooledThreadWithoutReadAccess {
5959
Log.log(logger::trace, "Executing contextChanged in background for {}", methodUnderCaret.id)
6060
val stopWatch = StopWatch.createStarted()
6161
try {

src/main/kotlin/org/digma/intellij/plugin/ui/notificationcenter/EventsNotificationsService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class GoToCodeObjectInsightsAction(
207207
val environmentsSupplier: EnvironmentsSupplier = AnalyticsService.getInstance(project).environment
208208

209209
val runnable = Runnable {
210-
Backgroundable.ensurePooledThread {
210+
Backgroundable.ensurePooledThreadWithoutReadAccess {
211211
ScopeManager.getInstance(project).changeScope(SpanScope(codeObjectId))
212212
}
213213
}

src/main/kotlin/org/digma/intellij/plugin/ui/notificationcenter/RegistrationRequestNotifications.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class RegisterAction(
143143

144144
private fun openRegistrationDialog(project: Project) {
145145
//this is called on EDT, start background and release the EDT thread
146-
Backgroundable.ensurePooledThread {
146+
Backgroundable.ensurePooledThreadWithoutReadAccess {
147147
project.service<RecentActivityService>().openRegistrationDialog()
148148
}
149149
}

0 commit comments

Comments
 (0)