Skip to content

fix(ts): name the hook and event in hook timeout errors - #1140

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
sathariels:fix/ts-hook-timeout-naming
Oct 12, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
sathariels:fix/ts-hook-timeout-naming

Conversation

@sathariels

Copy link
Copy Markdown
Contributor

Summary

Follow-up to the timeout path in laya-ts/src/hooks.ts that #1056 got parsing again. In the merge comment on that PR, Nandha kept runWithTimeout as Promise<void> and said returning the hook's value would be a contract change of its own. Nothing in the TypeScript runtime reads a hook's return value, so this leaves that signature alone.

The bug that is still on main is the message. operationName was built from opts.hook, which agent.ts never passes, and it dropped the event, so a timeout came out as hook hook. timed out after 50ms. It now names the hook that actually ran, Class.event, and uses the same wording as Python and the Java port: laya: hook SlowHook.onPredictStart exceeded 0.05s, on an Error whose name is TimeoutError.

What changed

  • dispatchAsync builds the name per hook as ${hook.constructor.name}.${event} instead of reading opts.hook.
  • Seconds are printed the way CPython's %g prints them (0.05, 1, not 1.0), matching laya/hooks.py and Hooks.seconds in the Java SDK.
  • A hook that returns a plain value starts no timer. A thenable still races the deadline, and clearTimeout runs on the way out either way.
  • Sync dispatch no longer computes timeoutMs and operationName and then ignores them. It still does not wait, and it still does not apply a timeout.
  • The PredictBatchOptions and predictBatch docblocks now say setDefaultHooks defaults, then installed hooks, then this call's hooks, which is what composeHooks already does (laya/router.py around the predict_batch composition comment).

Tests

New cases in laya-ts/tests/hooks.test.ts, under fake timers:

  • a timeout names the hook and the event, and the error name is TimeoutError
  • a fast async hook clears the timer it armed
  • a synchronous hook never calls setTimeout
  • hooksRaise: false warns with that same message and the call still returns
  • a per-call hooksTimeout overrides the agent default in both directions

On current main, before this change, four of those five failed. The warning was laya: hook SlowHook.onPredictStart failed: Error: hook hook. timed out after 50ms, the thrown error's name was Error, and the sync hook still armed a 50ms timer. The "clears the timer" case already passed, because runWithTimeout cleared in finally.

npm test -- --run in laya-ts: typecheck plus 35 files, 875 tests, all passed. npx tsc --noEmit is clean. There is no TypeScript lint script; CONTRIBUTING's ruff gate is Python, and this commit does not touch it. No changelog entry: the repo convention for this tree is the commit, and laya-ts has no changelog.

A timed-out hook reported `hook hook. timed out after Nms` because the
name came from opts.hook, which callers do not pass, and the event was
dropped. Name each hook as Class.event and match Python's TimeoutError
wording. Skip the timer for a synchronous result, and drop the unused
timeout locals in sync dispatch.

Co-authored-by: Nithilan Kumaran <sathariels@users.noreply.github.com>
@NandhaKishorM
NandhaKishorM merged commit 546ad74 into NandhaKishorM:main Oct 12, 2026
38 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged. Verified: 875 tests across 35 files pass, tsc --noEmit, test:types, build and test:package all clean.

Three separate defects fixed here, and the message format is the least of them.

The operationName was ${constructor.name}. with nothing after the dot, built once outside the hook loop from opts.hook rather than per hook from the hook being run, so every timeout reported the same name and none of them reported the event. Moving it inside the loop and reading hook, not opts.hook, is what makes it name the hook that actually timed out.

The AbortController was doing nothing an AbortController is for. Nothing awaited the signal, so the listener existed only to reject a promise the setTimeout could have rejected directly, and abortController.abort() never reached anything cancellable. The plain timer is both simpler and the same behaviour.

The non-thenable early return is the real behaviour change: a synchronous hook already finished, so arming a timer for it was pure overhead, and does not start a timer for a synchronous hook pins it with a setTimeout spy rather than by timing.

formatTimeoutSeconds matching CPython's %g is more care than I expected for a message string, and it is the right amount given the point is parity: exceeded 1s not exceeded 1.0s, and TimeoutError as the error name since JS has no such class. The per-call override test covering both directions (short call under a long default, long call under a short default) is what proves the composition rather than just the formatting.

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