Repository navigation
fix(java): close the ONNX session options, and a half-built split graph - #1328
Merged
NandhaKishorM merged 1 commit intoOct 24, 2026
Merged
Conversation
`open()` never closed its `OrtSession.SessionOptions`: they are an input to `createSession`, not something a live session keeps, so every load leaked one native options structure for the life of the process. That is invisible until a caller reloads checkpoints and native memory keeps climbing. While there: in the split form (`encoder.onnx` + `head.onnx`), a failure creating the head session closed nothing. `close()` is only reachable through a `LayaSession`, and that path never constructs one, so the encoder outlived the failed load. It is closed now, with its own failure attached as suppressed, the way `close()` itself reports a second failure. `OrtEnvironment` is deliberately untouched: it is ONNX Runtime's process-wide singleton, and closing it would break every other session in the process. Verified: ./gradlew test (JDK 17) 1182 tests, only the two inherited failures of NandhaKishorM#1323; the sample programs still compile and jcheck reports 0 diagnostics for them against the rebuilt classes.
NandhaKishorM
added a commit
that referenced
this pull request
Oct 24, 2026
33 PRs. Server hardening (#1331, #1332, #1333), TypeScript parity (#1330, #1336, #1337, #1338, #1339, #1340, #1341, #1342, #1343), fail-closed input handling in evals and the integrations (#1317, #1318, #1319, #1320, #1321, #1322, #1326, #1335), exact and faster script detection (#1334, #1349), JVM and .NET resource handling (#1327, #1328, #1329, #1346), port goldens and clipping (#1324 excluded, #1325, #1344, #1345), CI (#1323, #1347, #1348). Verified before tagging: all 92 suites pass, 610 tests. ruff clean on both lists. laya-ts 1247 tests across 44 files, tsc --noEmit clean. Version agrees across pyproject.toml, laya.__version__ and nix/package.nix. README carries 0.4.15 only. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What
LayaSession.openreleases what it allocates:OrtSession.SessionOptionsbecomes a try-with-resources resource, closed once both graphs arebuilt;
encoder.onnx+head.onnx), a failure creating the head session now closesthe encoder session that was already created, with the close failure attached as suppressed.
Why
SessionOptionsis an input tocreateSession, not something a live session keeps, andopen()never closed it: every load leaked one native options structure for the life of the process. That is
invisible until a caller reloads checkpoints — a router that evicts and reloads, or a server that
serves several models — and then native memory keeps climbing with no Java reference explaining it.
The second half is an error path.
LayaSession.close()is the only way to release a session, and itis only reachable through a constructed
LayaSession; when the head session failed to load, nothinghad been constructed, so the encoder session already created in that same method outlived the failed
load. Reporting the load failure is correct, but it should not cost a native session as well.
OrtEnvironmentis deliberately left alone:OrtEnvironment.getEnvironment()is ONNX Runtime'sprocess-wide singleton, and closing it would break every other session in the process. A linter that
flags every
AutoCloseablelocal flags it; that one is not a leak.How it was verified
Both failures are #1323's stale router assertions (
RouterTest,RouterLifecycleTest), unrelated tothis file;
LayaSessionis exercised byOneOptionGraphTestand the parity suites, which pass.The failure path itself (a good encoder with a graph that cannot be loaded) has no fixture in the
suite, so it is verified by construction rather than by a test:
close()'s own javadoc records thata failure closing one session used to leave the other open, and this applies the same rule to a
failure constructing the pair.
Checklist
main(upstream/mainat 0a05a45)