[runtime][python] Manage Pemja references across resource bridges - #1058
Draft
joeyutong wants to merge 1 commit into
Draft
[runtime][python] Manage Pemja references across resource bridges#1058joeyutong wants to merge 1 commit into
joeyutong wants to merge 1 commit into
Conversation
Introduce an operation-scoped ownership abstraction for Pemja references and apply it consistently across chat, embedding, vector-store, MCP, Mem0, and Python tool bridges. Materialize values that outlive bridge calls, transfer long-lived references explicitly, and roll back partially initialized resources before cache registration. Co-Authored-By: Codex <[email protected]> AI-Model: gpt-5 Generated-by: Codex (GPT-5) AI-Contributed/Feature: 1012/1012 AI-Contributed/UT: 742/742
joeyutong
force-pushed
the
codex/fix-pemja-resource-ownership
branch
from
August 30, 2026 10:27
dc34f88 to
7088146
Compare
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: #1057
Purpose of change
Pemja values crossing the Java/Python bridge may carry native Python references. The current resource paths do not share one ownership rule, so temporary values, nested results, long-lived resources, and partial initialization failures can retain references differently.
This change introduces
PythonObjectScopeas the common ownership abstraction:PyObjecthandles reachable through maps, iterables, and object arrays, deduplicates them by identity, and releases them in reverse order;release(...)makes ownership transfer to a long-lived wrapper explicit;The abstraction is applied consistently to chat and embedding models, vector stores, MCP servers/tools/prompts, Mem0 call-scoped values, Python-backed
FunctionToolcalls, and Java/Python resource-cache initialization.This complements #944, which owns executor-lifetime roots and the shutdown close chain, and #1049, which owns per-Action execution values. This PR covers the remaining operation-level resource bridges and initialization rollback paths.
Tests
mvn -pl runtime -am -DskipITs testPythonResourceProviderTest,ResourceCacheTestbash tools/ut.sh -p: 935 passed, 13 skipped, 165 deselected./tools/lint.sh -cbash tools/check-license.shgit diff --checkLocal Flink 2.2 A/B validation ran six jobs, each with a fresh TaskManager, one probe subtask, Pemja 0.5.7, and 200 measured operations:
0live bridge values; leak variant retained600messages,200tools,200 MiB, and increased RSS by160.3 MiB;0live bridge values; leak variant retained600documents,200normalized handles, about100 MiB, and increased RSS by99.1 MiB;0live resources with200logical closes; leak variant retained200resources and50 MiBwith no close calls.The deterministic live-reference, close-count, and reachable-payload measurements are the primary evidence; RSS is supporting evidence because JVM/CPython startup and native allocator behavior affect short runs.
API
This adds an
@Internalownership utility and a default materialization hook to the existing Python resource adapter. It does not change user-facing resource contracts, and the default method is binary compatible.Documentation
doc-neededdoc-not-neededdoc-includedWas this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)