fix(client): reject invalid timeout env values
Some checks failed
CI / Python 3.12 test suite (push) Has been cancelled
Some checks failed
CI / Python 3.12 test suite (push) Has been cancelled
This commit is contained in:
parent
547ee160ad
commit
e4ca2bef8e
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
@ -154,17 +155,22 @@ process.stdout.write(
|
||||
def _env_float(name: str, default: float) -> float:
|
||||
"""Read a float from the environment, falling back on the default.
|
||||
|
||||
Bad / unparseable values are ignored (we log a warning below) so a
|
||||
typo in ``CLAWBENCH_CONNECT_TIMEOUT`` never silently bricks a run.
|
||||
Bad, non-finite, or non-positive values are ignored (we log a
|
||||
warning below) so a typo in ``CLAWBENCH_CONNECT_TIMEOUT`` never
|
||||
silently bricks a run.
|
||||
"""
|
||||
raw = os.environ.get(name)
|
||||
if raw is None or raw == "":
|
||||
return default
|
||||
try:
|
||||
return float(raw)
|
||||
value = float(raw)
|
||||
except ValueError:
|
||||
logger.warning("ignoring invalid %s=%r; using default %s", name, raw, default)
|
||||
return default
|
||||
if not math.isfinite(value) or value <= 0:
|
||||
logger.warning("ignoring invalid %s=%r; using default %s", name, raw, default)
|
||||
return default
|
||||
return value
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@ -28,8 +28,9 @@ def test_gateway_config_env_overrides(monkeypatch):
|
||||
assert cfg.request_timeout == 120.0
|
||||
|
||||
|
||||
def test_gateway_config_invalid_env_falls_back_to_default(monkeypatch, caplog):
|
||||
monkeypatch.setenv("CLAWBENCH_CONNECT_TIMEOUT", "not-a-number")
|
||||
@pytest.mark.parametrize("raw", ["not-a-number", "nan", "inf", "0", "-1"])
|
||||
def test_gateway_config_invalid_env_falls_back_to_default(monkeypatch, caplog, raw):
|
||||
monkeypatch.setenv("CLAWBENCH_CONNECT_TIMEOUT", raw)
|
||||
with caplog.at_level("WARNING"):
|
||||
cfg = GatewayConfig()
|
||||
assert cfg.connect_timeout == 30.0
|
||||
|
||||
Loading…
Reference in New Issue
Block a user