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:
@@ -6,6 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
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
|
||||
@@ -73,7 +74,7 @@ public final class Sessions {
|
||||
if (microphoneOwner == session) return;
|
||||
ServerSession previous = microphoneOwner;
|
||||
microphoneOwner = session;
|
||||
handover.execute(() -> {
|
||||
handOver(() -> {
|
||||
if (previous != null) previous.setMicrophoneActive(false);
|
||||
if (session != null) session.setMicrophoneActive(true);
|
||||
});
|
||||
@@ -105,7 +106,7 @@ public final class Sessions {
|
||||
public void disconnected(ServerSession session) {
|
||||
if (microphoneOwner == session) {
|
||||
microphoneOwner = null;
|
||||
handover.execute(() -> session.setMicrophoneActive(false));
|
||||
handOver(() -> session.setMicrophoneActive(false));
|
||||
}
|
||||
assignMicrophoneIfFree();
|
||||
}
|
||||
@@ -134,6 +135,18 @@ public final class Sessions {
|
||||
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 ----
|
||||
|
||||
public void setAwayEverywhere(boolean away, String message) {
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
|
||||
/**
|
||||
* Tells the user about pokes and private messages through the desktop's notifications
|
||||
@@ -66,7 +67,7 @@ public final class DesktopNotifier {
|
||||
if (!wanted()) return;
|
||||
String body = BBCode.toPlain(message);
|
||||
Notice notice = new Notice(from + " poked you", body, () -> open(tab, null), null);
|
||||
worker.execute(() -> {
|
||||
later(() -> {
|
||||
DesktopNotifications out = backend();
|
||||
if (out == null) return;
|
||||
int id = out.show(notice, 0);
|
||||
@@ -78,7 +79,7 @@ public final class DesktopNotifier {
|
||||
if (!wanted()) return;
|
||||
String text = ChatHtml.plainMessage(message);
|
||||
Conversation key = new Conversation(tab, clientId);
|
||||
worker.execute(() -> {
|
||||
later(() -> {
|
||||
DesktopNotifications out = backend();
|
||||
if (out == null) return;
|
||||
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. */
|
||||
void windowActivated() {
|
||||
worker.execute(() -> {
|
||||
later(() -> {
|
||||
if (backend == null) return;
|
||||
conversations.values().forEach(shown -> backend.close(shown.id));
|
||||
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. */
|
||||
void forget(ServerTab tab) {
|
||||
worker.execute(() -> conversations.entrySet().removeIf(e -> {
|
||||
later(() -> conversations.entrySet().removeIf(e -> {
|
||||
if (e.getKey().tab() != tab) return false;
|
||||
if (backend != null) backend.close(e.getValue().id);
|
||||
return true;
|
||||
@@ -111,12 +112,23 @@ public final class DesktopNotifier {
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
worker.execute(() -> {
|
||||
later(() -> {
|
||||
if (backend != null) backend.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() {
|
||||
// Some window of ours has the focus exactly when there is an active window.
|
||||
return settings.desktopNotifications.get()
|
||||
|
||||
Reference in New Issue
Block a user