Keep the memory limit exact instead of amortizing it in tight loops#2695
Merged
Conversation
The tight-loop constraint amortization (sebastienros#2672) checks the Time, Cancellation and MemoryLimit constraints once every 64 statements. That is sound for a clock or a cancellation token - a check reads external state it does not consume - but allocation is irreversible and unbounded per statement: a single iteration can allocate arbitrarily much (e.g. `s += s` doubling), so a function-body tight loop can overshoot the configured cap, up to a real OutOfMemoryException, before the next 64-statement check fires. v4.12.0 had no while/do-while tight lane and checked memory every statement. Move MemoryLimitConstraint into the exact set so memory-limited engines check it per statement again (as before the tight lane), preserving the limit as a hard-ish bound for sandboxing untrusted code. Time and cancellation stay amortized, so the common timeout-only / unconstrained engine keeps the tight-lane fast path unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XM8rSnn8j66kDCTaP2exNK
lahma
enabled auto-merge (squash)
July 15, 2026 08:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The tight-loop constraint amortization (#2672) checks the built-in
Time,CancellationandMemoryLimitconstraints once every 64 statements. That reasoning is sound for a clock or a cancellation token — a check reads external state it does not consume, so bounded detection latency is equivalent to per-statement checking. But allocation is irreversible and unbounded per statement: a single iteration can allocate arbitrarily much (e.g.s += sdoubling), so a function-body tight loop can overshoot the configuredLimitMemorycap — up to a realOutOfMemoryException— before the next 64-statement check fires.Because #2688 extended the tight lane to
while/do-whileand #2672 keeps it armed under constraints, an engine that relies onLimitMemoryto sandbox untrusted code now enforces a much coarser bound than in 4.12.0, which had no while/do-while tight lane and checked memory every statement.Fix
Move
MemoryLimitConstraintinto the exact partition so memory-limited engines check it per statement again (as before the tight lane existed), preserving the limit as a hard-ish bound.TimeandCancellationstay amortized, so the common case — a timeout-only or unconstrained engine — keeps the tight-lane fast path completely unchanged. Only engines that registerLimitMemoryare affected, and they simply revert to pre-#2672 behavior.Testing
Full
Jint.Testsgreen (3603/3540, net10/net472), including the existingShouldThrowMemoryLimitExceededInsideFunctionLocalTightLoop/ timeout tight-lane tests — the memory limit still fires, now per statement.🤖 Generated with Claude Code
https://claude.ai/code/session_01XM8rSnn8j66kDCTaP2exNK