Skip to content

fix(java): leave nothing that can throw between taking a router lease and releasing it - #1329

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/java-router-lease-window
Oct 24, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/java-router-lease-window

Conversation

@thread3d

Copy link
Copy Markdown
Contributor

What

Close the window between taking a router Lease and entering the try that releases it.

  • predict: the languageFor(decision, lang) call moves inside try (Lease held = lease);
  • predictBatch: the contexts/indexOf allocations move above lease(checkpoint), so the
    statement after the acquisition is the one that releases it.

Why

Both methods acquire a lease before a try-with-resources that returns it:

Lease lease;
try {
    decision = decide.get();
    lease = lease(decision.model());
} catch (RuntimeException | Error failure) {
    ... failedBeforeStart(...);
    throw failure;
}
String resolved = languageFor(decision, lang);      // ← between acquire and release
try (Lease held = lease) { ... }

That gap is where a leak lives. Nothing in it throws today — languageFor only reads a record and
normalises a language tag — but the shape is exactly the one this file has already got wrong once: a
finally in Router.build that called finish(...) before lock.unlock() shipped an unreleased
lock that CodeQL caught on its first run. In predictBatch the two allocations in the same position
are the only realistic throwers left (OutOfMemoryError), and an error there would leave the lease
held with no owner able to release it.

The behaviour is unchanged: failedBeforeStart still observes a failure to route or load, and
languageFor is still computed before any hook runs, because it is the first statement inside the
block.

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 language-detection assertions on main, not this path:
RouterTest.abstainingHintFallsThrough (expected MULTILINGUAL, got ENGLISH) and
RouterLifecycleTest.closeFreesAndSeals (same expectation, after close). I reproduced both on a
clean worktree of upstream/main at 0a05a45 before touching anything.

The hook-ordering tests that pin this method's contract — AgentHooksTest, HooksTest,
RouterTest's routing cases, RouterLifecycleTest's lease accounting — pass.

Checklist

… and releasing it

In `predict`, the `languageFor(...)` call sat between the lease acquisition and
the `try (Lease held = lease)` that releases it; in `predictBatch`, the
`contexts`/`indexOf` allocations did. Neither throws today, but an
`OutOfMemoryError` from either would leave the lease held with no owner able to
release it — the same shape as the unreleased lock CodeQL found in `Router.build`.

`languageFor` now runs inside the try; the two collections are built before the
acquisition. The statement after taking a lease is the one that releases it.

Verified: ./gradlew test (JDK 17) 1182 tests, only the two inherited failures of
NandhaKishorM#1323 (which is the language-detection expectations, not this path).
@NandhaKishorM
NandhaKishorM merged commit 3efd820 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-router-lease-window 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