fix: flush pending spans before HTTP response to prevent loss in hosted sandboxes#46154
Merged
ankitbko merged 2 commits intoagentserver/responsesfrom Apr 7, 2026
Merged
Conversation
…ed sandboxes BatchSpanProcessor exports spans on a background timer (default 5s). In hosted sandbox environments the platform may suspend the process immediately after the HTTP response is sent, before the timer fires. This causes short-lived spans — such as LangGraph per-node invoke_agent spans created by third-party tracers — to be lost. Add flush_spans() to the core public API and call it: - In _endpoint_handler.py's finally block (covers all non-streaming exits) - In trace_stream's finally block (covers the streaming path) Locally verified: same agent code produces 30 spans with flush vs 11 without flush, confirming the BatchSpanProcessor timing issue. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ankitbko
requested changes
Apr 7, 2026
| AgentServerHost, | ||
| create_error_response, | ||
| end_span, | ||
| flush_spans, |
Member
There was a problem hiding this comment.
Please move all the core package changes to https://github.com/Azure/azure-sdk-for-python/pull/46127/changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BatchSpanProcessorexports spans on a background timer (default 5 seconds). In hosted sandbox environments (Azure AI Foundry vNext), the platform may suspend the process immediately after an HTTP response is sent, before the batch timer fires. This causes short-lived spans to be lost.What gets lost: Per-node
invoke_agentspans from LangGraph/LangChain auto-instrumentation (<1ms each) that end just before the response is returned.What survives without the fix:
chatspans (3-8s) andexecute_toolspans — these end during graph execution while subsequent LLM calls create enough wall-clock delay for the batch timer to fire.Before vs After
Before (without
force_flush) — 11 spansAll per-node
invoke_agentspans (user_proxy,orchestrator,draft_plan,run_tools,finalize, etc.) are missing — lost in theBatchSpanProcessorbuffer when the sandbox suspended.After (with
force_flush) — 19 spans ✅Full graph-node hierarchy preserved. For traces with replan loops (budget exceeded), span count grows to 31 with the full replan path visible.
Hosted validation (5 invokes, same agent image)
Paris and Rome trigger a replan loop (budget exceeded → replan → re-run tools), producing 31 spans.
Fix
flush_spans()to theazure-ai-agentserver-corepublic API (_tracing.py)_endpoint_handler.pyfinally block (covers all non-streaming exit paths)trace_stream()finally block (covers the streaming path)force_flushChanges
core/_tracing.pyflush_spans()function + call fromtrace_streamcore/__init__.pyflush_spansresponses/hosting/_endpoint_handler.pyflush_spans()in finally blockEnvironment
naarkalg-langgraph-travel-agent:1onhosted-agents-evals-bugbash-wus2hostedagentsevals.azurecr.io/naarkalg-langgraph-travel-agent:20260406112724langchain-azure-ai[opentelemetry]fromlangchain-ai/langchain-azure@maingpt-4o