Skip to content

Step Functions: Lazy Initialization of JVM for JSONata Evaluation #12369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ def eval_jsonata(self, jsonata_expression: JSONataExpression) -> Any:
raise JSONataException("UNKNOWN", str(ex))


# Final reference to the java evaluation function.
_eval_jsonata: Final[Callable[[JSONataExpression], Any]] = _JSONataJVMBridge.get().eval_jsonata
# Lazy initialization of the `eval_jsonata` function pointer.
# This ensures the JVM is only started when JSONata functionality is needed.
_eval_jsonata: Optional[Callable[[JSONataExpression], Any]] = None


def eval_jsonata_expression(jsonata_expression: JSONataExpression) -> Any:
global _eval_jsonata
if _eval_jsonata is None:
# Initialize _eval_jsonata only when invoked for the first time using the Singleton pattern.
_eval_jsonata = _JSONataJVMBridge.get().eval_jsonata
Comment on lines +95 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not be locking before assigning this global variable? I'm concerned about a race condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The singleton class pattern and the installer are thread-safe, and the installation can only occur once, so we should be okay to keep this as it

return _eval_jsonata(jsonata_expression)


Expand Down
Loading