Skip to content

Commit a39ea79

Browse files
tferrclaude
andcommitted
Replace Thread.stop() reliance in Executer.obliterate()
hread.stop() throws UnsupportedOperationException since JDK 20, so killing a script logged a stack trace per thread and only interrupted (when anything at all). Now: - Interrupt all threads of the executer (deduplicated), then try stop() only on threads still alive, giving up on the first UnsupportedOperationException - After a 1s grace period, report threads that are still running (script name included, wrapper thread labeled "runner") in the error screen and in the status bar, with a new error icon - Refresh the Kill menu and window title after obliterate(), since the wrapper thread's finally block may never run if it survives - showTemporaryStatus() accepts an optional icon, and always clears the icon of a previous message Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 638b3a0 commit a39ea79

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎src/main/java/org/scijava/ui/swing/script/Icons.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.formdev.flatlaf.FlatLaf;
3333
import com.formdev.flatlaf.icons.FlatAbstractIcon;
34+
import com.formdev.flatlaf.icons.FlatOptionPaneErrorIcon;
3435
import com.formdev.flatlaf.ui.FlatUIUtils;
3536
import com.formdev.flatlaf.util.UIScale;
3637

@@ -63,6 +64,13 @@ final class Icons {
6364
private Icons() {
6465
}
6566

67+
/** Small error icon for status messages */
68+
static Icon error() {
69+
final FlatAbstractIcon icon = new FlatOptionPaneErrorIcon();
70+
icon.setScale(.35f);
71+
return icon;
72+
}
73+
6674
/** Folder icon for the Files side tab (follows the component foreground) */
6775
static Icon folder() {
6876
return new GlyphIcon(FOLDER, null, null, false, 0, 0, 640, 640, 18);

‎src/main/java/org/scijava/ui/swing/script/TextEditor.java‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.Enumeration;
8787
import java.util.HashMap;
8888
import java.util.HashSet;
89+
import java.util.LinkedHashSet;
8990
import java.util.LinkedHashMap;
9091
import java.util.List;
9192
import java.util.Map;
@@ -2004,12 +2005,19 @@ private void setAutoCompletionEnabled(final boolean enabled) {
20042005
* given delay. Text set by other means in the meantime is left untouched
20052006
*/
20062007
void showTemporaryStatus(final String message, final int millis) {
2008+
showTemporaryStatus(message, millis, null);
2009+
}
2010+
2011+
/** As above, with an optional icon shown next to the message */
2012+
void showTemporaryStatus(final String message, final int millis, final Icon icon) {
20072013
if (statusRestoreTimer != null && statusRestoreTimer.isRunning())
20082014
statusRestoreTimer.stop(); // keep the text saved by the pending message
20092015
else
20102016
statusRestoreText = statusText.getText();
20112017
statusText.setText(message);
2018+
statusText.setIcon(icon); // null clears the icon of a previous message
20122019
statusRestoreTimer = new Timer(millis, e -> {
2020+
if (icon != null && statusText.getIcon() == icon) statusText.setIcon(null);
20132021
if (message.equals(statusText.getText()))
20142022
statusText.setText(statusRestoreText);
20152023
});
@@ -3458,8 +3466,11 @@ List<Thread> getAllThreads() {
34583466
}
34593467

34603468
/**
3461-
* Totally destroy/stop all threads in this and all recursive thread
3462-
* subgroups. Will remove itself from the executingTasks list.
3469+
* Interrupts all threads in this and all recursive thread subgroups, and
3470+
* additionally stops them if the JVM still allows it (Thread.stop() throws
3471+
* an UnsupportedOperationException since JDK 20). Threads that ignore the
3472+
* interrupt are reported in the error screen. Will remove itself from the
3473+
* executingTasks list.
34633474
*/
34643475
@SuppressWarnings("deprecation")
34653476
void obliterate() {
@@ -3471,17 +3482,50 @@ void obliterate() {
34713482
catch (final Exception e) {
34723483
log.error(e);
34733484
}
3474-
for (final Thread thread : getAllThreads()) {
3485+
final List<Thread> threads = getDistinctThreads();
3486+
threads.forEach(Thread::interrupt);
3487+
Thread.yield(); // give them a chance
3488+
for (final Thread thread : threads) {
3489+
if (!thread.isAlive()) continue;
34753490
try {
3476-
thread.interrupt();
3477-
Thread.yield(); // give it a chance
34783491
thread.stop();
34793492
}
3493+
catch (final UnsupportedOperationException unsupported) {
3494+
break; // no point in trying the remaining threads
3495+
}
34803496
catch (final Throwable t) {
34813497
log.error(t);
34823498
}
34833499
}
34843500
executingTasks.remove(this);
3501+
// The wrapper thread may survive (no Thread.stop()): refresh the state it would have
3502+
SwingUtilities.invokeLater(() -> kill.setEnabled(executingTasks.size() > 0));
3503+
setTitle();
3504+
reportSurvivors();
3505+
}
3506+
3507+
private List<Thread> getDistinctThreads() {
3508+
return new ArrayList<>(new LinkedHashSet<>(getAllThreads()));
3509+
}
3510+
3511+
/**
3512+
* Checks after a grace period (threads spawned in the meantime included)
3513+
* and flags threads that are still running in the console and status bar
3514+
*/
3515+
private void reportSurvivors() {
3516+
final String script = scriptNameOf(this);
3517+
final Timer timer = new Timer(1000, e -> {
3518+
final List<String> names = new ArrayList<>();
3519+
for (final Thread t : getDistinctThreads())
3520+
if (t.isAlive()) names.add(t.getName().equals(getName()) ? "runner" : t.getName());
3521+
if (names.isEmpty()) return;
3522+
errorScreen.append("\n[Kill] " + names.size() + " thread(s) of " + script
3523+
+ " still running 1s after Kill: " + String.join(", ", names) + "\n");
3524+
showTemporaryStatus(names.size() + " thread(s) of " + script + " still running after Kill. "
3525+
+ "See Errors", 15000, Icons.error());
3526+
});
3527+
timer.setRepeats(false);
3528+
timer.start();
34853529
}
34863530

34873531
@Override
@@ -3550,6 +3594,15 @@ public List<Executer> getExecutingTasks() {
35503594
return executingTasks;
35513595
}
35523596

3597+
/** The name of the script run by the executer (its tab is looked up) */
3598+
private String scriptNameOf(final Executer executer) {
3599+
for (int i = 0; i < tabbed.getTabCount(); i++) {
3600+
final TextEditorTab tab = (TextEditorTab) tabbed.getComponentAt(i);
3601+
if (executer == tab.getExecuter()) return tab.editorPane.getFileName();
3602+
}
3603+
return "script";
3604+
}
3605+
35533606
public void kill(final Executer executer) {
35543607
for (int i = 0; i < tabbed.getTabCount(); i++) {
35553608
final TextEditorTab tab = (TextEditorTab) tabbed.getComponentAt(i);

0 commit comments

Comments
 (0)