Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- EE reconnects after a switch restart on its own. It used to stop after five attempts within about a minute, before most switches finish restarting, and it waited for Reconnect switch when the connection dropped during an animation. It now retries for as long as it runs, with delays that grow to one minute. When the connection dropped before EE could confirm the switch lighting, EE checks and recovers that lighting itself once the switch answers, and resumes it only if it was running. For 10 minutes after a lost connection, a switch that answers before its lighting is ready is retried as well. A rejected login or SSH key still stops the attempts and asks you to reconnect.
- Quit closes EE even when it cannot confirm that the switch lighting was returned, for example while the switch restarts. The window closes at once, the tray icon goes away when the service has stopped, and the next start recovers the lighting. Before, EE stayed open and asked you to choose Quit again, and the second Quit took up to 25 more seconds.

## 0.6.0

This release removes API routes, saves schedules and port names in a format that 0.5.1 cannot read, keeps the fan mode for each switch in a new place, and moves the build to Python 3.14, Node.js 24.18.0 and .NET 10.
Expand Down
44 changes: 24 additions & 20 deletions desktop/EtherlightingEvolved.Native.Check/Checks/ShutdownChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace EtherlightingEvolved.Native.Check.Checks;
internal static class ShutdownChecks
{
// A loopback fixture with its own child. It records each request, noting whether it carried the administration
// key it was started with, and leaves the recovery journal alone. When its input closes it exits cleanly only if
// told to, standing in for the gateway's own cleanup after a desktop exit.
// key it was started with, and leaves the recovery journal alone. It records the lines it reads, and on "exit"
// ends its child and exits only if told to, standing in for the gateway closing without another release.
const string Gateway = """
import http from 'node:http';
import fs from 'node:fs';
Expand All @@ -18,7 +18,10 @@ internal static class ShutdownChecks
fs.writeFileSync('key', key);
const worker = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], {stdio:'ignore', windowsHide:true});
fs.writeFileSync('child.pid', String(worker.pid));
process.stdin.on('end', () => { if (fs.existsSync('exit-on-input-close')) { worker.kill(); process.exit(0); } });
process.stdin.on('data', chunk => {
fs.appendFileSync('input', String(chunk));
if (String(chunk).includes('exit') && fs.existsSync('exit-on-request')) { worker.kill(); process.exit(1); }
});
process.stdin.resume();
const server = http.createServer(async (req, res) => {
const reply = (code, body) => {res.writeHead(code); res.end(JSON.stringify(body));};
Expand Down Expand Up @@ -48,13 +51,13 @@ public static async Task Run(string output, Action<bool, string> check)
{
await UnexpectedExit(output, check);
var keys = new List<string>();
foreach (var scenario in new[] { "confirmed", "refused-then-cleaned", "refused-unfinished" })
foreach (var scenario in new[] { "confirmed", "refused-then-exited", "refused-unresponsive" })
{
var refused = scenario != "confirmed";
var directory = Directory.CreateDirectory(Path.Combine(output, "quit-" + Guid.NewGuid().ToString("N"))).FullName;
var script = Path.Combine(directory, "gateway.mjs");
await File.WriteAllTextAsync(script, Gateway);
if (scenario == "refused-then-cleaned") await File.WriteAllTextAsync(Path.Combine(directory, "exit-on-input-close"), "");
if (scenario == "refused-then-exited") await File.WriteAllTextAsync(Path.Combine(directory, "exit-on-request"), "");
var journal = Path.Combine(directory, "recovery.json");
await File.WriteAllTextAsync(journal, "{\"pending\":true}");
var receipt = Path.Combine(directory, "last-session.json");
Expand All @@ -69,34 +72,35 @@ public static async Task Run(string output, Action<bool, string> check)
check(key == host.AdminKey && key.Length == 64 && key.All(c => c is >= '0' and <= '9' or >= 'a' and <= 'f'),
$"{scenario}: the service receives this launch's administration key");
using var child = Process.GetProcessById(int.Parse(await File.ReadAllTextAsync(Path.Combine(directory, "child.pid"))));
var tray = host.StopAsync(force: false);
var tray = host.StopAsync();
var requests = Path.Combine(directory, "requests");
var deadline = Stopwatch.StartNew();
while (!File.Exists(requests) && deadline.Elapsed < TimeSpan.FromSeconds(5)) await Task.Delay(20);
var ipc = host.StopAsync(force: false);
var os = host.StopAsync(force: false);
var ipc = host.StopAsync();
var os = host.StopAsync();
check(!tray.IsCompleted && !ipc.IsCompleted && !os.IsCompleted, $"{scenario}: concurrent close requests wait for the first one");
await File.WriteAllTextAsync(Path.Combine(directory, "reply"), refused ? "refused" : "accepted");
var results = await Task.WhenAll(tray, ipc, os).WaitAsync(TimeSpan.FromSeconds(10));
var ending = Stopwatch.StartNew();
var results = await Task.WhenAll(tray, ipc, os).WaitAsync(TimeSpan.FromSeconds(30));
check(results.All(result => result == !refused), $"{scenario}: every close request gets the same result");
check((await File.ReadAllLinesAsync(requests)).SequenceEqual(["POST /api/workspace/close key"]),
$"{scenario}: one workspace close, with the administration key, is sent for concurrent requests");
check(!host.Running, $"{scenario}: Quit stops the service");
var input = Path.Combine(directory, "input");
if (!refused)
{
check(!host.Running && !File.Exists(receipt), "a confirmed Quit stops the service and records nothing");
check(!File.Exists(receipt) && !File.Exists(input), "a confirmed Quit sends no exit request and records nothing");
}
else
{
check(host.Running && !File.Exists(receipt), $"{scenario}: an unconfirmed Quit from the tray keeps the service running");
var forced = await host.StopAsync(force: true).WaitAsync(TimeSpan.FromSeconds(60));
var cleaned = scenario == "refused-then-cleaned";
check(forced == cleaned && !host.Running, cleaned
? "Quit again lets the service finish its own cleanup and exit"
: "Quit again ends a service whose cleanup did not finish");
check(cleaned
? !File.Exists(receipt)
: JsonNode.Parse(await File.ReadAllTextAsync(receipt))?["outcome"]?.GetValue<string>() == "cleanup_unconfirmed",
$"{scenario}: the unconfirmed exit is recorded only when the service had to be ended");
check(File.Exists(input) && File.ReadAllText(input).Contains("exit"),
$"{scenario}: after a refused close the service is told to exit without another attempt");
check(scenario == "refused-then-exited" ? ending.Elapsed < TimeSpan.FromSeconds(10) : ending.Elapsed >= TimeSpan.FromSeconds(14),
scenario == "refused-then-exited"
? "a service that exits on request is not waited on"
: "a service that ignores the exit request is ended after its time is up");
check(JsonNode.Parse(await File.ReadAllTextAsync(receipt))?["outcome"]?.GetValue<string>() == "cleanup_unconfirmed",
$"{scenario}: the unconfirmed exit is recorded so the next start recovers the lighting");
}
await child.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(5));
check(await File.ReadAllTextAsync(journal) == "{\"pending\":true}", $"{scenario}: the existing recovery journal survives desktop exit");
Expand Down
46 changes: 15 additions & 31 deletions desktop/EtherlightingEvolved.Native/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sealed partial class AppHost(Application app, IClassicDesktopStyleApplicationLif
TrayIcon? tray;
NativeMenu? menu;
NativeMenuItem? browserItem;
bool quitting, forceArmed;
bool quitting;
Task? quitTask;

public void Start()
Expand Down Expand Up @@ -273,7 +273,7 @@ void CreateTray()
}
menu.Add(new NativeMenuItemSeparator());
var quitItem = new NativeMenuItem("Quit");
quitItem.Click += (_, _) => _ = QuitAsync(force: false);
quitItem.Click += (_, _) => _ = QuitAsync();
menu.Add(quitItem);
WindowIcon? icon = null;
try { icon = new WindowIcon(AssetLoader.Open(new Uri("avares://EtherlightingEvolved/Assets/ee.ico"))); }
Expand Down Expand Up @@ -333,16 +333,15 @@ async Task<string> HandleAsync(string command)
ShowWindow();
return "ok";
}
// The installer must be able to replace the files, so its request ends EE even if cleanup is unconfirmed.
await QuitAsync(force: true);
await QuitAsync();
return service?.Running == true ? "service_still_running" : "closed";
}

/// <summary>
/// Quits after returning the switch lighting. A Quit from the tray whose return is not confirmed keeps EE
/// running and says so; choosing Quit again, the installer and Windows sign-out end EE anyway.
/// Quits after trying to return the switch lighting. EE closes even when the return is not confirmed, for
/// example while the switch is restarting; the next start recovers the lighting.
/// </summary>
Task QuitAsync(bool force) => quitTask ??= CloseDesktopAsync(force || forceArmed);
Task QuitAsync() => quitTask ??= CloseDesktopAsync();

void PrepareToQuit()
{
Expand All @@ -355,38 +354,23 @@ void PrepareToQuit()
}
}

async Task CloseDesktopAsync(bool force)
async Task CloseDesktopAsync()
{
var code = 0;
try { PrepareToQuit(); window?.Shell.Status("Returning the switch lighting…"); }
// The window goes away at once; the tray icon stays until the service has stopped.
try
{
PrepareToQuit();
window?.Hide();
if (tray is not null) tray.ToolTipText = "Etherlighting Evolved is closing";
}
catch (Exception) { code = 1; }
var confirmed = true;
try { confirmed = service is null || await service.StopAsync(force); }
try { confirmed = service is null || await service.StopAsync(); }
catch (Exception) { confirmed = false; }
if (!confirmed && !force)
{
KeepRunning();
return;
}
await Finish(confirmed && code == 0 ? 0 : 1);
}

void KeepRunning()
{
forceArmed = true;
quitting = false;
quitTask = null;
// After the service has stopped, reconnecting cannot succeed; the window keeps offering to start it again.
if (workspace is not null && service is { Running: true })
{
workspace.Closing = false;
workspace.ConnectFrames();
}
ShowWindow();
window?.Shell.Notify("EE could not confirm that the switch lighting was returned. Choose Quit again to close anyway; " +
"EE recovers the lighting the next time it connects.", true);
}

async Task Finish(int code)
{
try { window?.Shell.Studio.SaveUiState(window.Shell.PageName); }
Expand Down
25 changes: 10 additions & 15 deletions desktop/EtherlightingEvolved.Native/ServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,14 @@ static async Task Drain(Process ended)
/// <summary>
/// Asks the gateway to close the workspace, which returns the switch lighting, and waits for the service to
/// exit. True only when that is confirmed. Concurrent callers share one close request.
/// <para>Without <paramref name="force"/> an unconfirmed close leaves the service running, so the user can try
/// again. With it (a second Quit, the installer, Windows sign-out) the gateway first gets its own cleanup
/// attempts after its input closes, and is ended only if it still has not exited; the unconfirmed exit is then
/// recorded so the next start recovers the lighting.</para>
/// <para>When the close is not confirmed, for example because the switch is unreachable, the service is ended
/// anyway and the unconfirmed exit is recorded, so the next start recovers the lighting.</para>
/// </summary>
public async Task<bool> StopAsync(bool force = true)
public async Task<bool> StopAsync()
{
Task<bool> close;
lock (stopLock) close = stopping ??= Task.Run(CloseAsync);
if (await close.ConfigureAwait(false)) return true;
if (!force)
{
// Let a later request ask the gateway again.
lock (stopLock) if (stopping == close) stopping = null;
return false;
}
Task<bool> ending;
lock (stopLock) ending = forcing ??= Task.Run(EndAsync);
return await ending.ConfigureAwait(false);
Expand Down Expand Up @@ -243,14 +235,17 @@ async Task<bool> EndAsync()
{
var running = process;
if (running is null) return true;
// Closing the gateway's input starts its own cleanup: up to four attempts five seconds apart.
try { running.StandardInput.Close(); } catch (Exception) { }
// "exit" tells the gateway that this close was already tried. It makes no further attempts: it closes
// private and LAN access, tells the engine to quit and exits within ten seconds. Closing its input without
// "exit" would start the cleanup it runs after a desktop crash, four attempts five seconds apart.
try
{
await running.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(25)).ConfigureAwait(false);
if (running.ExitCode == 0) return true;
await running.StandardInput.WriteLineAsync("exit").ConfigureAwait(false);
running.StandardInput.Close();
}
catch (Exception) { }
try { await running.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(15)).ConfigureAwait(false); }
catch (Exception) { }
Kill();
if (Address is not null) Locations.RecordUnconfirmedExit(dataDirectory);
return false;
Expand Down
11 changes: 9 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,13 @@ removing the rule.
```

`connected` can become false while logged in, for example when the switch reboots. EE reconnects automatically,
and `error` holds the last connection error until it does.
and `error` holds the last connection error until it does. Connection errors are retried for as long as EE runs, with
delays that grow to one minute. For 10 minutes after a lost connection, a switch that answers but does not yet offer
its lighting is treated as still starting and retried too. When the connection was lost before EE could confirm its
lighting, EE runs the same checks as [`POST /api/session/recover`](#post-apisessionrecover) on its own when the switch
answers, and resumes the lighting only if it was running before. A rejected login or SSH key, a changed lighting
owner, or capabilities that are still missing after 10 minutes stop the automatic attempts and set
`reconnect_required`.

`pending_scene_choice` is set while a new connection waits for the user to choose its lighting (see
[`POST /api/session/scene-choice`](#post-apisessionscene-choice)), otherwise `null`. `handoff_state` is `none`,
Expand Down Expand Up @@ -1272,7 +1278,8 @@ automatic requests until an explicit user action. Normal successful topology ref
`switch_health` holds the switch's memory, temperature and storage readings, `health_sample_age_ms` their age and
`health_error` is `null`, `unavailable` or `invalid_response`.
- `reconnect_required` is true when an interrupted connection needs
[`POST /api/session/recover`](#post-apisessionrecover) before lighting resumes.
[`POST /api/session/recover`](#post-apisessionrecover) before lighting resumes. It stays false while EE retries
the connection and recovers the lighting on its own.
- `hardware_profile` describes the detected model and its physical port layout, or `null`.
- The state also carries `schedule_clock` ([WebSocket](#websocket-apiws)), `identify`
([Port identification](#port-identification)), `telemetry` and `poe_summary`
Expand Down
Loading
Loading