Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(java): a throw from finish() stranded Router's lock

Adeshina Lasisia05824116192
diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java
index 46ec4715..fcc10755 100644
--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java
+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Router.java
@@ -924,6 +924,12 @@ public final class Router implements AutoCloseable, Predictor {
// The build itself runs outside `lock`, so routing and eviction are not stalled for
// the seconds a cold checkpoint takes, and serialised by `buildLock` so two cold
// loads do not hold two checkpoints in flight at once.
+ //
+ // Acquired INSIDE the outer try deliberately, which reads like the anti-pattern and
+ // is not: the handler below is what records the failure on `inflight` and counts its
+ // latch down, so a throw from the acquisition itself must still reach it or every
+ // waiter on this checkpoint parks forever. `buildLock` is not held on that path, so
+ // the finally that releases it is never reached with nothing to release.
buildLock.lock();
try {
lock.lock();
@@ -959,12 +965,24 @@ public final class Router implements AutoCloseable, Predictor {
evictLocked(toClose);
}
} finally {
- // ALWAYS, even if eviction threw. Skipping it left the new slot published
- // with a lease that nothing could release -- so the agent could never be
- // retired or closed -- and left every waiter parked on a latch for a
- // checkpoint that had in fact loaded.
- finish(checkpoint, inflight);
- lock.unlock();
+ // `finish` ALWAYS, even if eviction threw. Skipping it left the new slot
+ // published with a lease that nothing could release -- so the agent could
+ // never be retired or closed -- and left every waiter parked on a latch for
+ // a checkpoint that had in fact loaded.
+ //
+ // And `lock.unlock()` in a finally OF ITS OWN, because the two statements in
+ // sequence did not deliver what that paragraph claims: `finish` dereferences
+ // `inflight` and counts down its latch, so a throw from it skipped the unlock
+ // and left `lock` held with no owner able to release it. Every later
+ // `lock.lock()` would then block forever -- including the handler below that
+ // exists to record the failure, so the router would wedge rather than report.
+ // Found by CodeQL's unreleased-lock query, which sees the control flow a
+ // pattern scanner cannot.
+ try {
+ finish(checkpoint, inflight);
+ } finally {
+ lock.unlock();
+ }
}
if (duplicate) {
toClose.add(agent);
@@ -1170,8 +1188,8 @@ public final class Router implements AutoCloseable, Predictor {
lock.unlock();
}
for (Checkpoint checkpoint : wanted) {
- lock.lock();
boolean already;
+ lock.lock();
try {
already = slots.containsKey(checkpoint);
} finally {
diff --git a/laya-java/scripts/scan_local.sh b/laya-java/scripts/scan_local.sh
new file mode 100755
index 00000000..68cfbecf
--- /dev/null
+++ b/laya-java/scripts/scan_local.sh
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+# Every scanner the pipeline runs against laya-java, locally, in one command.
+#
+# This exists because "the scanner will catch it in CI" means shipping a defect to a reviewer.
+# CodeQL found an unreleased lock in `Router.build` on its first CI run -- a `finally` that called
+# `finish(...)` before `lock.unlock()`, so a throw from `finish` left the lock held with no owner
+# able to release it. Semgrep could not have found it: an unreleased lock is a control-flow
+# property, and Semgrep matches patterns. The lesson is not "add Semgrep rules", it is "run the
+# engine that reasons about control flow before pushing, not after".
+#
+# Usage, from the repository root:
+#
+# laya-java/scripts/scan_local.sh # everything that needs no download
+# CODEQL=/path/to/codeql laya-java/scripts/scan_local.sh # ...and CodeQL too
+#
+# A CodeQL CLI is a ~700 MB download (the `codeql-bundle-*` asset on github/codeql-action
+# releases, which carries the query packs). Without $CODEQL this script SKIPS it and says so
+# loudly, and exits non-zero, because a partial scan reported as a pass is the thing that let the
+# lock through.
+set -u
+cd "$(dirname "$0")/../.." || exit 2
+
+status=0
+note() { printf '\n=== %s ===\n' "$1"; }
+fail() { printf ' FAILED: %s\n' "$1"; status=1; }
+
+note "workflow folding (a comment in a run: > block truncates the command)"
+python3 laya-java/scripts/check_workflow_folding.py || fail "check_workflow_folding"
+
+note "semgrep (p/java + p/security-audit, --error so findings block)"
+if command -v semgrep > /dev/null 2>&1; then
+ semgrep --config p/java --config p/security-audit --error --metrics=off \
+ laya-java/laya-java/src/main/java || fail "semgrep"
+else
+ fail "semgrep is not installed (pip install semgrep)"
+fi
+
+note "java dependency advisories (OSV)"
+( cd laya-java && ./gradlew --no-daemon -q :laya-java:dependencies \
+ --configuration runtimeClasspath ) \
+ | python3 laya-java/scripts/check_java_dependencies.py || fail "check_java_dependencies"
+
+note "codeql (java-kotlin, security-extended)"
+if [ -n "${CODEQL:-}" ] && [ -x "${CODEQL}" ]; then
+ db="${TMPDIR:-/tmp}/laya-java-codeql-db"
+ rm -rf "$db"
+ # `--command` rather than autobuild: autobuild would pick its own Gradle invocation, and this
+ # has to be the same compile the module ships -- release 17, -Werror.
+ if "$CODEQL" database create "$db" --language=java-kotlin --overwrite \
+ --source-root=. \
+ --command="laya-java/gradlew --no-daemon -p laya-java :laya-java:compileJava" \
+ > /dev/null; then
+ "$CODEQL" database analyze "$db" \
+ codeql/java-queries:codeql-suites/java-security-extended.qls \
+ --format=sarif-latest --output="${TMPDIR:-/tmp}/laya-java.sarif" > /dev/null \
+ || fail "codeql analyze"
+ python3 - "${TMPDIR:-/tmp}/laya-java.sarif" <<'PY' || fail "codeql findings"
+import io, json, sys
+sarif = json.load(io.open(sys.argv[1], encoding="utf-8"))
+results = [r for run in sarif.get("runs", []) for r in run.get("results", [])]
+if not results:
+ print(" codeql: no findings")
+ raise SystemExit(0)
+for r in results:
+ loc = (r.get("locations") or [{}])[0].get("physicalLocation", {})
+ print(" %s %s:%s %s" % (
+ r.get("ruleId"),
+ loc.get("artifactLocation", {}).get("uri"),
+ loc.get("region", {}).get("startLine"),
+ (r.get("message") or {}).get("text", "")[:90]))
+print(" codeql: %d finding(s)" % len(results))
+raise SystemExit(1)
+PY
+ else
+ fail "codeql database create"
+ fi
+else
+ printf ' SKIPPED: set CODEQL to a codeql CLI to run it.\n'
+ printf ' Not optional before a push: CodeQL is the only one of these that reasons about\n'
+ printf ' control flow, and it is what found the unreleased lock the others missed.\n'
+ status=1
+fi
+
+note "result"
+if [ "$status" -eq 0 ]; then
+ echo " every scanner ran and found nothing"
+else
+ echo " at least one scanner failed, was skipped, or reported findings -- do not push"
+fi
+exit "$status"