[runtime][java] Make embedded Python interpreters thread-confined - #1092
Open
wenjin272 wants to merge 3 commits into
Open
[runtime][java] Make embedded Python interpreters thread-confined#1092wenjin272 wants to merge 3 commits into
wenjin272 wants to merge 3 commits into
Conversation
Create an interpreter lazily for each caller thread and keep Python invocation and result conversion on the same interpreter. Preserve reentrant callbacks and add deterministic concurrent cross-language ChatModel E2E coverage for Java and Python agents. Generated-by: Codex 0.144.5 (GPT-5) Co-authored-by: Codex <codex@openai.com>
wenjin272
marked this pull request as draft
September 3, 2026 11:16
Keep managed Java async workers on thread-local interpreters while routing Python-created and other unmanaged callers through bounded, affinity-preserving callback lanes. Close every interpreter on its owning worker and extend cross-language concurrency coverage for reverse callbacks. Generated-by: Codex 0.144.5 (GPT-5) Co-authored-by: Codex <codex@openai.com>
Extract Python ChatMessage fields before crossing into Java so message conversion no longer re-enters Python through the interpreter manager. Keep the concurrent E2E focused on supported overlapping calls and document the nested callback thread limitation. Generated-by: Codex 0.144.5 (GPT-5) Co-authored-by: Codex <codex@openai.com>
wenjin272
force-pushed
the
codex/fix-python-interpreter-thread-safety
branch
from
September 3, 2026 13:50
61d1329 to
2db48c3
Compare
wenjin272
marked this pull request as ready for review
September 3, 2026 14:58
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.
Linked issue: Closes #1087
Purpose of change
Concurrent cross-language calls no longer enter one embedded
PythonInterpreterfrom multiple threads, avoiding the native JVM crashes observed in async ChatModel and Mem0 paths without globally serializing calls.Runtime flow
PythonInterpreterManager.Key decisions
num-async-threads. Managed Java workers can safely own an interpreter, while creating one on a CPython-created thread can attach a second Python thread state to that native thread; only the latter calls are forwarded.num-async-threads; threads and interpreters remain lazy.Behavioral Semantics
Interaction decisions
Behavioral contracts
PythonInterpreter.Failure behavior
Tests
Coverage targets the native-crash risks: concurrent interpreter entry, Python-created-thread re-entry, opaque-object affinity, and shutdown. The prerequisite-gated Mem0 E2E was also verified manually with its external dependencies available.
Not verified: none identified for the changed behavior.
Implementation invariants and supporting evidence
AsyncExecutorThreadFactoryexecution and removed after its exit cleanup.API
No public API or configuration key is added. Existing callers require no source changes.
num-async-threadsnow also bounds the per-subtask callback lanes; unused lanes create neither threads nor interpreters. Java-only and no-Python plans retain their existing execution behavior, apart from stricter async-worker shutdown waiting.Documentation
doc-neededdoc-not-neededdoc-includedWas this patch authored or co-authored using generative AI tooling?
Generated-by: Codex 0.144.5 (GPT-5)