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:
@@ -251,7 +251,9 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
|
||||
Arrays, lists, strings and scalars are unchanged. **Still in-process-only limits** (the boundary is
|
||||
`Scripting:Runner=OutOfProcess`): memory exhaustion inside a run (the sample is after the fact), a
|
||||
non-terminating loop (an orphan thread, then quarantine) and the same two inside an enumerated
|
||||
sequence. See `docs/VirtualTags.md` §"Known resource limits (accepted trade-offs)".
|
||||
sequence. See `docs/VirtualTags.md` §"Known resource limits (accepted trade-offs)". The out-of-process
|
||||
worker applies the same rule with the same cap and the same failure text (#1064), so a script behaves
|
||||
identically under `Scripting:Runner=InProcess` and `OutOfProcess`.
|
||||
- **Out of process (opt-in, lmxopcua#780).** `Scripting:Runner=OutOfProcess` moves the
|
||||
virtual-tag and Calculation seams' compiled runs — and the scripted-alarm predicates,
|
||||
when `Scripting:Runner:Seams` lists `ScriptedAlarm` — into `ScriptWorker` processes, where a
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -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!;
|
||||
|
||||
@@ -80,6 +80,58 @@ public sealed class ParityTests
|
||||
Same(actual.Value, expected.Value, typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#1064: the result rule of #1059 (<see cref="ScriptResultMaterializer"/>) holds in both runners — a lazy
|
||||
/// sequence becomes the same typed array, and an over-cap / open-slot sequence fails with the same reason. Before
|
||||
/// the fix the worker answered every lazy sequence "unsupported type" and shipped an over-cap array whole.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string> ResultCases() => new()
|
||||
{
|
||||
{ "lazy query", "return System.Linq.Enumerable.Range(0, 3).Select(i => i * 1.5);" },
|
||||
{ "iterator", "System.Collections.Generic.IEnumerable<int> F() { for (var i = 0; i < 3; i++) yield return i; } return F();" },
|
||||
{ "endless iterator", "System.Collections.Generic.IEnumerable<double> F() { var i = 0.0; while (true) yield return i++; } return F();" },
|
||||
{ "over-cap lazy", "return System.Linq.Enumerable.Range(0, int.MaxValue).Select(i => (double)i);" },
|
||||
{ "over-cap array", "return new double[5];" },
|
||||
{ "at-cap array", "return new double[4];" },
|
||||
{ "empty lazy", "return System.Linq.Enumerable.Empty<string>();" },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ResultCases))]
|
||||
public async Task Result_materialisation_and_the_element_cap_agree(string name, string source)
|
||||
{
|
||||
const int cap = 4;
|
||||
using var inProcess = new InProcessScriptRunner(new ScriptingOptions { MaxResultElements = cap });
|
||||
var options = Scripts.Options();
|
||||
options.MaxResultElements = cap;
|
||||
await using var outOfProcess = Scripts.Runner(options);
|
||||
var invocation = Scripts.Vt("result-" + name, source);
|
||||
|
||||
var expected = await inProcess.RunAsync(invocation, Ct);
|
||||
var actual = await outOfProcess.RunAsync(invocation, Ct);
|
||||
|
||||
actual.Status.ShouldBe(expected.Status, $"{name}: {actual.Reason} vs {expected.Reason}");
|
||||
actual.Reason.ShouldBe(expected.Reason);
|
||||
Same(actual.Value, expected.Value, name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_worker_holds_a_lazy_result_to_the_configured_cap()
|
||||
{
|
||||
// Positive control first (the cap is not simply "always fails"): 3 elements under a cap of 3 materialise.
|
||||
var options = Scripts.Options();
|
||||
options.MaxResultElements = 3;
|
||||
await using var runner = Scripts.Runner(options);
|
||||
|
||||
var ok = await runner.RunAsync(Scripts.Vt("cap-ok", "return System.Linq.Enumerable.Range(0, 3).Select(i => (double)i);"), Ct);
|
||||
ok.Status.ShouldBe(ScriptRunStatus.Ok, ok.Reason);
|
||||
ok.Value.ShouldBeOfType<double[]>().ShouldBe([0.0, 1.0, 2.0]);
|
||||
|
||||
var over = await runner.RunAsync(Scripts.Vt("cap-over", "return System.Linq.Enumerable.Range(0, 4).Select(i => (double)i);"), Ct);
|
||||
over.Status.ShouldBe(ScriptRunStatus.Failed);
|
||||
over.Reason.ShouldNotBeNull().ShouldContain("more than 3 elements");
|
||||
}
|
||||
|
||||
private static void Same(object? actual, object? expected, string because)
|
||||
{
|
||||
if (expected is null)
|
||||
|
||||
Reference in New Issue
Block a user