From 34a64ec9c6cd4f74e126760f418d109af4f97ecc Mon Sep 17 00:00:00 2001 From: plean Date: Wed, 18 Nov 2026 03:03:53 +0000 Subject: [PATCH] served_ms is request-level (includes queue wait); with-open for image reads - served_ms measured from request entry, not inside the worker thread: a client that queued 8 s behind a parse no longer sees served_ms: 12 - image file handle bound in a with block (no fd held to GC) --- omni_api/mcp_server.py | 3 ++- omni_api/server.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/omni_api/mcp_server.py b/omni_api/mcp_server.py index 8d048b0..7e889e5 100644 --- a/omni_api/mcp_server.py +++ b/omni_api/mcp_server.py @@ -78,7 +78,8 @@ def omniparse(image_path: str = '', image_b64: str = '', format: str = 'view', else f'b64[{len(image_b64)} chars]' if image_b64 else 'no-input') try: if image_path: - b64 = base64.b64encode(open(image_path, 'rb').read()).decode() + with open(image_path, 'rb') as fh: + b64 = base64.b64encode(fh.read()).decode() elif image_b64: b64 = image_b64 else: diff --git a/omni_api/server.py b/omni_api/server.py index 2de91d9..5b8154d 100644 --- a/omni_api/server.py +++ b/omni_api/server.py @@ -94,8 +94,7 @@ def _cache_key(raw: bytes, req) -> str: return h.hexdigest() -def _run_cached(key: str, raw: bytes, req) -> dict: - t0 = time.perf_counter() +def _run_cached(key: str, raw: bytes, req, t0: float) -> dict: eng: ParseEngine = STATE['engine'] cached = STATE['cache'].get(key) hit = cached is not None @@ -144,6 +143,7 @@ async def root(): @app.post('/parse') async def parse(req: ParseReq, authorization: str | None = Header(None)): + t0 = time.perf_counter() # request-level: includes queue wait the client feels _auth(authorization) if not STATE['ready']: raise HTTPException(503, 'models still loading - see /healthz', @@ -164,12 +164,12 @@ async def parse(req: ParseReq, authorization: str | None = Header(None)): try: async with STATE['queue']: return await asyncio.to_thread(_run_cached, _cache_key(raw_bytes, req), - raw_bytes, req) + raw_bytes, req, t0) finally: STATE['waiters'] -= 1 async with STATE['queue']: return await asyncio.to_thread(_run_cached, _cache_key(raw_bytes, req), - raw_bytes, req) + raw_bytes, req, t0) @app.post('/parse_upload')