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(hooks): reject non-finite hooks_timeout and enforce in API contract

Swatantra Yadavb015929ad509
diff --git a/laya/hooks.py b/laya/hooks.py
index f2e3696e..fc3829c3 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import asyncio
import contextvars
import inspect
+import math
import threading
import time
import uuid
@@ -245,12 +246,14 @@ def validate_timeout(value: Optional[float]) -> Optional[float]:
A non-positive timeout is rejected here rather than left to
``thread.join``: ``join(0)`` and ``join(-1)`` return before the hook has
started, so the outcome of a fast hook with such a value is a race.
+ Non-finite values are rejected too: ``join(nan)`` raises ``ValueError`` and
+ ``join(inf)`` raises ``OverflowError`` instead of waiting.
"""
if value is None:
return None
timeout = float(value)
- if timeout <= 0:
- raise ValueError("hooks_timeout must be a positive number or None; got %r" % (value,))
+ if not math.isfinite(timeout) or timeout <= 0:
+ raise ValueError("hooks_timeout must be a positive finite number or None; got %r" % (value,))
return timeout
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 7a9b6fb6..4d0f81b5 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1478,12 +1478,19 @@ from laya.hooks import validate_timeout # noqa: E402
check_raises("timeout/zero is rejected", ValueError, lambda: validate_timeout(0))
check_raises("timeout/negative is rejected", ValueError, lambda: validate_timeout(-0.5))
+check_raises("timeout/NaN is rejected", ValueError, lambda: validate_timeout(float("nan")))
+check_raises("timeout/infinity is rejected", ValueError, lambda: validate_timeout(float("inf")))
+check_raises("timeout/-infinity is rejected", ValueError, lambda: validate_timeout(float("-inf")))
check("timeout/positive passes through", validate_timeout(1.5), 1.5)
check("timeout/None means no limit", validate_timeout(None), None)
f = make_fake()
check_raises("timeout/zero per call is rejected", ValueError,
lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=0))
+check_raises("timeout/NaN per call is rejected", ValueError,
+ lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=float("nan")))
+check_raises("timeout/infinity per call is rejected", ValueError,
+ lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=float("inf")))
not_running = asyncio.new_event_loop()
try:
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 2dc747d6..b059c2f4 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -201,10 +201,21 @@ for event in HOOK_EVENTS:
# process-wide default registry lives in laya.hooks (not the top level)
for helper in ("default_hooks", "set_default_hooks", "add_default_hook", "clear_default_hooks",
- "compose_hooks"):
+ "compose_hooks", "validate_timeout"):
check_true("laya.hooks/%s exists" % helper, callable(getattr(__import__("laya.hooks", fromlist=[helper]), helper, None)))
check_true("defaults/not exported at top level", not hasattr(laya, "set_default_hooks"))
+from laya.hooks import validate_timeout # noqa: E402
+
+check("validate_timeout/None", validate_timeout(None), None)
+check("validate_timeout/positive float", validate_timeout(1.5), 1.5)
+for bad in (0, -1, float("nan"), float("inf"), float("-inf")):
+ try:
+ validate_timeout(bad)
+ FAIL.append("validate_timeout/%r accepted; want ValueError" % (bad,))
+ except ValueError:
+ PASS.append("validate_timeout/%r rejected" % (bad,))
+
# --------------------------------------------------------------- class defaults
for label, cls in (("Agent", Agent), ("Router", Router), ("ONNXAgent", ONNXAgent)):
check("%s/hooks default" % label, cls.hooks, ())