Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public void processEvent(RevEngAIAnalysisStatusChangedEvent event) {
// Unknown: the wait for it has already ended, so starting another would end at once and loop
case Unknown -> {}
default -> {
if (event.isFromWait()) {
// Progress from a wait, which is tracked already, or has ended since it reported this: starting
// another here would resume a wait the user cancelled
return;
}
// A new or resumed analysis: an earlier ending no longer applies
if (terminalStatusLines.remove(event.getProgram()) != null) {
refreshIfActive(event.getProgram());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* (e.g. function info). Use {@link ai.reveng.toolkit.ghidra.core.RevEngAIAnalysisResultsLoaded} for that.
*/
public class RevEngAIAnalysisStatusChangedEvent extends PluginEvent {
/// The source of the statuses a running wait reports, see {@link #isFromWait()}
public static final String WAIT_SOURCE = "RevEng.AI analysis wait";

private final AnalysisStatus status;
private final GhidraRevengService.ProgramWithID programWithID;

Expand All @@ -41,6 +44,12 @@ public void fireLater(PluginTool tool) {
Swing.runLater(() -> tool.firePluginEvent(this));
}

/// Whether a running wait reported this status. Such a status is delivered later than it was seen (see
/// {@link #fireLater}), so the wait may already have ended, e.g. because it was cancelled.
public boolean isFromWait() {
return WAIT_SOURCE.equals(getSourceName());
}

public AnalysisStatus getStatus() {
return status;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,8 @@ public StatusPoller.Result<AnalysisStatus> waitForAnalysis(
if (currentStatus != lastStatus[0]) {
lastStatus[0] = currentStatus;
if (tool != null) {
new RevEngAIAnalysisStatusChangedEvent(null, programWithID, currentStatus).fireLater(tool);
new RevEngAIAnalysisStatusChangedEvent(RevEngAIAnalysisStatusChangedEvent.WAIT_SOURCE, programWithID, currentStatus)
.fireLater(tool);
}
}
return switch (currentStatus) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/ai/reveng/TestAnalysisLogComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,42 @@ public AnalysisStatus status(AnalysisID analysisID) {
assertFalse(taskMonitorComponent.isVisible());
}

/// A wait's statuses are delivered after it reported them, so one can arrive once the wait has been cancelled.
/// It must not start the wait again.
@Test
public void testStatusFromACancelledWaitDoesNotResumeIt() throws Exception {
var defaultTool = env.showTool();
var programWithID = getPlaceHolderID();
var statusCalls = new AtomicInteger();
var service = addMockedService(defaultTool, new UnimplementedAPI() {
@Override
public AnalysisStatus status(AnalysisID analysisID) {
statusCalls.incrementAndGet();
return AnalysisStatus.Queued;
}
});
attach(service, programWithID);
env.addPlugin(AnalysisManagementPlugin.class);
defaultTool.firePluginEvent(
new RevEngAIAnalysisStatusChangedEvent("Test", programWithID, AnalysisStatus.Queued));

AnalysisLogComponent logComponent = (AnalysisLogComponent) defaultTool.getComponentProvider(AnalysisLogComponent.NAME);
var trackedPrograms = (Map<Program, Task>) getInstanceField("trackedPrograms", logComponent);
((TaskMonitorComponent) getInstanceField("taskMonitorComponent", logComponent)).cancel();
waitForTasks();
assertFalse(trackedPrograms.containsKey(programWithID.program()));

// The Queued the cancelled wait reported, arriving late
runSwing(() -> defaultTool.firePluginEvent(new RevEngAIAnalysisStatusChangedEvent(
RevEngAIAnalysisStatusChangedEvent.WAIT_SOURCE, programWithID, AnalysisStatus.Queued)));
waitForSwing();
int callsAfterCancel = statusCalls.get();
waitForTasks();

assertFalse("a late status must not start another wait", trackedPrograms.containsKey(programWithID.program()));
assertEquals("no polling after the cancel", callsAfterCancel, statusCalls.get());
}

/// An analysis that was still Uploaded when Ghidra closed is waited on again when the program is reopened
@Test
public void testReopenedUploadedAnalysisIsWaitedOn() throws Exception {
Expand Down
Loading