fix SSH ping CPU spin and Linux releases
This commit is contained in:
@@ -222,11 +222,82 @@ async function testCompleteAppTeardownIsImmediateAndIdempotent() {
|
||||
assert.strictEqual(app._closingSessions.size, 0);
|
||||
}
|
||||
|
||||
async function testLatencyPingIsSingleFlightAndIgnoresStaleResults() {
|
||||
const flushAsync = () => new Promise((resolve) => setImmediate(resolve));
|
||||
let nextTimerID = 1;
|
||||
const timers = new Map();
|
||||
const latencyEl = { textContent: "", className: "" };
|
||||
const context = loadScript("src/js/app.js", {
|
||||
setTimeout: (callback) => {
|
||||
const id = nextTimerID++;
|
||||
timers.set(id, callback);
|
||||
return id;
|
||||
},
|
||||
clearTimeout: (id) => timers.delete(id),
|
||||
document: {
|
||||
querySelector: () => null,
|
||||
getElementById: (id) => (id === "status-latency" ? latencyEl : null),
|
||||
addEventListener: () => {},
|
||||
},
|
||||
});
|
||||
const App = context.window.NexusTerminal.App;
|
||||
const app = Object.create(App.prototype);
|
||||
app._latencySessionID = null;
|
||||
app._latencyTimer = null;
|
||||
app._latencyGeneration = 0;
|
||||
app._latencyPingInFlight = false;
|
||||
|
||||
const pending = [];
|
||||
context.window.NexusTerminal.wailsService = {
|
||||
PingSession: (sessionID) =>
|
||||
new Promise((resolve, reject) => {
|
||||
pending.push({ sessionID, resolve, reject });
|
||||
}),
|
||||
};
|
||||
|
||||
app._startLatencyPing("session-a");
|
||||
app._startLatencyPing("session-a");
|
||||
assert.strictEqual(pending.length, 1, "an in-flight ping must not overlap");
|
||||
assert.strictEqual(timers.size, 0, "the next ping waits for completion");
|
||||
|
||||
app._startLatencyPing("session-b");
|
||||
assert.strictEqual(pending.length, 2);
|
||||
pending[0].reject(new Error("stale session closed"));
|
||||
await flushAsync();
|
||||
assert.strictEqual(app._latencySessionID, "session-b");
|
||||
assert.strictEqual(
|
||||
app._latencyPingInFlight,
|
||||
true,
|
||||
"a stale rejection must not stop the current session ping",
|
||||
);
|
||||
|
||||
pending[1].resolve(42);
|
||||
await flushAsync();
|
||||
assert.strictEqual(latencyEl.textContent, "42 ms");
|
||||
assert.strictEqual(latencyEl.className, "latency-good");
|
||||
assert.strictEqual(timers.size, 1, "one follow-up ping should be scheduled");
|
||||
|
||||
const followUp = timers.values().next().value;
|
||||
timers.clear();
|
||||
followUp();
|
||||
assert.strictEqual(pending.length, 3);
|
||||
assert.strictEqual(pending[2].sessionID, "session-b");
|
||||
assert.strictEqual(timers.size, 0, "follow-up pings must also be single-flight");
|
||||
|
||||
pending[2].resolve(250);
|
||||
await flushAsync();
|
||||
assert.strictEqual(latencyEl.className, "latency-bad");
|
||||
assert.strictEqual(timers.size, 1);
|
||||
app._stopLatencyPing();
|
||||
assert.strictEqual(timers.size, 0);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
await testRepeatedSplitCleanup();
|
||||
testClosedPaneRejectsLateConnection();
|
||||
testPerSessionStoresReleaseIDs();
|
||||
await testCompleteAppTeardownIsImmediateAndIdempotent();
|
||||
await testLatencyPingIsSingleFlightAndIgnoresStaleResults();
|
||||
console.log("Session lifecycle regression tests passed.");
|
||||
})().catch((err) => {
|
||||
console.error(err);
|
||||
|
||||
+45
-7
@@ -49,6 +49,10 @@
|
||||
this.leviathan = new LeviathanWorkbench(this);
|
||||
/** @type {Map<string, Promise<void>>} Idempotent in-flight session closes. */
|
||||
this._closingSessions = new Map();
|
||||
this._latencySessionID = null;
|
||||
this._latencyTimer = null;
|
||||
this._latencyGeneration = 0;
|
||||
this._latencyPingInFlight = false;
|
||||
this.updateInfo = null;
|
||||
this.mode = "nexus";
|
||||
}
|
||||
@@ -1362,17 +1366,44 @@
|
||||
}
|
||||
|
||||
_startLatencyPing(sessionID) {
|
||||
// Don't restart if already pinging the same session.
|
||||
if (this._latencySessionID === sessionID && this._latencyTimer) return;
|
||||
// Do not queue overlapping backend requests for the active session.
|
||||
if (
|
||||
this._latencySessionID === sessionID &&
|
||||
(this._latencyTimer || this._latencyPingInFlight)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this._stopLatencyPing();
|
||||
this._latencySessionID = sessionID;
|
||||
const generation = this._latencyGeneration;
|
||||
|
||||
const isCurrent = () =>
|
||||
this._latencyGeneration === generation &&
|
||||
this._latencySessionID === sessionID;
|
||||
|
||||
const scheduleNext = () => {
|
||||
if (!isCurrent() || this._latencyTimer) return;
|
||||
this._latencyTimer = setTimeout(() => {
|
||||
this._latencyTimer = null;
|
||||
void doPing();
|
||||
}, 10000);
|
||||
};
|
||||
|
||||
const doPing = async () => {
|
||||
if (!window.NexusTerminal?.wailsService) return;
|
||||
if (!isCurrent()) return;
|
||||
if (!window.NexusTerminal?.wailsService) {
|
||||
scheduleNext();
|
||||
return;
|
||||
}
|
||||
|
||||
this._latencyPingInFlight = true;
|
||||
let succeeded = false;
|
||||
try {
|
||||
const ms = await window.NexusTerminal.wailsService.PingSession(sessionID);
|
||||
if (!isCurrent()) return;
|
||||
succeeded = true;
|
||||
const el = document.getElementById("status-latency");
|
||||
if (el && this._latencySessionID === sessionID) {
|
||||
if (el) {
|
||||
el.textContent = ms + " ms";
|
||||
el.className =
|
||||
ms < 100
|
||||
@@ -1382,6 +1413,7 @@
|
||||
: "latency-bad";
|
||||
}
|
||||
} catch (_) {
|
||||
if (!isCurrent()) return;
|
||||
// Session may have disconnected — clear indicator.
|
||||
const el = document.getElementById("status-latency");
|
||||
if (el) {
|
||||
@@ -1389,18 +1421,24 @@
|
||||
el.className = "";
|
||||
}
|
||||
this._stopLatencyPing();
|
||||
} finally {
|
||||
if (isCurrent()) {
|
||||
this._latencyPingInFlight = false;
|
||||
if (succeeded) scheduleNext();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
doPing();
|
||||
this._latencyTimer = setInterval(doPing, 10000);
|
||||
void doPing();
|
||||
}
|
||||
|
||||
_stopLatencyPing() {
|
||||
this._latencyGeneration = (this._latencyGeneration || 0) + 1;
|
||||
if (this._latencyTimer) {
|
||||
clearInterval(this._latencyTimer);
|
||||
clearTimeout(this._latencyTimer);
|
||||
this._latencyTimer = null;
|
||||
}
|
||||
this._latencyPingInFlight = false;
|
||||
this._latencySessionID = null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user