OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Python SDK blocking mutations time out before the server wait budget

Open HaningZS opened this issue 1 month ago • 1 comments

Issue Origin

Observed or reproduced in a real environment: source SDK with real loopback HTTP transport and deterministic delayed responses, not a deployed OpenViking/model service.

Bug Description

The Python SDK forwards an explicit server wait timeout but does not extend the HTTP read timeout for add_resource, add_skill, update_skill, rm, or write. A blocking call can therefore raise httpx.ReadTimeout before the server's requested wait budget expires. Both async and sync clients are affected.

batch_write already avoids this through _wait_request_kwargs: for wait=True with an explicit timeout, its read budget is max(client_timeout, timeout + 30), while connect/write/pool budgets stay at the configured client timeout. That existing rule was introduced in #3948.

At current main 676c4038, the five methods above omit this request option. The server's timeout is a processing-wait budget, not the client's HTTP budget. In particular, the resource wait-expiry response can contain DEADLINE_EXCEEDED and a task_id (#4888); an earlier transport timeout prevents the caller from receiving that result.

Steps to Reproduce

  1. Initialize the current source AsyncHTTPClient or SyncHTTPClient with HTTP timeout 0.08 seconds.
  2. Call a listed method with wait=True, timeout=1.0 against a local HTTP fixture that returns a valid response after 0.25 seconds.
  3. Compare it with a non-empty batch_write using the same client settings and delay.

The scaled durations keep reproduction bounded. The equivalent budget mismatch also occurs with longer ordinary values, e.g. a 180-second client budget and 300-second server wait.

Expected Behavior / Proposed Contract

Apply the existing batch_write rule consistently to these five explicit-wait operations:

  • Extend only the HTTP read budget to max(client_timeout, timeout + 30).
  • Preserve the configured connect, upload/write, and pool budgets, request payloads, and error mapping.
  • Leave wait=False and an unspecified timeout=None on the ordinary client HTTP budget; do not turn them into unlimited requests.
  • Preserve an explicit zero server timeout rather than treating it as omitted, so its server deadline response can still be received.
  • Do not add retries, cancel background work, or change server waiting semantics.

This proposal reuses the existing timeout owner instead of introducing new configuration or a separate deadline policy. Please flag if maintainers intend these single-operation methods to follow a different HTTP-budget contract.

Actual Behavior

Before changing the implementation, I exercised six operations (the five affected ones plus batch_write) through both public SDK clients, with five success/error/control scenarios: 60 real HTTP cases, plus caller-cancellation validation.

  • The five affected operations timed out at about 0.08 seconds in the explicit-wait success scenario; batch_write received the delayed success.
  • They also hid delayed server DEADLINE_EXCEEDED responses behind ReadTimeout; batch_write preserved the error and nested details.
  • Ordinary no-wait and unspecified-timeout controls retained their client deadline, as intended.

An expansion of the existing batch-write timeout contract test also gives 10 failing read-budget cases and 20 passing controls on the unmodified implementation. These results exercise the SDK and HTTP boundary only; they do not establish model execution, storage behavior, or background-task completion.

Minimal Reproducible Example

Run with the source SDK on PYTHONPATH or openviking-sdk installed:

import asyncio
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import httpx
from openviking_sdk import AsyncHTTPClient


class Handler(BaseHTTPRequestHandler):
    def log_message(self, *args):
        pass

    def do_POST(self):
        self.rfile.read(int(self.headers.get("Content-Length", 0)))
        time.sleep(0.25)
        body = b'{"status":"ok","result":{}}'
        try:
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
        except (BrokenPipeError, ConnectionResetError):
            pass


async def reproduce(url):
    client = AsyncHTTPClient(url=url, timeout=0.08, api_key="fixture-key")
    await client.initialize()
    try:
        calls = {
            "write": lambda: client.write(
                "viking://resources/demo.md", "body", wait=True, timeout=1.0
            ),
            "batch_write": lambda: client.batch_write(
                "viking://resources/demo",
                [{"uri": "viking://resources/demo/file.md", "content": "body"}],
                wait=True, timeout=1.0,
            ),
        }
        for name, call in calls.items():
            try:
                await call()
                print(name, "ok")
            except httpx.ReadTimeout:
                print(name, "ReadTimeout")
    finally:
        await client.close()


with ThreadingHTTPServer(("127.0.0.1", 0), Handler) as server:
    threading.Thread(target=server.serve_forever, daemon=True).start()
    try:
        asyncio.run(reproduce(f"http://127.0.0.1:{server.server_port}"))
    finally:
        server.shutdown()
# Current: write ReadTimeout; batch_write ok

OpenViking Version

Official main 676c4038; source Python SDK (latest SDK release is 0.1.15).

Python Version

3.13.12; httpx 0.28.1.

Operating System

macOS (Apple Silicon).

Model Backend

Other: no model call.

Additional Context

I checked open and closed issues/PRs for ReadTimeout, timeout titles, batch_write/timeout, and SDK HTTP/wait budgets. #3404 concerns constructor-timeout precedence, #4888 concerns server wait-expiry reporting, and #3948 introduced the existing batch-write protection; none covers these missing five call sites. The current server/SDK code still reproduces the mismatch.

I can send a focused PR reusing _wait_request_kwargs at those call sites, extending the existing contract test, and documenting the two timeout budgets in both SDK READMEs. The work and reproductions are AI-assisted, under human-selected scope and review direction.

HaningZS avatar Oct 13 '26 09:10 HaningZS

Proposed fix: #6008. It reuses the existing read-timeout policy at the five missing call sites and keeps upload/connect/pool budgets, no-wait/unspecified-timeout behavior, and server deadline/error details unchanged.

The expanded existing contract test fails 10 cases on the baseline and passes all 30 after the fix. Cross-version SDK tests and bounded real-HTTP success/error/upload/cancellation controls passed; full details and the known baseline diagnostics are in the PR. The change is ready for maintainer review of the proposed boundary.

HaningZS avatar Oct 13 '26 09:10 HaningZS