fix: self-referential thunks produce clean error instead of StackOverflowError#1020
Open
He-Pin wants to merge 1 commit into
Open
fix: self-referential thunks produce clean error instead of StackOverflowError#1020He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
…flowError Motivation: `local x = x; x` caused an uncontrolled java.lang.StackOverflowError with a raw Java stack trace. C++ jsonnet, go-jsonnet, and jrsonnet all produce clean, controlled error messages for self-referential thunks. Modification: - Val.scala: Add `evaluating` boolean flag to LazyExpr. When value is first requested, set the flag to true. If value is requested again while the flag is true, it's a cycle — throw a clean error: "Infinite recursion detected (self-referential thunk)". Result: Self-referential thunks now produce a clean sjsonnet.Error instead of crashing the JVM. Cross-implementation comparison: | Expression | cpp-jsonnet | go-jsonnet | jrsonnet | sjsonnet (before) | sjsonnet (after) | |---|---|---|---|---|---| | local x = x; x | "max stack frames" | "max stack frames" | "infinite recursion" | StackOverflowError 💥 | "Infinite recursion detected" ✅ | | local f() = f(); f() | "max stack frames" | "max stack frames" | "stack overflow" | "Max stack frames" ✅ | "Max stack frames" ✅ | | local x = 1+2; x | 3 | 3 | 3 | 3 ✅ | 3 ✅ |
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.
Summary
local x = x; xcaused an uncontrolledjava.lang.StackOverflowErrorwith raw Java stack traceevaluatingboolean flag toLazyExprfor cycle detection — same approach as jrsonnetCross-implementation comparison
local x = x; xlocal f() = f(); f()local x = 1+2; x3333✅3✅Root cause
visitValidId→LazyExpr.value→visitExpr→visitValidIdforms an infinite loop with no stack depth check in the path. ThecheckStackDepthguard exists in function call and binary op visitors but was missing from the thunk evaluation path.Test plan
./mill sjsonnet.jvm[3.3.7].test— all suites greenerror.self_referential_thunk.jsonnet