fix(scripting): #1059 materialise a returned lazy sequence inside the guarded run with an element cap (Scripting:MaxResultElements); document the in-process sandbox boundary

This commit is contained in:
Joseph Doherty
2026-12-03 15:12:37 -05:00
parent 8225aaa18c
commit f3f24f2984
8 changed files with 289 additions and 3 deletions
@@ -28,6 +28,9 @@ public sealed class ScriptingOptions
/// <summary>Default <see cref="MaxAllocBytesPerRun"/>: 64 MB.</summary>
public const long DefaultMaxAllocBytesPerRun = 64L * 1024 * 1024;
/// <summary>Default <see cref="MaxResultElements"/>: 100 000.</summary>
public const int DefaultMaxResultElements = 100_000;
/// <summary>Default <see cref="QuarantineAfterConsecutiveTimeouts"/>: 3.</summary>
public const int DefaultQuarantineAfterConsecutiveTimeouts = 3;
@@ -53,6 +56,15 @@ public sealed class ScriptingOptions
/// </summary>
public long MaxAllocBytesPerRun { get; set; } = DefaultMaxAllocBytesPerRun;
/// <summary>
/// The most elements a script's returned array / collection / sequence may hold (lmxopcua#1059). A lazy sequence
/// (iterator, unfinished LINQ query) the script returns is enumerated into an array INSIDE the guarded run — so the
/// enumeration is charged to the run's timeout and allocation budget instead of happening unbounded on the
/// host's publish path — and cut off with a <see cref="ScriptResultException"/> past this count. Zero or negative
/// becomes the default. Default 100 000.
/// </summary>
public int MaxResultElements { get; set; } = DefaultMaxResultElements;
/// <summary>
/// Consecutive timeouts after which the script is quarantined: no further runs are dispatched, every
/// evaluation reports Bad, and the quarantine lifts only on a source change, a redeploy or an explicit
@@ -174,6 +186,13 @@ public sealed class ScriptingOptions
alloc = MinMaxAllocBytesPerRun;
}
var maxResultElements = MaxResultElements;
if (maxResultElements <= 0)
{
notes.Add($"{SectionName}:{nameof(MaxResultElements)} {maxResultElements} is not positive; using the default {DefaultMaxResultElements}.");
maxResultElements = DefaultMaxResultElements;
}
var runTimeout = RunTimeout;
if (runTimeout <= TimeSpan.Zero)
{
@@ -196,6 +215,7 @@ public sealed class ScriptingOptions
{
RunTimeout = runTimeout,
MaxAllocBytesPerRun = alloc,
MaxResultElements = maxResultElements,
QuarantineAfterConsecutiveTimeouts = threshold,
StuckOrphanQuarantineFactor = factor,
Runner = Runner,