03ac57d5d6
- 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
71 lines
4.4 KiB
C#
71 lines
4.4 KiB
C#
using System.IO.Pipes;
|
|
using System.Runtime.Versioning;
|
|
using System.Security.AccessControl;
|
|
using System.Security.Principal;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.OutOfProcess;
|
|
|
|
/// <summary>
|
|
/// The ScriptWorker pipe's explicit ACL on Windows (lmxopcua#780 design §6, "an explicit ACL when the worker runs as a
|
|
/// different account"): a protected DACL that admits the host account and — when <c>Scripting:Worker:Identity</c> is
|
|
/// set — the worker identity, each with read/write, and nobody else (no SYSTEM, no Administrators, no Everyone). The
|
|
/// owner is the host token's owner, as .NET's <c>PipeOptions.CurrentUserOnly</c> sets it, so the worker's
|
|
/// <c>CurrentUserOnly</c> client check (pipe owner == its token owner, which a restricted token keeps) still
|
|
/// authenticates the server end.
|
|
/// </summary>
|
|
[SupportedOSPlatform("windows")]
|
|
internal static class WorkerPipeSecurity
|
|
{
|
|
/// <summary>The rights each admitted principal gets: read/write data, attributes and the security descriptor (read).</summary>
|
|
internal const PipeAccessRights GrantedRights = PipeAccessRights.ReadWrite;
|
|
|
|
/// <summary>Builds the pipe ACL for this host process.</summary>
|
|
/// <param name="workerSid">The worker identity's SID string, or null when the worker keeps the host's token.</param>
|
|
/// <returns>The pipe security.</returns>
|
|
public static PipeSecurity ForCurrentHost(string? workerSid)
|
|
{
|
|
using var identity = WindowsIdentity.GetCurrent();
|
|
var owner = identity.Owner ?? identity.User ?? throw new InvalidOperationException("the host token has no owner");
|
|
var user = identity.User ?? owner;
|
|
return Create(owner, user, workerSid is null ? null : new SecurityIdentifier(workerSid));
|
|
}
|
|
|
|
/// <summary>Builds the pipe ACL.</summary>
|
|
/// <param name="owner">The pipe owner (the host token's owner).</param>
|
|
/// <param name="hostUser">The host account (its token's user SID).</param>
|
|
/// <param name="worker">The worker identity, or null.</param>
|
|
/// <returns>The pipe security: protected, allow-only, one ACE for the host account and one for the worker identity.</returns>
|
|
public static PipeSecurity Create(SecurityIdentifier owner, SecurityIdentifier hostUser, SecurityIdentifier? worker)
|
|
{
|
|
var security = new PipeSecurity();
|
|
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
|
|
security.SetOwner(owner);
|
|
// The host account and the worker identity, nothing else: not the owner when it differs (an elevated host's
|
|
// owner is BUILTIN\Administrators — .NET's CurrentUserOnly would grant it; the owner keeps only its implicit
|
|
// READ_CONTROL / WRITE_DAC).
|
|
var principals = new List<SecurityIdentifier> { hostUser };
|
|
if (worker is not null && !principals.Contains(worker)) principals.Add(worker);
|
|
foreach (var principal in principals)
|
|
security.AddAccessRule(new PipeAccessRule(principal, GrantedRights, AccessControlType.Allow));
|
|
return security;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The pipe's buffer quota in each direction (lmxopcua#789). With a quota of 0 an overlapped write stays pending
|
|
/// until the peer has READ the bytes, so a blocking writer (<see cref="ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol.WindowsBlockingPipeStream"/>)
|
|
/// would wait out a full peer wake-up per frame — measured on windev as half the closed-loop throughput. With a
|
|
/// quota a write completes as soon as the bytes are buffered; a peer that stops reading still fills it and blocks
|
|
/// the writer: the host's write-stall check (<c>WorkerConnection.WriteStallLimit</c>) then kills the worker, which
|
|
/// fails the write and releases the blocked thread.
|
|
/// </summary>
|
|
internal const int BufferBytes = 1024 * 1024;
|
|
|
|
/// <summary>Creates the single-instance server end of a worker pipe with <paramref name="security"/>.</summary>
|
|
/// <param name="pipeName">The random pipe name.</param>
|
|
/// <param name="security">The ACL (<see cref="ForCurrentHost"/>).</param>
|
|
/// <returns>The server stream.</returns>
|
|
public static NamedPipeServerStream CreateServer(string pipeName, PipeSecurity security)
|
|
=> NamedPipeServerStreamAcl.Create(pipeName, PipeDirection.InOut, maxNumberOfServerInstances: 1,
|
|
PipeTransmissionMode.Byte, PipeOptions.Asynchronous, inBufferSize: BufferBytes, outBufferSize: BufferBytes, security);
|
|
}
|