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(laya-ts): end batch request contexts in reverse start order

thread3d864900eccccc
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index 68d4e2b3..8c5f36d8 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -1019,6 +1019,13 @@ export class Router extends HookRegistry {
* Run onPredictEnd for every started per-request context and raise the first hook
* failure, if any. Elapsed and usage are stamped on every context before any end hook
* runs, so one request's end hooks never inflate another request's elapsed time.
+ *
+ * The end hooks run in REVERSE start order, because every request started before any
+ * ended: a hook that opens something in start and closes it in end (a tracing span, an
+ * `AsyncLocalStorage` frame, an in-flight counter) must unwind the last one first, or it
+ * closes the wrong frame and every request after the first inherits a corrupted context.
+ * Python's `Router.predict_batch` ends its contexts the same way and says so
+ * (`laya/router.py::_end_contexts`).
*/
private _endContexts(active: Hook[], contexts: PredictContext[], raiseErrors: boolean): void {
for (const ctx of contexts) {
@@ -1026,7 +1033,7 @@ export class Router extends HookRegistry {
if (ctx.results !== null) ctx.usage = aggregateUsage(ctx.results);
}
let firstError: unknown = null;
- for (const ctx of contexts) {
+ for (const ctx of [...contexts].reverse()) {
try {
dispatch(active, "onPredictEnd", ctx, { raiseErrors });
} catch (hookErr) {
diff --git a/laya-ts/tests/router-batch.test.ts b/laya-ts/tests/router-batch.test.ts
index a6e5ad78..163fb392 100644
--- a/laya-ts/tests/router-batch.test.ts
+++ b/laya-ts/tests/router-batch.test.ts
@@ -30,7 +30,8 @@ it.each([false, true])("runs process defaults once per request with fallback=%s"
]);
expect(results).toHaveLength(2);
expect(starts).toEqual([["first request"], ["second request"]]);
- expect(ends).toEqual(starts);
+ // Ends unwrap in reverse start order: every request started before any ended.
+ expect(ends).toEqual([...starts].reverse());
} finally {
clearDefaultHooks();
}
@@ -235,7 +236,9 @@ describe("Router.routeBatch / predictBatch", () => {
const items = [req("one", { model: "english" }), req("two", { model: "english" })];
const results = await router.predictBatch(items, null, { hooks: [perCall] });
expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);
- // routeBatch runs before any predict hook. End hooks follow start order on this runtime.
+ // routeBatch runs before any predict hook. End hooks unwind in REVERSE start order (the
+ // LIFO rule Python's Router.predict_batch documents), and within one request the installed
+ // hook still runs before the per-call one.
expect(order).toEqual([
"installed:route:one",
"percall:route:one",
@@ -245,10 +248,10 @@ describe("Router.routeBatch / predictBatch", () => {
"percall:start:one",
"installed:start:two",
"percall:start:two",
- "installed:end:one",
- "percall:end:one",
"installed:end:two",
"percall:end:two",
+ "installed:end:one",
+ "percall:end:one",
]);
order.length = 0;
@@ -266,8 +269,8 @@ describe("Router.routeBatch / predictBatch", () => {
"installed:route:two",
"installed:start:one",
"installed:start:two",
- "installed:end:one",
"installed:end:two",
+ "installed:end:one",
]);
const { router: bare } = makeRouter();
@@ -295,8 +298,32 @@ describe("Router.routeBatch / predictBatch", () => {
});
expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);
expect(calls.length).toBe(1);
- // Starts run for every request in the checkpoint group, then ends, in start order.
- expect(events).toEqual(["start:one", "start:two", "end:one", "end:two"]);
+ // Starts run for every request in the checkpoint group, then ends, in REVERSE start
+ // order -- the LIFO unwind Python's Router.predict_batch documents.
+ expect(events).toEqual(["start:one", "start:two", "end:two", "end:one"]);
+ });
+
+ it("ends three request contexts in reverse start order", async () => {
+ // Two requests cannot tell FIFO from LIFO apart in a way that names the rule, and this is
+ // the shape a tracing hook depends on: start a, b, c then end c, b, a.
+ const events: string[] = [];
+ const { router } = makeRouter();
+ await router.predictBatch(
+ [req("a", { model: "english" }), req("b", { model: "english" }), req("c", { model: "english" })],
+ null,
+ {
+ onPredictStart(ctx) {
+ events.push(`start:${ctx.states[0]}`);
+ },
+ onPredictEnd(ctx) {
+ events.push(`end:${ctx.states[0]}`);
+ },
+ },
+ );
+ expect(events).toEqual([
+ "start:a", "start:b", "start:c",
+ "end:c", "end:b", "end:a",
+ ]);
});
it("hooksRaise false swallows a throwing per-call hook; the default still raises", async () => {
@@ -390,7 +417,7 @@ describe("Router.routeBatch / predictBatch", () => {
},
});
expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);
- expect(events).toEqual(["start:one", "start:two", "end:one", "end:two"]);
+ expect(events).toEqual(["start:one", "start:two", "end:two", "end:one"]);
} finally {
warn.mockRestore();
}