Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(server): 减少请求循环引用和 GC 延迟 (#5127)
Qin Haojie398e978b98d3
diff --git a/openviking/observability/http_observability_middleware.py b/openviking/observability/http_observability_middleware.pyindex 5df654a3c..d2ca05659 100644--- a/openviking/observability/http_observability_middleware.py+++ b/openviking/observability/http_observability_middleware.py@@ -16,10 +16,10 @@ import threadingimport timefrom contextlib import nullcontextfrom dataclasses import dataclass-from typing import Any, Awaitable, Callable, ContextManager, Optional+from typing import Any, ContextManager, Optionalfrom starlette.requests import Request-from starlette.responses import Response+from starlette.types import ASGIApp, Message, Receive, Scope, Sendfrom openviking.metrics.datasources import HttpRequestLifecycleDataSourcefrom openviking.observability.context import (@@ -745,171 +745,98 @@ def apply_http_metrics_finalize()-async def _execute_request_with_span(- request: Request,- call_next: Callable[[Request], Awaitable[Response]],- root_attrs: RootSpanAttributes,- span_cm: Optional[Any],-) -> Response:- """- Execute the request with optional span context management.-- This function encapsulates the common logic for both cases:- 1. When OTel is available and a span context manager exists- 2. When OTel is not available or span creation failed-- Uses `contextlib.nullcontext` to unify the handling of both cases,- eliminating code duplication.-- Args:- request: The incoming HTTP request.- call_next: The next middleware/handler in the chain.- root_attrs: The root span attributes.- span_cm: The span context manager (if available), or None.-- Returns:- The response from the next handler.-- Raises:- Any exception raised by the next handler.- """- # Use nullcontext when span_cm is None to unify handling- context = span_cm if span_cm is not None else nullcontext()-- with context as span:- try:- response = await call_next(request)-- # Update route and span name if we have a real span- if span is not None:- _maybe_update_route_and_span_name(- request=request,- root_attrs=root_attrs,- span=span,- )- # Apply root attributes to span- maybe_apply_root_span_attributes(root_attrs)-- # Set status code from response- root_attrs.http_status_code = int(getattr(response, "status_code", 500))-- # Apply response state (status code, etc.)- maybe_apply_root_span_response(root_attrs)-- return response-- except Exception as exc:- # The catch-all JSON renderer is outside this middleware, so mirror- # its public mapping before the audit event is emitted. This keeps- # the stored status and envelope aligned with the client response;- # unexpected exception text remains confined to logs and traces.- try:- from openviking.server.error_mapping import map_exception- from openviking.server.models import ERROR_CODE_TO_HTTP_STATUS-- mapped = map_exception(exc)- except Exception:- mapped = None- if mapped is not None:- root_attrs.http_status_code = ERROR_CODE_TO_HTTP_STATUS.get(mapped.code, 500)- capture_public_http_error(- code=mapped.code,- message=mapped.message,- details=mapped.details,- )- else:- root_attrs.http_status_code = 500- capture_public_http_error(- code="INTERNAL",- message="Internal server error",- )- # Update route and span name if we have a real span- if span is not None:- _maybe_update_route_and_span_name(- request=request,- root_attrs=root_attrs,- span=span,- )- # Apply root attributes to span- maybe_apply_root_span_attributes(root_attrs)-- # Apply error state to span- maybe_apply_root_span_error(root_attrs, exc)-- raise+class HTTPObservabilityMiddleware:+ """Observe HTTP requests without task or response-stream wrappers."""+ def __init__(self, app: ASGIApp) -> None:+ self.app = app-def create_http_observability_middleware() -> Callable[[Request, Callable], Response]:- """- Create the unified HTTP observability middleware.-- This middleware handles:- - HTTP metrics collection (inflight, request duration, status codes)- - OTel trace span creation and management- - Context propagation (root observability context binding)-- Returns:- The middleware function.- """+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:+ if scope["type"] != "http":+ await self.app(scope, receive, send)+ return- async def middleware(request: Request, call_next: Callable) -> Response:- # Skip metrics/tracing for health check and metrics endpoints+ request = Request(scope)if should_skip_http_metrics(request):- return await call_next(request)-- # Extract request information- raw_path = str(request.url.path)- route_template = _get_route_template(request)- request_id = request.state.request_id+ await self.app(scope, receive, send)+ return- # Create root span attributesroot_attrs = create_root_span_attributes(http_method=request.method,- http_route=route_template,- request_id=request_id,- url_path=raw_path,+ http_route=_get_route_template(request),+ request_id=request.state.request_id,+ url_path=str(request.url.path),url_scheme=request.url.scheme,http_host=request.url.netloc,source_type=request.headers.get("x-source-type"),source_version=request.headers.get("x-source-version"),)-- # Attach to request state for later accessrequest.state.root_span_attrs = root_attrs- request.state.request_id = request_id-- # Bind root context and start metricserror_token = bind_http_error_context()root_token = bind_root_observability_context(root_attrs)- apply_http_metrics_start(request=request, root_attrs=root_attrs)-start = time.perf_counter()+ response_elapsed: float | None = Nonetry:- # Try to create a root span if OTel is available+ apply_http_metrics_start(request=request, root_attrs=root_attrs)span_cm = maybe_start_root_span(request, root_attrs)-- # Execute the request with unified span handling- response = await _execute_request_with_span(- request=request,- call_next=call_next,- root_attrs=root_attrs,- span_cm=span_cm,- )-- return response-+ with span_cm if span_cm is not None else nullcontext() as span:++ async def send_observed(message: Message) -> None:+ nonlocal response_elapsed+ if message["type"] == "http.response.start":+ root_attrs.http_status_code = int(message["status"])+ if span is not None:+ _maybe_update_route_and_span_name(+ request=request, root_attrs=root_attrs, span=span+ )+ maybe_apply_root_span_attributes(root_attrs)+ maybe_apply_root_span_response(root_attrs)+ # Preserve the existing response-header latency metric.+ # Keep context bound until streaming/background work exits.+ response_elapsed = time.perf_counter() - start+ apply_http_metrics_finalize(+ request=request, root_attrs=root_attrs, elapsed=response_elapsed+ )+ await send(message)++ try:+ await self.app(scope, receive, send_observed)+ except Exception as exc:+ if response_elapsed is None:+ # Match the outer exception renderer before publishing the+ # audit event. A response already started keeps its status.+ from openviking.server.error_mapping import map_exception+ from openviking.server.models import ERROR_CODE_TO_HTTP_STATUS++ mapped = map_exception(exc)+ if mapped is not None:+ root_attrs.http_status_code = ERROR_CODE_TO_HTTP_STATUS.get(+ mapped.code, 500+ )+ capture_public_http_error(+ code=mapped.code, message=mapped.message, details=mapped.details+ )+ else:+ root_attrs.http_status_code = 500+ capture_public_http_error(+ code="INTERNAL", message="Internal server error"+ )+ if span is not None:+ _maybe_update_route_and_span_name(+ request=request, root_attrs=root_attrs, span=span+ )+ maybe_apply_root_span_attributes(root_attrs)+ maybe_apply_root_span_error(root_attrs, exc)+ raisefinally:- # Calculate elapsed time and finalize metrics- elapsed = time.perf_counter() - start- apply_http_metrics_finalize(- request=request,- root_attrs=root_attrs,- elapsed=elapsed,- )-- # Reset root context- reset_root_observability_context(root_token)- reset_http_error_context(error_token)-- return middleware+ try:+ if response_elapsed is None:+ apply_http_metrics_finalize(+ request=request,+ root_attrs=root_attrs,+ elapsed=time.perf_counter() - start,+ )+ finally:+ reset_root_observability_context(root_token)+ reset_http_error_context(error_token)diff --git a/openviking/server/app.py b/openviking/server/app.pyindex 0acdf0e6b..01d020d45 100644--- a/openviking/server/app.py+++ b/openviking/server/app.py@@ -3,9 +3,7 @@"""FastAPI application for OpenViking HTTP Server."""import asyncio-import loggingimport os-import timefrom concurrent.futures import ThreadPoolExecutorfrom contextlib import asynccontextmanagerfrom pathlib import Path@@ -29,7 +27,7 @@ from openviking.server.dependencies import set_server_config, set_servicefrom openviking.server.error_mapping import map_exceptionfrom openviking.server.identity import Rolefrom openviking.server.models import ERROR_CODE_TO_HTTP_STATUS, ErrorInfo, Response-from openviking.server.profile_middleware import create_profile_http_middleware+from openviking.server.profile_middleware import ProfileMiddlewarefrom openviking.server.request_id import REQUEST_ID_HEADER, RequestIdMiddlewarefrom openviking.server.routers import (acl_router,@@ -59,6 +57,7 @@ from openviking.server.routers import (watches_router,webdav_router,)+from openviking.server.timing_middleware import RequestTimingMiddlewarefrom openviking.service.core import OpenVikingServicefrom openviking.service.task_tracker import get_task_trackerfrom openviking_cli.exceptions import OpenVikingError@@ -464,62 +463,16 @@ def create_app(config.observability.dump_body.max_bytes,)- # Add HTTP observability middleware (metrics, tracing).- # Note: In FastAPI/Starlette, middleware added later executes first (outer layer).- # We want timing to be the outermost layer to measure the full request duration.+ # Later registrations wrap earlier ones: timing/header logging -> profile ->+ # observability -> optional body dump -> routes. Native ASGI middleware keeps+ # response streams and request execution on the downstream application's path.from openviking.observability.http_observability_middleware import (- create_http_observability_middleware,+ HTTPObservabilityMiddleware,)- http_observability_middleware = create_http_observability_middleware()- profile_http_middleware = create_profile_http_middleware()-- @app.middleware("http")- async def add_http_observability(request: Request, call_next: Callable):- return await http_observability_middleware(request, call_next)-- @app.middleware("http")- async def add_profile_output(request: Request, call_next: Callable):- return await profile_http_middleware(request, call_next)-- # Add request timing middleware last (so it executes first as the outermost layer)- # This ensures X-Process-Time includes the full request duration including- # observability middleware overhead.- # Add request header logging middleware (for debug)- @app.middleware("http")- async def log_request_headers(request: Request, call_next: Callable):- access_logger = logging.getLogger("uvicorn.access")- if access_logger.isEnabledFor(logging.DEBUG):- headers = dict(request.headers)- header_names = ", ".join(sorted(headers.keys()))- access_logger.debug(- f"Request headers for {request.method} {request.url.path}: {header_names}"- )- response = await call_next(request)- return response-- # Add request timing middleware- @app.middleware("http")- async def add_timing(request: Request, call_next: Callable):- """- Middleware to measure request processing time.-- This middleware is added last so it executes as the outermost layer,- ensuring X-Process-Time includes the full request duration including- all other middleware overhead.-- Args:- request: The incoming HTTP request.- call_next: The next middleware/handler in the chain.-- Returns:- The response with X-Process-Time header added.- """- start_time = time.perf_counter()- response = await call_next(request)- process_time = time.perf_counter() - start_time- response.headers["X-Process-Time"] = str(process_time)- return response+ app.add_middleware(HTTPObservabilityMiddleware)+ app.add_middleware(ProfileMiddleware)+ app.add_middleware(RequestTimingMiddleware)# Add exception handler for OpenVikingError@app.exception_handler(OpenVikingError)diff --git a/openviking/server/profile_middleware.py b/openviking/server/profile_middleware.pyindex ebbd390ef..df3931bbb 100644--- a/openviking/server/profile_middleware.py+++ b/openviking/server/profile_middleware.py@@ -9,12 +9,11 @@ import jsonimport siteimport sysconfigfrom pathlib import Path-from typing import Awaitable, Callablefrom fastapi import Request-from fastapi.responses import FileResponse, JSONResponse-from starlette.responses import Response as StarletteResponse-from starlette.responses import StreamingResponse+from fastapi.responses import JSONResponse+from starlette.datastructures import Headers, MutableHeaders+from starlette.types import ASGIApp, Message, Receive, Scope, SendPROFILE_TRUE_VALUES = {"1", "true", "yes", "on"}PROFILE_SORT_BY = "cumulative"@@ -110,7 +109,7 @@ def _sanitize_profile_path(path: str) -> str:return "/".join(parts[idx + 1 :])for idx, part in enumerate(parts):- if part.startswith("python") and idx + 1 < len(parts):+ if part.startswith("python") and part[6:7].isdigit() and idx + 1 < len(parts):suffix = parts[idx + 1 :]if suffix:return "/".join(suffix)@@ -167,60 +166,55 @@ def format_profile_output(profiler: cProfile.Profile) -> list[str]:return truncated.splitlines()-async def inject_profile_into_response(response, profile_lines: list[str]):- if isinstance(response, (FileResponse, StreamingResponse)):- return response+class ProfileMiddleware:+ """Profile response generation and annotate ordinary JSON responses."""- content_type = response.headers.get("content-type", "").lower()- if "application/json" not in content_type and "+json" not in content_type:- return response+ def __init__(self, app: ASGIApp) -> None:+ self.app = app- if hasattr(response, "body") and response.body is not None:- body = response.body- else:- body = b""- async for chunk in response.body_iterator:- body += chunk-- try:- payload = json.loads(body)- except (TypeError, ValueError):- return StarletteResponse(- content=body,- status_code=response.status_code,- headers=dict(response.headers),- media_type=response.media_type,- )-- if not isinstance(payload, dict):- return StarletteResponse(- content=body,- status_code=response.status_code,- headers=dict(response.headers),- media_type=response.media_type,- )-- payload["profile"] = profile_lines- rebuilt = JSONResponse(status_code=response.status_code, content=payload)- for key, value in response.headers.items():- if key.lower() not in {"content-length", "content-type"}:- rebuilt.headers[key] = value- return rebuilt---def create_profile_http_middleware() -> Callable[[Request, Callable[..., Awaitable]], Awaitable]:- async def add_profile_output(request: Request, call_next: Callable[..., Awaitable]):- if not profile_enabled(request):- return await call_next(request)+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:+ if scope["type"] != "http" or not profile_enabled(Request(scope)):+ await self.app(scope, receive, send)+ returnprofiler = cProfile.Profile()+ response_start: Message | None = None++ async def send_profiled(message: Message) -> None:+ nonlocal response_start+ if message["type"] == "http.response.start":+ profiler.disable()+ headers = Headers(raw=message.get("headers", []))+ content_type = headers.get("content-type", "").lower()+ # JSONResponse sends a sized body in one message. StreamingResponse+ # omits its length; FileResponse advertises byte ranges/downloads.+ if (+ scope["method"] != "HEAD"+ and ("application/json" in content_type or "+json" in content_type)+ and "content-length" in headers+ and "content-disposition" not in headers+ and "accept-ranges" not in headers+ ):+ response_start = message+ return+ elif response_start is not None:+ if message["type"] == "http.response.body" and not message.get("more_body", False):+ try:+ payload = json.loads(message.get("body", b""))+ except (TypeError, ValueError):+ payload = None+ if isinstance(payload, dict):+ payload["profile"] = format_profile_output(profiler)+ body = JSONResponse(payload).body+ MutableHeaders(scope=response_start)["content-length"] = str(len(body))+ message = {**message, "body": body}+ # A chunked body or pathsend is passed through without buffering.+ await send(response_start)+ response_start = None+ await send(message)+profiler.enable()try:- response = await call_next(request)+ await self.app(scope, receive, send_profiled)finally:profiler.disable()-- profile_lines = format_profile_output(profiler)- return await inject_profile_into_response(response, profile_lines)-- return add_profile_outputdiff --git a/openviking/server/timing_middleware.py b/openviking/server/timing_middleware.pynew file mode 100644index 000000000..ebfae57d3--- /dev/null+++ b/openviking/server/timing_middleware.py@@ -0,0 +1,41 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""HTTP processing-time headers and request-header diagnostics."""++import logging+import time++from starlette.datastructures import MutableHeaders+from starlette.requests import Request+from starlette.types import ASGIApp, Message, Receive, Scope, Send++_access_logger = logging.getLogger("uvicorn.access")+++class RequestTimingMiddleware:+ def __init__(self, app: ASGIApp) -> None:+ self.app = app++ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:+ if scope["type"] != "http":+ await self.app(scope, receive, send)+ return++ started_at = time.perf_counter()+ if _access_logger.isEnabledFor(logging.DEBUG):+ request = Request(scope)+ _access_logger.debug(+ "Request headers for %s %s: %s",+ request.method,+ request.url.path,+ ", ".join(sorted(request.headers.keys())),+ )++ async def send_timed(message: Message) -> None:+ if message["type"] == "http.response.start":+ MutableHeaders(scope=message)["X-Process-Time"] = str(+ time.perf_counter() - started_at+ )+ await send(message)++ await self.app(scope, receive, send_timed)diff --git a/openviking/service/task_tracker_concurrency.py b/openviking/service/task_tracker_concurrency.pyindex c79cd7c37..d661a8fb8 100644--- a/openviking/service/task_tracker_concurrency.py+++ b/openviking/service/task_tracker_concurrency.py@@ -30,34 +30,34 @@ async def run_to_completion(aligned with the physical I/O lifetime."""work: asyncio.Future[_ResultT] = asyncio.ensure_future(factory())- caller = asyncio.current_task()+ caller: Any = asyncio.current_task()cancellation: asyncio.CancelledError | None = None- while not work.done():- try:- await asyncio.shield(work)- except asyncio.CancelledError as exc:- if work.cancelled():- raise- cancellation = exc- except BaseException:- # A completion exception can race with delivery of caller- # cancellation. Honour the already-requested cancellation below.- caller_with_cancellation_count: Any = caller- if (- caller is None- or not hasattr(caller, "cancelling")- or caller_with_cancellation_count.cancelling() == 0- ):- raise- cancellation = asyncio.CancelledError()-- if cancellation is not None:- if not work.cancelled():- # Caller cancellation wins once it has been observed. Consume a- # later work failure so asyncio does not report it as unhandled.- work.exception()- raise cancellation- return work.result()+ try:+ while not work.done():+ try:+ await asyncio.shield(work)+ except asyncio.CancelledError as exc:+ if work.cancelled():+ raise+ cancellation = exc+ except BaseException:+ # A completion exception can race with delivery of caller+ # cancellation. Honour the already-requested cancellation below.+ if caller is None or not hasattr(caller, "cancelling") or caller.cancelling() == 0:+ raise+ cancellation = asyncio.CancelledError()++ if cancellation is not None:+ if not work.cancelled():+ # Caller cancellation wins once it has been observed. Consume a+ # later work failure so asyncio does not report it as unhandled.+ work.exception()+ raise cancellation+ return work.result()+ finally:+ # Propagated exceptions retain this frame. Keeping either task here+ # creates a traceback cycle that also retains the finished request.+ del work, caller, cancellationclass OwnerLoopDispatcher:diff --git a/tests/metrics/integration/test_account_dimension.py b/tests/metrics/integration/test_account_dimension.pyindex cef8e078d..292c9d884 100644--- a/tests/metrics/integration/test_account_dimension.py+++ b/tests/metrics/integration/test_account_dimension.py@@ -27,8 +27,9 @@ from openviking.observability.context import (reset_root_observability_context,)from openviking.observability.http_observability_middleware import (- create_http_observability_middleware,+ HTTPObservabilityMiddleware,)+from openviking.server.request_id import RequestIdMiddlewarefrom openviking.telemetry.span_models import RootSpanAttributes@@ -38,13 +39,11 @@ def _bind_root_context_for_account(account_id: str | None):return bind_root_observability_context(root)-def _build_test_app(middleware_factory, *, route_path: str, handler):+def _build_test_app(middleware_class, *, route_path: str, handler):app = FastAPI()- @app.middleware("http")- async def middleware_entry(request, call_next):- request.state.request_id = "test-request"- return await middleware_factory(request, call_next)+ app.add_middleware(middleware_class)+ app.add_middleware(RequestIdMiddleware)app.get(route_path)(handler)return app@@ -114,7 +113,7 @@ def test_http_collector_uses_unknown_account_when_unbound():def test_http_metrics_middleware_propagates_state_account_to_collector(monkeypatch):"""HTTP middleware must attach the authenticated account id to emitted http events."""captured: list[tuple[str, dict]] = []- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewaredef _fake_emit(event_name: str, payload: dict) -> None:captured.append((event_name, dict(payload)))diff --git a/tests/metrics/integration/test_http_metrics.py b/tests/metrics/integration/test_http_metrics.pyindex 2d4cb02ba..191228780 100644--- a/tests/metrics/integration/test_http_metrics.py+++ b/tests/metrics/integration/test_http_metrics.py@@ -22,10 +22,11 @@ from openviking.observability.context import ()from openviking.observability.http_observability_middleware import (_INFLIGHT_COUNTER,+ HTTPObservabilityMiddleware,_get_route_template,_inflight_delta,- create_http_observability_middleware,)+from openviking.server.request_id import RequestIdMiddlewarefrom openviking.telemetry.span_models import RootSpanAttributes@@ -35,13 +36,11 @@ def _bind_root_context_for_account(account_id: str | None):return bind_root_observability_context(root)-def _build_test_app(middleware_factory, *, route_path: str, method: str, handler):+def _build_test_app(middleware_class, *, route_path: str, method: str, handler):app = FastAPI()- @app.middleware("http")- async def middleware_entry(request, call_next):- request.state.request_id = "test-request"- return await middleware_factory(request, call_next)+ app.add_middleware(middleware_class)+ app.add_middleware(RequestIdMiddleware)if method == "GET":app.get(route_path)(handler)@@ -156,13 +155,9 @@ def test_inflight_delta_removes_zero_value_entries():assert _INFLIGHT_COUNTER.get("/api/v1/resources", None) == 0-def test_http_metrics_module_exposes_only_unified_middleware_entrypoint():- assert not hasattr(http_middleware, "create_http_metrics_middleware")--def test_http_metrics_middleware_emits_authenticated_account_id(monkeypatch):captured: list[tuple[str, dict]] = []- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewaredef _fake_emit(event_name: str, payload: dict) -> None:captured.append((event_name, dict(payload)))@@ -203,7 +198,7 @@ def test_http_metrics_middleware_emits_authenticated_account_id(monkeypatch):def test_http_metrics_middleware_ignores_internal_metrics_route(monkeypatch):captured: list[tuple[str, dict]] = []- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewaredef _fake_emit(event_name: str, payload: dict) -> None:captured.append((event_name, dict(payload)))@@ -230,7 +225,7 @@ def test_http_metrics_middleware_ignores_internal_metrics_route(monkeypatch):def test_http_metrics_middleware_still_records_business_route(monkeypatch):captured: list[tuple[str, dict]] = []- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewaredef _fake_emit(event_name: str, payload: dict) -> None:captured.append((event_name, dict(payload)))@@ -258,7 +253,7 @@ def test_http_metrics_middleware_still_records_business_route(monkeypatch):def test_http_metrics_middleware_uses_route_bound_during_call_next(monkeypatch):captured: list[tuple[str, dict]] = []- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewaredef _fake_emit(event_name: str, payload: dict) -> None:captured.append((event_name, dict(payload)))@@ -286,7 +281,7 @@ def test_http_metrics_middleware_uses_route_bound_during_call_next(monkeypatch):def test_http_metrics_middleware_logs_error_when_metrics_write_fails(monkeypatch):- middleware = create_http_observability_middleware()+ middleware = HTTPObservabilityMiddlewareerror_calls: list[tuple[str, tuple, dict]] = []def _boom(**_kwargs):diff --git a/tests/server/test_profile_middleware.py b/tests/server/test_profile_middleware.pyindex 4dc87b930..f1dfa4145 100644--- a/tests/server/test_profile_middleware.py+++ b/tests/server/test_profile_middleware.py@@ -1,11 +1,17 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0+import asyncio+import sys+import httpximport pytestfrom fastapi import APIRouter-from fastapi.responses import PlainTextResponse, StreamingResponse+from fastapi.responses import FileResponse, JSONResponse, PlainTextResponse, StreamingResponse+from starlette.background import BackgroundTask+from openviking.metrics.datasources import HttpRequestLifecycleDataSource+from openviking.observability.context import get_root_observability_contextfrom openviking.server.app import create_appfrom openviking.server.config import ServerConfigfrom openviking.server.profile_middleware import PROFILE_TOP_N, _sanitize_profile_path@@ -40,15 +46,30 @@ def _make_test_app_with_config(config: ServerConfig):@pytest.mark.asyncioasync def test_profile_query_adds_profile_field_to_json_response():app = _make_test_app()+ completed = []++ @app.get("/profile-json")+ async def json_response():+ response = JSONResponse(+ {"status": "ok"}, background=BackgroundTask(completed.append, "done")+ )+ response.set_cookie("first", "1")+ response.set_cookie("second", "2")+ return response+transport = httpx.ASGITransport(app=app)async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:- resp = await client.get("/health", params={"profile": "1"})+ resp = await client.get("/profile-json", params={"profile": "1"})assert resp.status_code == 200body = resp.json()assert "profile" in bodyassert isinstance(body["profile"], list)+ assert int(resp.headers["content-length"]) == len(resp.content)+ assert len(resp.headers.get_list("set-cookie")) == 2+ assert completed == ["done"]+ assert float(resp.headers["x-process-time"]) >= 0assert any("ncalls" in line for line in body["profile"])assert any("cumtime" in line for line in body["profile"])assert not any(@@ -90,7 +111,7 @@ async def test_profile_query_is_ignored_when_server_profile_disabled():@pytest.mark.asyncio-async def test_profile_query_does_not_rewrite_plain_text_response():+async def test_profile_query_does_not_rewrite_plain_text_or_file_response(tmp_path):app = _make_test_app()router = APIRouter()@@ -98,38 +119,129 @@ async def test_profile_query_does_not_rewrite_plain_text_response():async def plain():return PlainTextResponse("ok")+ path = tmp_path / "download.json"+ path.write_bytes(b'{"download":true}')++ @router.get("/file")+ async def file():+ return FileResponse(path)+app.include_router(router)transport = httpx.ASGITransport(app=app)async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:resp = await client.get("/plain", params={"profile": "1"})+ file_resp = await client.get("/file", params={"profile": "1"})assert resp.status_code == 200assert resp.text == "ok"assert resp.headers["content-type"].startswith("text/plain")+ assert file_resp.content == path.read_bytes()+ assert int(file_resp.headers["content-length"]) == len(file_resp.content)@pytest.mark.asyncio-async def test_profile_query_does_not_rewrite_streaming_response():[email protected]("ending", ["complete", "disconnect", "cancel", "error"])+async def test_profile_query_preserves_stream_delivery_and_context_cleanup(ending, monkeypatch):app = _make_test_app()router = APIRouter()+ first_chunk = asyncio.Event()+ release = asyncio.Event()+ disconnect = asyncio.Event()+ generator_closed = asyncio.Event()+ messages = []+ completed_requests = []+ background = []+ monkeypatch.setattr(+ HttpRequestLifecycleDataSource,+ "record_request",+ lambda **kwargs: completed_requests.append(kwargs),+ )async def _iterator():- yield b"streamed"+ try:+ assert get_root_observability_context().request_id == "stream-request"+ yield b'{"first":true}\n'+ await release.wait()+ assert get_root_observability_context().request_id == "stream-request"+ if ending == "error":+ raise RuntimeError("stream failed after headers")+ yield b'{"last":true}\n'+ finally:+ generator_closed.set()@router.get("/stream")async def stream():- return StreamingResponse(_iterator(), media_type="text/plain")+ return StreamingResponse(+ _iterator(),+ media_type="application/json",+ background=BackgroundTask(background.append, "done"),+ )app.include_router(router)- transport = httpx.ASGITransport(app=app)-- async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:- resp = await client.get("/stream", params={"profile": "1"})-- assert resp.status_code == 200- assert resp.text == "streamed"- assert resp.headers["content-type"].startswith("text/plain")+ scope = {+ "type": "http",+ "asgi": {"version": "3.0", "spec_version": "2.3"},+ "http_version": "1.1",+ "method": "GET",+ "scheme": "http",+ "path": "/stream",+ "query_string": b"profile=1",+ "root_path": "",+ "headers": [(b"x-request-id", b"stream-request")],+ "server": ("testserver", 80),+ "client": ("testclient", 12345),+ }++ async def receive():+ await disconnect.wait()+ return {"type": "http.disconnect"}++ async def send(message):+ messages.append(message)+ if message["type"] == "http.response.body" and message.get("body"):+ first_chunk.set()++ async def serve():+ try:+ await app(scope, receive, send)+ finally:+ assert get_root_observability_context() is None+ assert sys.getprofile() is None++ task = asyncio.create_task(serve())+ try:+ await asyncio.wait_for(first_chunk.wait(), timeout=2)+ assert not task.done()+ assert len(completed_requests) == 1+ assert completed_requests[0]["status"] == "200"+ headers = dict(messages[0]["headers"])+ assert float(headers[b"x-process-time"]) >= 0+ if ending == "cancel":+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task+ elif ending == "disconnect":+ disconnect.set()+ await asyncio.wait_for(task, timeout=2)+ else:+ release.set()+ if ending == "error":+ with pytest.raises(RuntimeError, match="response already started"):+ await asyncio.wait_for(task, timeout=2)+ else:+ await asyncio.wait_for(task, timeout=2)+ assert background == ["done"]+ assert generator_closed.is_set()+ assert len(completed_requests) == 1+ body = b"".join(m.get("body", b"") for m in messages)+ assert body == (+ b'{"first":true}\n{"last":true}\n' if ending == "complete" else b'{"first":true}\n'+ )+ finally:+ if not task.done():+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)def test_sanitize_profile_path_prefers_package_root_over_project_root_for_venv_packages():diff --git a/tests/server/test_request_id.py b/tests/server/test_request_id.pyindex ab03406e7..87a69e577 100644--- a/tests/server/test_request_id.py+++ b/tests/server/test_request_id.py@@ -6,18 +6,20 @@from __future__ import annotationsimport asyncio+import gcimport logging+import weakreffrom types import SimpleNamespacefrom uuid import UUIDimport httpx-from fastapi import FastAPI+from fastapi import FastAPI, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom openviking.metrics.datasources import HttpRequestLifecycleDataSourcefrom openviking.observability.context import get_root_observability_contextfrom openviking.observability.http_observability_middleware import (- create_http_observability_middleware,+ HTTPObservabilityMiddleware,)from openviking.server.app import create_appfrom openviking.server.config import ServerConfig@@ -35,12 +37,7 @@ class _CaptureHandler(logging.Handler):def _make_test_app() -> FastAPI:app = FastAPI()- observability_middleware = create_http_observability_middleware()-- @app.middleware("http")- async def add_observability(request, call_next):- return await observability_middleware(request, call_next)-+ app.add_middleware(HTTPObservabilityMiddleware)app.add_middleware(RequestIdMiddleware)app.add_middleware(CORSMiddleware,@@ -62,19 +59,33 @@ def _assert_generated_request_id(value: str) -> None:assert UUID(value).version == 4-async def test_client_request_id_is_returned_and_reused_by_observability() -> None:+async def test_request_context_is_reused_and_released_after_response() -> None:app = _make_test_app()+ payload_refs = []++ class RequestPayload:+ pass@app.get("/items/{item_id}")- async def item(item_id: str):+ async def item(item_id: str, request: Request):+ request.state.payload = RequestPayload()+ payload_refs.append(weakref.ref(request.state.payload))root = get_root_observability_context()return {"item_id": item_id, "observability_request_id": root.request_id}- response = await _request(- app,- "/items/42",- headers={REQUEST_ID_HEADER: "resource-import-20260728-001"},- )+ gc_was_enabled = gc.isenabled()+ gc.disable()+ try:+ response = await _request(+ app,+ "/items/42",+ headers={REQUEST_ID_HEADER: "resource-import-20260728-001"},+ )+ assert get_root_observability_context() is None+ assert payload_refs[0]() is None+ finally:+ if gc_was_enabled:+ gc.enable()assert response.status_code == 200assert response.headers[REQUEST_ID_HEADER] == "resource-import-20260728-001"diff --git a/tests/telemetry/test_http_observability_middleware.py b/tests/telemetry/test_http_observability_middleware.pyindex 9a1863cb9..e19a6db15 100644--- a/tests/telemetry/test_http_observability_middleware.py+++ b/tests/telemetry/test_http_observability_middleware.py@@ -27,10 +27,10 @@ class _DummySpanCM:def test_http_observability_middleware_updates_route_template_after_routing(monkeypatch) -> None:"""- Ensure `http_route` is finalized after routing has occurred (post-call_next).+ Ensure `http_route` is finalized after routing has occurred (before response headers).Starlette/FastAPI route matching happens downstream of middleware, so reading- `request.scope["route"]` before `call_next` may yield no route.+ `request.scope["route"]` before routing may yield no route."""from openviking.observability import http_observability_middleware as mw_mod@@ -60,12 +60,8 @@ def test_http_observability_middleware_updates_route_template_after_routing(monkmonkeypatch.setattr(mw_mod, "create_root_span_attributes", _capture_create_root_span_attributes)app = FastAPI()- http_mw = mw_mod.create_http_observability_middleware()-- @app.middleware("http")- async def _mw(request, call_next):- request.state.request_id = "test-request"- return await http_mw(request, call_next)+ app.add_middleware(mw_mod.HTTPObservabilityMiddleware)+ app.add_middleware(RequestIdMiddleware)@app.get("/hello")async def hello():@@ -97,12 +93,7 @@ def test_http_observability_captures_public_error_without_reading_response_body(monkeypatch.setattr(mw_mod, "maybe_start_root_span", lambda *_: None)app = FastAPI()- http_mw = mw_mod.create_http_observability_middleware()-- @app.middleware("http")- async def _mw(request, call_next):- return await http_mw(request, call_next)-+ app.add_middleware(mw_mod.HTTPObservabilityMiddleware)app.add_middleware(RequestIdMiddleware)@app.get("/invalid")diff --git a/tests/test_task_tracker_concurrency.py b/tests/test_task_tracker_concurrency.pyindex 66a28b294..e95c4d2b2 100644--- a/tests/test_task_tracker_concurrency.py+++ b/tests/test_task_tracker_concurrency.py@@ -2,7 +2,9 @@# SPDX-License-Identifier: AGPL-3.0import asyncio+import gcimport threading+import weakreffrom copy import deepcopyfrom typing import Any@@ -425,24 +427,51 @@ def test_store_io_limiter_rejects_non_positive_limit():@pytest.mark.asyncio-async def test_run_to_completion_preserves_caller_cancellation_when_work_fails_later():[email protected]("cancel", [False, True])+async def test_run_to_completion_releases_failed_request_and_preserves_cancellation(cancel):from openviking.service.task_tracker_concurrency import run_to_completionstarted = asyncio.Event()release = asyncio.Event()+ class RequestPayload:+ pass+async def fail_later() -> None:started.set()await release.wait()raise RuntimeError("late store failure")- operation = asyncio.create_task(run_to_completion(fail_later))- await started.wait()- operation.cancel()- release.set()-- with pytest.raises(asyncio.CancelledError):- await operation+ async def request():+ payload = RequestPayload()+ try:+ await run_to_completion(fail_later)+ except (asyncio.CancelledError, RuntimeError) as exc:+ assert type(exc) is (asyncio.CancelledError if cancel else RuntimeError)+ if not cancel:+ assert str(exc) == "late store failure"+ else:+ pytest.fail("The operation must propagate its failure or caller cancellation")+ return weakref.ref(payload)++ # Finished requests must be released without waiting for cyclic GC.+ gc_was_enabled = gc.isenabled()+ gc.disable()+ try:+ operation = asyncio.create_task(request())+ await started.wait()+ if cancel:+ operation.cancel()+ await asyncio.sleep(0)+ assert not operation.done()+ release.set()+ payload_ref = await operation+ await asyncio.sleep(0)+ assert payload_ref() is None+ finally:+ release.set()+ if gc_was_enabled:+ gc.enable()@pytest.mark.asyncio