Skip to content

fix(java): close the ONNX session options, and a half-built split graph - #1328

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/java-session-resources
Oct 24, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/java-session-resources

Conversation

@thread3d

Copy link
Copy Markdown
Contributor

What

LayaSession.open releases what it allocates:

  • OrtSession.SessionOptions becomes a try-with-resources resource, closed once both graphs are
    built;
  • in the split form (encoder.onnx + head.onnx), a failure creating the head session now closes
    the encoder session that was already created, with the close failure attached as suppressed.

Why

SessionOptions is an input to createSession, not something a live session keeps, and open()
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 it
is only reachable through a constructed LayaSession; when the head session failed to load, nothing
had 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.

OrtEnvironment is deliberately left alone: OrtEnvironment.getEnvironment() is ONNX Runtime's
process-wide singleton, and closing it would break every other session in the process. A linter that
flags every AutoCloseable local flags it; that one is not a leak.

How it was verified

cd laya-java && ./gradlew --no-daemon test        # JDK 17, -Xlint:all -Werror
# 1182 tests completed, 2 failed, 14 skipped

Both failures are #1323's stale router assertions (RouterTest, RouterLifecycleTest), unrelated to
this file; LayaSession is exercised by OneOptionGraphTest and the parity suites, which pass.

# the three sample programs still compile and check clean against the rebuilt classes
jcheck check --root samples --sources Quickstart.java,Routing.java,Benchmark.java \
  --classpath ".../build/classes/java/main:.../onnxruntime-1.20.0.jar" --release 17
# analysis reached T3 of 5; jcheck: 0 errors, 0 warnings.

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 that
a failure closing one session used to leave the other open, and this applies the same rule to a
failure constructing the pair.

Checklist

`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
NandhaKishorM merged commit 3cd6509 into NandhaKishorM:main Oct 24, 2026
46 of 50 checks passed
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>
@thread3d
thread3d deleted the fix/java-session-resources branch October 28, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants