Desktop: no exceptions when servers report leaving after shutdown

Quitting from the shutdown hook (SIGTERM) leaves every server, whose
disconnects then reach the mic hand-over and the notifier after their
threads have stopped. Both now drop work that comes in that late.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 20:30:11 +00:00
co-authored by Claude Opus 5.5
parent e9110c2767
commit 0043c03410
2 changed files with 32 additions and 7 deletions
@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
/** /**
* Every server the client is on, and what they share. Only one of them can capture with the * Every server the client is on, and what they share. Only one of them can capture with the
@@ -73,7 +74,7 @@ public final class Sessions {
if (microphoneOwner == session) return; if (microphoneOwner == session) return;
ServerSession previous = microphoneOwner; ServerSession previous = microphoneOwner;
microphoneOwner = session; microphoneOwner = session;
handover.execute(() -> { handOver(() -> {
if (previous != null) previous.setMicrophoneActive(false); if (previous != null) previous.setMicrophoneActive(false);
if (session != null) session.setMicrophoneActive(true); if (session != null) session.setMicrophoneActive(true);
}); });
@@ -105,7 +106,7 @@ public final class Sessions {
public void disconnected(ServerSession session) { public void disconnected(ServerSession session) {
if (microphoneOwner == session) { if (microphoneOwner == session) {
microphoneOwner = null; microphoneOwner = null;
handover.execute(() -> session.setMicrophoneActive(false)); handOver(() -> session.setMicrophoneActive(false));
} }
assignMicrophoneIfFree(); assignMicrophoneIfFree();
} }
@@ -134,6 +135,18 @@ public final class Sessions {
return session == null || !session.isConnected() ? null : session.microphone(); return session == null || !session.isConnected() ? null : session.microphone();
} }
/**
* Runs a hand-over on {@link #handover}. Leaving every server on {@link #shutdown} reports
* each disconnect to the frontend, which may pass it on here after the thread is gone;
* with every server left, there is nothing to hand over.
*/
private void handOver(Runnable change) {
try {
handover.execute(change);
} catch (RejectedExecutionException ignored) {
}
}
// ---- the user's own state ---- // ---- the user's own state ----
public void setAwayEverywhere(boolean away, String message) { public void setAwayEverywhere(boolean away, String message) {
@@ -19,6 +19,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
/** /**
* Tells the user about pokes and private messages through the desktop's notifications * Tells the user about pokes and private messages through the desktop's notifications
@@ -66,7 +67,7 @@ public final class DesktopNotifier {
if (!wanted()) return; if (!wanted()) return;
String body = BBCode.toPlain(message); String body = BBCode.toPlain(message);
Notice notice = new Notice(from + " poked you", body, () -> open(tab, null), null); Notice notice = new Notice(from + " poked you", body, () -> open(tab, null), null);
worker.execute(() -> { later(() -> {
DesktopNotifications out = backend(); DesktopNotifications out = backend();
if (out == null) return; if (out == null) return;
int id = out.show(notice, 0); int id = out.show(notice, 0);
@@ -78,7 +79,7 @@ public final class DesktopNotifier {
if (!wanted()) return; if (!wanted()) return;
String text = ChatHtml.plainMessage(message); String text = ChatHtml.plainMessage(message);
Conversation key = new Conversation(tab, clientId); Conversation key = new Conversation(tab, clientId);
worker.execute(() -> { later(() -> {
DesktopNotifications out = backend(); DesktopNotifications out = backend();
if (out == null) return; if (out == null) return;
Shown shown = conversations.computeIfAbsent(key, k -> new Shown()); Shown shown = conversations.computeIfAbsent(key, k -> new Shown());
@@ -92,7 +93,7 @@ public final class DesktopNotifier {
/** The window is in front again: what was notified of is in plain sight. */ /** The window is in front again: what was notified of is in plain sight. */
void windowActivated() { void windowActivated() {
worker.execute(() -> { later(() -> {
if (backend == null) return; if (backend == null) return;
conversations.values().forEach(shown -> backend.close(shown.id)); conversations.values().forEach(shown -> backend.close(shown.id));
conversations.clear(); conversations.clear();
@@ -103,7 +104,7 @@ public final class DesktopNotifier {
/** A private chat on a server that went away can neither be opened nor answered. */ /** A private chat on a server that went away can neither be opened nor answered. */
void forget(ServerTab tab) { void forget(ServerTab tab) {
worker.execute(() -> conversations.entrySet().removeIf(e -> { later(() -> conversations.entrySet().removeIf(e -> {
if (e.getKey().tab() != tab) return false; if (e.getKey().tab() != tab) return false;
if (backend != null) backend.close(e.getValue().id); if (backend != null) backend.close(e.getValue().id);
return true; return true;
@@ -111,12 +112,23 @@ public final class DesktopNotifier {
} }
void shutdown() { void shutdown() {
worker.execute(() -> { later(() -> {
if (backend != null) backend.shutdown(); if (backend != null) backend.shutdown();
}); });
worker.shutdown(); worker.shutdown();
} }
/**
* Runs on the worker. The frame may still pass on a server's disconnect after
* {@link #shutdown}, once the worker is gone; nothing is shown any more by then.
*/
private void later(Runnable task) {
try {
worker.execute(task);
} catch (RejectedExecutionException ignored) {
}
}
private boolean wanted() { private boolean wanted() {
// Some window of ours has the focus exactly when there is an active window. // Some window of ours has the focus exactly when there is an active window.
return settings.desktopNotifications.get() return settings.desktopNotifications.get()