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
+15 -1
View File
@@ -239,6 +239,19 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
`Scripting:MaxAllocBytesPerRun` (64 MB) is quarantined. This is sampled after the
run returns, so a run that OOMs the process first is not stopped — that needs an
out-of-process runner (→ #750).
- **A returned sequence is materialised inside the run, bounded (#1059).** A script can return an
iterator (`yield`) or an unfinished LINQ query: nothing has run when `Run` returns, so the host used
to enumerate it on its own thread — outside the timeout and the allocation sample, with no element
bound. The guard now enumerates a lazy sequence into a typed array (`double[]` for an
`IEnumerable<double>`) on the run's own thread, so the work is charged to the timeout, the orphan slot
and the allocation budget, and refuses one past `Scripting:MaxResultElements` (default 100 000; also
caps a returned array / collection) with a failed run (Bad, script log) — not a timeout strike. A lazy
sequence whose element type is `object`, an interface or another collection is refused: the host
copies and compares a returned array recursively, so an open-slot array can nest or contain itself.
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)".
- **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
@@ -254,7 +267,7 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
handler that throws on every frame cannot overflow the stack while unwinding
(lmxopcua#971 review); the editor does not show it and does not flag
recursion, and bounded recursion still works.
- **Configuration keys and their ranges.** All four knobs are node-wide (the
- **Configuration keys and their ranges.** The numeric knobs are node-wide (the
`Scripting` appsettings section, shared by virtual tags, Calculation tags and
scripted alarms) and are clamped when bound, with one startup Warning per
clamped value:
@@ -263,6 +276,7 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
|---|---|---|
| `Scripting:RunTimeout` | `00:00:02` (2 s) | zero or negative becomes the default; above `00:01:00` becomes 60 s. The per-run budget of virtual tags and scripted-alarm predicates (lmxopcua#779 — alarms used to fall back to 250 ms). Calculation tags keep their per-driver `runTimeoutMs`. |
| `Scripting:MaxAllocBytesPerRun` | `67108864` (64 MB) | `0` (or negative) disables the budget; a positive value below `1048576` (1 MB) becomes 1 MB |
| `Scripting:MaxResultElements` | `100000` | most elements a returned array / collection / materialised sequence may hold (#1059); zero or negative becomes the default |
| `Scripting:QuarantineAfterConsecutiveTimeouts` | `3` | `1`–`1000` |
| `Scripting:StuckOrphanQuarantineFactor` | `10` | `0` (or negative) disables the rule; above `1000` becomes `1000`; `NaN` becomes the default. In-process runner only (lmxopcua#780 P4) |