fix(focas): recycle the device handle under ConnectGate and drain it first (R07:L6)
The handle-recycle loop disposed the device client with no gate. It could null a client a concurrent EnsureConnectedAsync had just installed (orphaning a live two-socket session), or dispose one a poll was mid-read on (ObjectDisposedException -> BadCommunicationError + Degraded on every recycle interval). The recycle now takes the device's ConnectGate, detaches the client, and disposes it via SynchronizedFocasClient.DisposeWhenIdleAsync, which waits (bounded by the call timeout plus slack) for ops that already hold the client. The gate stays held through the drain, so no fresh session opens beside the old one. SynchronizedFocasClient no longer disposes its call gate. Releasing a disposed semaphore threw out of reads that had otherwise completed. Ops that reach a disposed client now get ObjectDisposedException and are never run on the dead socket. Refs #740 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -1779,6 +1779,13 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How long a handle recycle waits for in-flight ops on the detached client: one call timeout (each op is
|
||||
/// bounded by it) plus slack, so a recycle never kills a read that would have completed.
|
||||
/// </summary>
|
||||
private TimeSpan RecycleDrainBudget =>
|
||||
(_options.Timeout > TimeSpan.Zero ? _options.Timeout : TimeSpan.FromSeconds(2)) + TimeSpan.FromMilliseconds(500);
|
||||
|
||||
private async Task RecycleLoopAsync(DeviceState state, CancellationToken ct)
|
||||
{
|
||||
while (!ct.IsCancellationRequested)
|
||||
@@ -1788,14 +1795,31 @@ public sealed class FocasDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
|
||||
// Close the current handle — the next Read / Write / Probe call triggers
|
||||
// EnsureConnectedAsync, which reopens a fresh one. We don't block here on
|
||||
// reconnect because the goal is just to release the FWLIB handle slot; a
|
||||
// readable tick one probe cycle later is an acceptable cost.
|
||||
try { state.DisposeClient(); }
|
||||
// reconnect because the goal is just to release the FWLIB handle slot.
|
||||
//
|
||||
// Under the device's ConnectGate, exactly like EnsureConnectedAsync's install: unserialised, the
|
||||
// recycle could null a client a concurrent connect had just installed (orphaning a live two-socket
|
||||
// session) or dispose one a poll was mid-read on (BadCommunicationError + Degraded every interval).
|
||||
// The gate is held through the drain so no fresh session opens beside the old one — the CNC's
|
||||
// handle pool is small — and the detached client is disposed only once the ops already holding it
|
||||
// have finished (bounded by the call timeout).
|
||||
try { await state.ConnectGate.WaitAsync(ct).ConfigureAwait(false); }
|
||||
catch (OperationCanceledException) { break; }
|
||||
try
|
||||
{
|
||||
var old = state.Client;
|
||||
state.Client = null;
|
||||
if (old is SynchronizedFocasClient sync)
|
||||
await sync.DisposeWhenIdleAsync(RecycleDrainBudget).ConfigureAwait(false);
|
||||
else
|
||||
old?.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
/* already disposed or race — next EnsureConnected recovers */
|
||||
_logger.LogDebug(ex, "FOCAS handle-recycle dispose failed for {Host}", state.Options.HostAddress);
|
||||
}
|
||||
finally { state.ConnectGate.Release(); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user