fix(scripting): #971 review — guard script catch/finally handlers so unwinding cannot overflow the stack
Sibling hunt for the #779 stack guard: a catch or finally runs as a funclet on top of the frames being unwound, so a handler that throws starts a nested dispatch deeper still (~6 KB/level). 'catch { throw; }' on every frame killed the host while unwinding from the guard's own exception, and 300 frames that each rethrow (or whose finally throws) killed it with no guard involvement. The stack-guard rewrite now gives every script catch the filter 'when (ScriptStackGuard.CanHandle() && (filter))' and wraps every finally body in 'if (ScriptStackGuard.CanHandle())'. Once the stack is low, script handlers are skipped and the in-flight exception reaches the host in one dispatch. Verified on 1.5 MB / 1 MB / 512 KB / 256 KB threads. Seven new child-process scenarios in ScriptRecursionContainmentTests (all crashed before), plus semantics-preservation rows for filters, declaration-less catch, pattern variables and finally on both paths. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,15 @@ public sealed class ScriptStackGuardInjectionTests
|
||||
// Static lambda and a lambda capturing ctx.
|
||||
[InlineData("System.Func<int, int> f = static x => x - 1; return f(9);", 8)]
|
||||
[InlineData("System.Func<string, bool> known = p => ctx.GetTag(p).StatusCode == 0u; return known(\"missing\") ? 1 : 0;", 0)]
|
||||
// lmxopcua#971 review: guarded handlers keep their meaning on a healthy stack — an existing filter still
|
||||
// decides, a declaration-less catch still catches, pattern variables from the filter stay in scope, and a
|
||||
// finally still runs on both the normal and the exceptional path.
|
||||
[InlineData("try { throw new InvalidOperationException(\"a\"); } catch (InvalidOperationException e) when (e.Message == \"b\") { return 1; } catch (Exception) { return 2; }", 2)]
|
||||
[InlineData("try { throw new InvalidOperationException(); } catch { return 4; }", 4)]
|
||||
[InlineData("try { throw new ArgumentException(\"x\"); } catch (Exception e) when (e is ArgumentException a && a.Message == \"x\") { return a.Message.Length + 2; }", 3)]
|
||||
[InlineData("var n = 0; try { n = 1; } finally { n += 10; } return n;", 11)]
|
||||
[InlineData("var n = 0; try { try { throw new Exception(); } finally { n = 5; } } catch { n += 1; } return n;", 6)]
|
||||
[InlineData("int Rec(int d) { try { if (d >= 50) throw new InvalidOperationException(); return Rec(d + 1); } catch (InvalidOperationException) { throw; } } try { return Rec(0); } catch (InvalidOperationException) { return 50; }", 50)]
|
||||
public void Rewritten_lambdas_and_local_functions_keep_their_meaning(string source, int expected) =>
|
||||
Run(source).ShouldBe(expected);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user