review(Driver.FOCAS.Cli): FlushLogging() in finally + fix misleading detach comment

Re-review at 7286d320. -006 (Low): FlushLogging() in all command finally blocks + tests.
-007: rewrite the inaccurate handler-detach comment (cleanup is via await using disposal).
This commit is contained in:
Joseph Doherty
2026-06-19 12:08:45 -04:00
parent b50fd6c34a
commit 754c5a3684
6 changed files with 191 additions and 25 deletions
@@ -43,16 +43,26 @@ public sealed class ProbeCommand : FocasCommandBase
// already invokes ShutdownAsync, so a redundant explicit ShutdownAsync(CancellationToken.None)
// in a finally block ran shutdown twice. The await-using on the next line is enough.
await using var driver = new FocasDriver(options, DriverInstanceId);
await driver.InitializeAsync("{}", ct);
var snapshot = await driver.ReadAsync(["__probe"], ct);
var health = driver.GetHealth();
try
{
await driver.InitializeAsync("{}", ct);
var snapshot = await driver.ReadAsync(["__probe"], ct);
var health = driver.GetHealth();
await console.Output.WriteLineAsync($"CNC: {CncHost}:{CncPort}");
await console.Output.WriteLineAsync($"Series: {Series}");
await console.Output.WriteLineAsync($"Health: {health.State}");
if (health.LastError is { } err)
await console.Output.WriteLineAsync($"Last error: {err}");
await console.Output.WriteLineAsync();
await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
await console.Output.WriteLineAsync($"CNC: {CncHost}:{CncPort}");
await console.Output.WriteLineAsync($"Series: {Series}");
await console.Output.WriteLineAsync($"Health: {health.State}");
if (health.LastError is { } err)
await console.Output.WriteLineAsync($"Last error: {err}");
await console.Output.WriteLineAsync();
await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
}
finally
{
// Driver.FOCAS.Cli-006: flush Serilog before process exit so buffered log output
// emitted during driver shutdown is not silently dropped (matching DriverCommandBase
// docs and every sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
FlushLogging();
}
}
}
@@ -44,9 +44,19 @@ public sealed class ReadCommand : FocasCommandBase
// already invokes ShutdownAsync, so a redundant explicit ShutdownAsync(CancellationToken.None)
// in a finally block ran shutdown twice. The await-using on the next line is enough.
await using var driver = new FocasDriver(options, DriverInstanceId);
await driver.InitializeAsync("{}", ct);
var snapshot = await driver.ReadAsync([tagName], ct);
await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
try
{
await driver.InitializeAsync("{}", ct);
var snapshot = await driver.ReadAsync([tagName], ct);
await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
}
finally
{
// Driver.FOCAS.Cli-006: flush Serilog before process exit so buffered log output
// emitted during driver shutdown is not silently dropped (matching DriverCommandBase
// docs and every sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
FlushLogging();
}
}
/// <summary>Constructs a tag name from address and data type.</summary>
@@ -110,16 +110,19 @@ public sealed class SubscribeCommand : FocasCommandBase
{
if (handle is not null)
{
// Driver.FOCAS.Cli-002: detach the OnDataChange handler before unsubscribe +
// disposal for symmetry with the handle teardown, so a future refactor that
// reuses the driver after the subscribe verb returns wouldn't leak a
// dangling subscription.
// (Single anonymous handler instance is captured implicitly by `await using`
// disposing the driver immediately afterwards; the unsubscribe + dispose
// sequence is what really cleans up here.)
// Driver.FOCAS.Cli-002: stop the subscription before disposal — UnsubscribeAsync
// halts the poll-group ticker so no further OnDataChange events fire. The
// anonymous handler is never explicitly removed via -=; instead, driver disposal
// (via `await using` immediately after this finally) tears down the PollGroupEngine,
// which is the real cleanup. The unsubscribe here is a best-effort subscription
// lifecycle step so the CNC-side session is cleanly released before teardown.
try { await driver.UnsubscribeAsync(handle, CancellationToken.None); }
catch { /* teardown best-effort */ }
}
// Driver.FOCAS.Cli-006: flush Serilog before process exit so buffered log output
// emitted during driver shutdown is not silently dropped (matching DriverCommandBase
// docs and every sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
FlushLogging();
}
}
}
@@ -54,9 +54,19 @@ public sealed class WriteCommand : FocasCommandBase
// already invokes ShutdownAsync, so a redundant explicit ShutdownAsync(CancellationToken.None)
// in a finally block ran shutdown twice. The await-using on the next line is enough.
await using var driver = new FocasDriver(options, DriverInstanceId);
await driver.InitializeAsync("{}", ct);
var results = await driver.WriteAsync([new WriteRequest(tagName, parsed)], ct);
await console.Output.WriteLineAsync(SnapshotFormatter.FormatWrite(Address, results[0]));
try
{
await driver.InitializeAsync("{}", ct);
var results = await driver.WriteAsync([new WriteRequest(tagName, parsed)], ct);
await console.Output.WriteLineAsync(SnapshotFormatter.FormatWrite(Address, results[0]));
}
finally
{
// Driver.FOCAS.Cli-006: flush Serilog before process exit so buffered log output
// emitted during driver shutdown is not silently dropped (matching DriverCommandBase
// docs and every sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
FlushLogging();
}
}
/// <summary>Parse <c>--value</c> per <see cref="FocasDataType"/>, invariant culture throughout.</summary>