fix(scripting): #1064 the ScriptWorker applies the in-process result rule — a returned lazy sequence is materialised inside the run and held to Scripting:MaxResultElements (Hello.max_result_elements), same value and same failure text in both runners

This commit is contained in:
Joseph Doherty
2026-12-03 16:35:01 -05:00
parent fe4bbbe256
commit a06dc8dd9f
8 changed files with 85 additions and 5 deletions
@@ -764,7 +764,8 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
LoadTimeout: LoadTimeout,
CompileThreads: _compileThreads,
WorkerSid: WorkerSid,
HeartbeatSilenceLimit: _heartbeatSilenceLimit);
HeartbeatSilenceLimit: _heartbeatSilenceLimit,
MaxResultElements: _options.MaxResultElements);
return new WorkerShard(label, isolation,
(onExiting, ct) => WorkerConnection.StartAsync(spec, _log, OnLogs, onExiting, ct),
OnWorkerExit, _log);
@@ -102,6 +102,11 @@ internal sealed record BatchSubmission(string Key, EvalRequest Request, TimeSpan
/// Ask the worker to run the soft clock from each run's start (lmxopcua#827; see <see cref="WorkerConnection.WorkerSoftClock"/>).
/// False (tests only) keeps the pre-#827 host clock, which is also what a worker that does not echo it gets.
/// </param>
/// <param name="MaxResultElements">
/// <c>Scripting:MaxResultElements</c> (lmxopcua#1064): the element cap the worker holds a value script's returned
/// sequence to while materialising it inside the run — the same rule the in-process guard applies (#1059). 0 = the
/// worker's default.
/// </param>
internal sealed record WorkerLaunchSpec(
string Shard,
string WorkerPath,
@@ -117,7 +122,8 @@ internal sealed record WorkerLaunchSpec(
string? WorkerSid = null,
bool WorkerSoftClock = true,
TimeSpan? HeartbeatSilenceLimit = null,
bool TestStallReaderOnDiag = false)
bool TestStallReaderOnDiag = false,
int MaxResultElements = 0)
{
/// <summary>The default compile timeout.</summary>
public static readonly TimeSpan DefaultLoadTimeout = TimeSpan.FromSeconds(60);
@@ -448,6 +454,7 @@ internal sealed class WorkerConnection : IAsyncDisposable
CompileThreads = (uint)Math.Max(0, spec.CompileThreads),
WorkerSoftClock = spec.WorkerSoftClock,
TestStallReaderOnDiag = spec.TestStallReaderOnDiag,
MaxResultElements = (uint)Math.Max(0, spec.MaxResultElements),
};
/// <summary>
@@ -53,6 +53,11 @@ message Hello {
// Test seam (lmxopcua#789): the worker's reader stops reading for good at the first DiagnosticsRequest while its
// heartbeats go on, so the host's write-stall check can be exercised against a real worker. Never set in production.
bool test_stall_reader_on_diag = 12;
// lmxopcua#1064: Scripting:MaxResultElements. A value script's returned sequence is materialised inside the run and
// held to this element cap, exactly as the in-process ScriptRunGuard does (ScriptResultMaterializer, #1059) — so a
// lazy sequence is enumerated in the worker's run (under its deadlines), and an over-cap result fails the script the
// same way in both runners. 0 = the worker's default (ScriptingOptions.DefaultMaxResultElements).
uint32 max_result_elements = 13;
}
message Ready {
@@ -39,8 +39,14 @@ public sealed class ScriptResultException : InvalidOperationException
/// non-terminating loop are still only contained by the guard's after-the-fact allocation sample and its timeout
/// (an orphaned thread). Only <c>Scripting:Runner=OutOfProcess</c> contains those. See docs/VirtualTags.md.
/// </para>
/// <para>
/// The ONE result rule for both runners (lmxopcua#1064): the in-process <see cref="ScriptRunGuard"/> and the
/// out-of-process <c>ScriptWorker</c> both call <see cref="Materialize"/> right after a value script returns,
/// inside the run, with the node's <c>Scripting:MaxResultElements</c> (the worker receives it in its
/// <c>Hello</c>). Public for that reason only.
/// </para>
/// </remarks>
internal static class ScriptResultMaterializer
public static class ScriptResultMaterializer
{
/// <summary>Materialises <paramref name="value"/> as described on the type.</summary>
/// <param name="value">The script's returned value.</param>
@@ -77,6 +77,7 @@ internal sealed class WorkerHost
private int _maxFrameBytes = ScriptWorkerFrameCodec.DefaultMaxFrameBytes;
private int _queueCapacity = 4096;
private TimeSpan _hardGrace = TimeSpan.FromSeconds(1);
private int _maxResultElements = ScriptingOptions.DefaultMaxResultElements;
private TimeSpan _heartbeatInterval = TimeSpan.FromSeconds(1);
/// <summary>
@@ -160,6 +161,7 @@ internal sealed class WorkerHost
if (hello.HardGraceMs > 0) _hardGrace = TimeSpan.FromMilliseconds(hello.HardGraceMs);
if (hello.HeartbeatIntervalMs > 0) _heartbeatInterval = TimeSpan.FromMilliseconds(hello.HeartbeatIntervalMs);
if (hello.CompileThreads > 0) _compileThreads = (int)Math.Min(hello.CompileThreads, 64);
if (hello.MaxResultElements > 0) _maxResultElements = (int)Math.Min(hello.MaxResultElements, int.MaxValue);
_workerSoftClock = hello.WorkerSoftClock;
_testStallReaderOnDiag = hello.TestStallReaderOnDiag;
// lmxopcua#1014: no PE-cache key or directory any more — the host verifies, compiles and signs, and sends a
@@ -458,6 +460,11 @@ internal sealed class WorkerHost
try
{
object? value = script.Run(inputs, logger, clock, cts.Token, out var isPredicate);
// lmxopcua#1064: the in-process guard's result rule (#1059), applied here too and still inside the run —
// this eval is registered with the watchdog and its allocation sample is still open — so a returned lazy
// sequence is enumerated under the run's deadlines and held to Scripting:MaxResultElements, and an
// over-cap or open-slot sequence fails the script with the same message in both runners.
if (!isPredicate) value = ScriptResultMaterializer.Materialize(value, _maxResultElements);
if (isPredicate)
{
result.Predicate = (bool)value!;