Skip to content

Commit 8a65459

Browse files
committed
CommandModule: fix horrible bug in cancelation
If your Command implemented Cancelable, then calling cancel(...) from within the command itself (e.g., from an initializer callback) did not actually cancel anything, because the wrapping CommandModule instance did not recognize the cancelation state of its wrapped Command. Now, the CommandModule always leans on its wrapped Command for all cancelation-related operations, unless the Command itself does not implement Cancelable. Noticed by Richard Domander. Thanks to Mark Hiner for help debugging.
1 parent 01547c6 commit 8a65459

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main/java/org/scijava/command/CommandModule.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public class CommandModule extends AbstractModule implements Cancelable,
8686
@Parameter
8787
private Context context;
8888

89-
/** Reason for cancelation, or null if not canceled. */
89+
/**
90+
* Reason for cancelation, or null if not canceled. Note that this field is
91+
* only relevant if the delegate {@link Command} is not itself
92+
* {@link Cancelable}.
93+
*/
9094
private String cancelReason;
9195

9296
/** Creates a command module for the given {@link PluginInfo}. */
@@ -205,20 +209,26 @@ public void run() {
205209

206210
@Override
207211
public boolean isCanceled() {
212+
if (command instanceof Cancelable) {
213+
return ((Cancelable) command).isCanceled();
214+
}
208215
return cancelReason != null;
209216
}
210217

211218
@Override
212219
public void cancel(final String reason) {
213-
cancelReason = reason == null ? "" : reason;
214220
if (command instanceof Cancelable) {
215-
// propagate cancelation to the command instance itself
216221
((Cancelable) command).cancel(reason);
222+
return;
217223
}
224+
cancelReason = reason == null ? "" : reason;
218225
}
219226

220227
@Override
221228
public String getCancelReason() {
229+
if (command instanceof Cancelable) {
230+
return ((Cancelable) command).getCancelReason();
231+
}
222232
return cancelReason;
223233
}
224234

0 commit comments

Comments
 (0)