fix(js): convert PolyglotException to JsEvaluationException at the engine boundary#3256
Draft
amanjeetsingh150 wants to merge 1 commit intomainfrom
Draft
fix(js): convert PolyglotException to JsEvaluationException at the engine boundary#3256amanjeetsingh150 wants to merge 1 commit intomainfrom
amanjeetsingh150 wants to merge 1 commit intomainfrom
Conversation
…gine boundary Catch PolyglotException at every context.eval() call site in GraalJsEngine (the IIFE evaluation path and the bootstrap script) and rethrow as JsEvaluationException(JsScriptError). The new JsScriptError is a plain Kotlin data class — message, causeMessage, sourceClass, stackFrames (List<String>), isHostException, isGuestException — resolved while the engine is still open and entirely detached from polyglot internals. Why: PolyglotException's polyglotStackTrace property exposes live StackFrame objects whose getLanguage() calls back into the originating PolyglotContext. If a caller retains the exception on a model that is later serialized by a reflective walker (Jackson, etc.) after the engine has closed, that callback hits ShouldNotReachHere and crashes the JVM. Stripping the exception at the engine boundary closes that class of failure for any downstream consumer of JsEngine.evaluateScript. Behaviour change: callers that previously caught PolyglotException from JsEngine.evaluateScript now catch JsEvaluationException. Broad catches (Throwable / Exception) are unaffected. Tests: new round-trip test asserts JsScriptError serialises through Jackson without touching polyglot fields, plus shape assertions on the wrapped error. The existing sandboxing test is updated to expect the new exception type.
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
GraalJsEnginenow catchesPolyglotExceptionat everycontext.eval()call site (the IIFE evaluation path and the bootstrap script) and rethrows a plainJsEvaluationException(JsScriptError). Live polyglot internals never escape the engine.Why
PolyglotExceptionexposes apolyglotStackTrace: List<StackFrame>property whosegetLanguage()callback re-enters the originatingPolyglotContext. If a caller retains the exception on a model that's later walked by a reflective serializer (Jackson and friends) after the engine has closed, that callback hitsShouldNotReachHereand the JVM dies.Stripping the exception to plain Strings at the boundary is a one-place fix that closes this class of failure for every downstream consumer of
JsEngine.evaluateScript— without anyone having to remember to do safe-extraction at every call site.Shape of the new types
Resolved entirely while the engine is still open. After the constructor returns, the original
PolyglotExceptioncan be released and the engine closed without affecting the wrapper.Behaviour change for library users
Callers that previously caught
PolyglotExceptionfromJsEngine.evaluateScriptnow catchJsEvaluationException. Broad catches (Throwable/Exception) are unaffected — the exception still propagates as aRuntimeException. The CLI's own error formatter (ErrorViewUtils) is not impacted; it falls through toe.stackTraceToString()either way.Migration
Verify
./gradlew testpasses across all modules.GraalJsEngineTestcover the wrapping behaviour, JsScriptError shape, and Jackson round-trip with the engine closed.sandboxing workstest updated to expect the new exception type.