fix(scripting): #789 review — reader faults end the worker, write stalls kill it, Cancel off the worker reader
- Host reader: the frame read and its handling are caught separately. Only the read's I/O exceptions mean the pipe ended; anything HandleFrame's inline completion work throws now kills that worker as ProtocolFault (outstanding runs -> worker restarted / BadResourceUnavailable) instead of escaping a dedicated thread (process crash) or being swallowed as a clean end (an InvalidOperationException used to be, leaving the worker unread until a Crash 2 s later). - Worker reader: the same catch-all around HandleFrame (exit ProtocolFault). - Worker: Cancel() runs on the pool, so a script's blocking ctx.CancellationToken callback cannot stop the worker reading its pipe. - Host write-stall check: ScriptWorkerFrameWriter.WriteInProgressFor; the heartbeat monitor kills a worker whose pipe write has been blocked longer than max(10 s, silence limit) as KilledUnresponsive, which fails the write and releases the producer (seam submit, deploy load, Cancel). Fixes the wrong 'heartbeat monitor ends it' comment on the pipe buffer quota. - StopAsync sends Shutdown off the caller so its 1 s budget holds on a full pipe. - WindowsBlockingPipeStream refuses the async surface (Stream's base versions serialise a write behind a waiting read). - Tests: PipeFaultTests (throwing completion callback -> ProtocolFault and a BadResourceUnavailable sibling, shard serves again; a worker that stops reading but heartbeats is killed within the bound and the blocked producer returns, via the Hello.test_stall_reader_on_diag seam); BlockingPipeStreamTests dispose with a write blocked on a full pipe, and with a read and a write both pending; Category=ScriptWorker on the protocol, handshake and pipe test classes. Mutation checks: no stall kill -> stall test red; catch-all without the fault -> callback test red (Crash). Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
@@ -126,7 +126,8 @@ public sealed class ScriptWorkerFrameReader
|
||||
/// <see cref="ScriptWorkerFrameCodec.BlockingStream"/>, so no frame needs the socket engine or the completion port):
|
||||
/// the draining thread blocks while the pipe is full. Only
|
||||
/// one thread drains at a time — every other producer just queues and returns — and a peer that stops reading is
|
||||
/// killed by its supervisor (the host's heartbeat check), which fails the write and releases that thread.</para>
|
||||
/// killed by its supervisor (the host's write-stall check, <see cref="WriteInProgressFor"/>; on the worker side the
|
||||
/// host's silence check), which fails the write and releases that thread.</para>
|
||||
/// <para>A failed write completes the writer and reports the exception once through <c>onFault</c>.</para>
|
||||
/// </remarks>
|
||||
public sealed class ScriptWorkerFrameWriter
|
||||
@@ -141,6 +142,9 @@ public sealed class ScriptWorkerFrameWriter
|
||||
private int _faulted;
|
||||
private long _seq;
|
||||
|
||||
/// <summary><see cref="System.Diagnostics.Stopwatch.GetTimestamp"/> when the write in progress started; 0 = none.</summary>
|
||||
private long _writeStarted;
|
||||
|
||||
/// <summary>Initializes the writer.</summary>
|
||||
/// <param name="stream">The pipe.</param>
|
||||
/// <param name="onFault">Called once when a write fails (or a frame is refused); the writer is then completed.</param>
|
||||
@@ -162,6 +166,14 @@ public sealed class ScriptWorkerFrameWriter
|
||||
/// <summary>The negotiated frame cap.</summary>
|
||||
public int MaxFrameBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// How long the pipe write in progress has been blocked, or null when no write is in progress. A blocking write
|
||||
/// stays in progress while the pipe is full, i.e. while the peer is not reading: the supervisor uses this to end a
|
||||
/// peer that keeps talking but stopped listening (lmxopcua#789), which releases the thread blocked here.
|
||||
/// </summary>
|
||||
public TimeSpan? WriteInProgressFor
|
||||
=> Volatile.Read(ref _writeStarted) is var started and not 0 ? System.Diagnostics.Stopwatch.GetElapsedTime(started) : null;
|
||||
|
||||
/// <summary>Whether <see cref="Complete"/> has been called or a write failed.</summary>
|
||||
public bool IsCompleted => Volatile.Read(ref _completed) != 0;
|
||||
|
||||
@@ -196,8 +208,16 @@ public sealed class ScriptWorkerFrameWriter
|
||||
FillPass();
|
||||
if (_pass.WrittenCount > 0)
|
||||
{
|
||||
if (_synchronous) _stream.Write(_pass.WrittenSpan);
|
||||
else await _stream.WriteAsync(_pass.WrittenMemory).ConfigureAwait(false);
|
||||
Volatile.Write(ref _writeStarted, System.Diagnostics.Stopwatch.GetTimestamp());
|
||||
try
|
||||
{
|
||||
if (_synchronous) _stream.Write(_pass.WrittenSpan);
|
||||
else await _stream.WriteAsync(_pass.WrittenMemory).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Volatile.Write(ref _writeStarted, 0);
|
||||
}
|
||||
_pass.ResetWrittenCount();
|
||||
// Synchronous on purpose: a pipe has no write buffer to flush, and PipeStream does not override
|
||||
// FlushAsync, so the base Stream.FlushAsync would queue a thread-pool work item per pass.
|
||||
|
||||
Reference in New Issue
Block a user