Skip to content

Commit

Permalink
Fix InterruptedException in projects with large dependency trees (#419)
Browse files Browse the repository at this point in the history
* Increased scan timeout limit to 1 hour.
* Fixed scan cancellation not working.
  • Loading branch information
asafgabai authored Sep 19, 2023
1 parent d08cb2a commit c070426
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/jfrog/ide/idea/scan/ScanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static com.jfrog.ide.common.utils.XrayConnectionUtils.createXrayClientBuilder;

public class ScanManager {
private final int SCAN_TIMEOUT_MINUTES = 10;
private final int SCAN_TIMEOUT_MINUTES = 60;
private final Project project;
private final ScannerFactory factory;
private final SourceCodeScannerManager sourceCodeScannerManager;
Expand Down Expand Up @@ -96,8 +96,9 @@ public void startScan() {
scanner.asyncScanAndUpdateResults();
}
executor.shutdown();
//noinspection ResultOfMethodCallIgnored
executor.awaitTermination(SCAN_TIMEOUT_MINUTES, TimeUnit.MINUTES);
if (!executor.awaitTermination(SCAN_TIMEOUT_MINUTES, TimeUnit.MINUTES)) {
logError(Logger.getInstance(), "Scan timeout of " + SCAN_TIMEOUT_MINUTES + " minutes elapsed. The scan is being canceled.", true);
}
// Cache tree only if no errors occurred during scan.
if (scanners.values().stream().anyMatch(ScannerBase::isScanInterrupted)) {
componentsTree.deleteCachedTree();
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.jfrog.ide.common.log.Utils.logError;
Expand All @@ -75,6 +76,7 @@ public abstract class ScannerBase {
protected SourceCodeScannerManager sourceCodeScannerManager;
String basePath;
private ExecutorService executor;
private com.intellij.openapi.progress.ProgressIndicator progressIndicator;

/**
* @param project currently opened IntelliJ project. We'll use this project to retrieve project based services
Expand Down Expand Up @@ -295,6 +297,7 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat
log.info("Scan already in progress");
return;
}
progressIndicator = indicator;
scanAndUpdate(new ProgressIndicatorImpl(indicator));
}

Expand All @@ -311,7 +314,7 @@ public void onThrowable(@NotNull Throwable error) {
}

};
executor.submit(createRunnable(scanAndUpdateTask, latch, this.log));
executor.submit(createRunnable(scanAndUpdateTask, latch, progressIndicator, log));
}

/**
Expand All @@ -334,12 +337,13 @@ private String getTaskTitle() {
/**
* Create a runnable to be submitted to the executor service, or run directly.
*
* @param task - The task to submit
* @param latch - The countdown latch, which makes sure the executor service doesn't get more than 3 tasks.
* If null, the scan was initiated by a change in the project descriptor and the executor
* service is terminated. In this case, there is no requirement to wait.
* @param task The task to submit
* @param latch The countdown latch, which makes sure the executor service doesn't get more than 3 tasks.
* If null, the scan was initiated by a change in the project descriptor and the executor
* service is terminated. In this case, there is no requirement to wait.
* @param progressIndicator The task's {@link com.intellij.openapi.progress.ProgressIndicator} object.
*/
public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch latch, Log log) {
public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch latch, com.intellij.openapi.progress.ProgressIndicator progressIndicator, Log log) {
return () -> {
// The progress manager is only good for foreground threads.
if (SwingUtilities.isEventDispatchThread()) {
Expand All @@ -354,7 +358,9 @@ public static Runnable createRunnable(Task.Backgroundable task, CountDownLatch l
latch.await();
}
} catch (InterruptedException e) {
logError(log, ExceptionUtils.getRootCauseMessage(e), e, true);
// This exception is thrown when this thread is interrupted (e.g. when the scan timeout has elapsed).
logError(log, ExceptionUtils.getRootCauseMessage(e), e, false);
progressIndicator.cancel();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SourceCodeScannerManager {
protected Project project;
protected PackageManagerType packageType;
private static final String SKIP_FOLDERS_SUFFIX = "*/**";
private com.intellij.openapi.progress.ProgressIndicator progressIndicator;

public SourceCodeScannerManager(Project project) {
this.project = project;
Expand Down Expand Up @@ -113,6 +114,7 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat
log.info("Advanced source code scan is already in progress");
return;
}
progressIndicator = indicator;
sourceCodeScanAndUpdate(new ProgressIndicatorImpl(indicator), ProgressManager::checkCanceled, log);
}

Expand All @@ -128,7 +130,7 @@ public void onThrowable(@NotNull Throwable error) {
}

};
executor.submit(createRunnable(sourceCodeScanTask, latch, log));
executor.submit(createRunnable(sourceCodeScanTask, latch, progressIndicator, log));
}

private void sourceCodeScanAndUpdate(ProgressIndicator indicator, Runnable checkCanceled, Logger log) {
Expand Down

0 comments on commit c070426

Please sign in to comment.