fix: extract mutable eval state into EvalContext — fixes #90, #93#103
Open
matanzwix wants to merge 2 commits intodashjoin:mainfrom
Open
fix: extract mutable eval state into EvalContext — fixes #90, #93#103matanzwix wants to merge 2 commits intodashjoin:mainfrom
matanzwix wants to merge 2 commits intodashjoin:mainfrom
Conversation
…n#90, dashjoin#93) Replace the broken static ThreadLocal<Jsonata> with a lightweight EvalContext value object on a single static ThreadLocal. The Jsonata instance is now immutable after construction and freely shareable across threads with no cloning.
This was referenced Mar 28, 2026
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
Two related thread-safety bugs make it unsafe to cache and reuse parsed
Jsonatainstances:Issue #93 — Bindings leak across instances on the same thread
The constructor sets
current.set(this)on a singlestatic ThreadLocal<Jsonata>. When a second instance is constructed on the same thread, it overwrites the first. Subsequent evaluations of the first instance can silently use the second instance's per-thread evaluator state, causing environment bindings to leak across unrelated expressions.Issue #90 —
$eval()returnsnullfor nested/deep contexts_evaluate()stores evaluation state such asinputandenvironmentas mutable instance fields during recursive evaluation. When$eval()reads that state throughJsonata.current.get(), it can observe whatever the most recent recursive_evaluate()call wrote, rather than the correct calling context for that$eval()invocation. This causes cases like$eval($.funcs.func)to returnnullinstead of the expected result.Both bugs stem from the same root cause: per-evaluation mutable state stored on the shared
Jsonatainstance and accessed through a single staticThreadLocal<Jsonata>.Solution
Extract all per-evaluation mutable state into a lightweight
EvalContextvalue object stored in a singlestatic ThreadLocal<EvalContext>.This separates:
JsonataAs a result,
Jsonatano longer stores per-evaluation mutable state, and parsed expressions can be safely cached and reused across threads after setup.What changed
Jsonata.javastatic ThreadLocal<Jsonata> currentstatic ThreadLocal<EvalContext> evalContextJsonata(input,timestamp, current eval state)EvalContextgetPerThreadInstance()+ clone per threadJsonata(Jsonata other)current.set(this)_evaluate()overwrites mutable state onthis_evaluate()saves/restores state onEvalContextintry/finallyevaluate(Object, Frame)does not manage a dedicated execution contextevaluate(Object, Frame)creates/restoresEvalContextFunctions.javaJsonata.current.get()call sites now read fromJsonata.evalContext.get()$eval(),$now(),$millis(), and lambda application now useEvalContextfuncApply()no longer performs redundant repeatedThreadLocal.get()callsDesign