Repository navigation
Fix Laminar span stack warning in LocalConversation - #1039
Conversation
Fixes #1038: Prevents double-ending of tracing spans that caused 'Attempted to end active span, but stack is empty' warnings. The issue occurred because: 1. Span was started in __init__ when observability is enabled 2. Span was ended in run()'s finally block 3. Span was ended again in close() during cleanup/garbage collection Changes: - Added _span_ended flag to track span state - Check should_enable_observability() before ending spans - Prevent double span ending in both run() and close() methods Tests: - Added comprehensive test suite to verify fix - All existing tests continue to pass - No more span warnings in examples Co-authored-by: openhands <openhands@all-hands.dev>
Coverage Report •
|
|||||||||||||||||||||||||||||||||||
|
@OpenHands Does this issue exist in any other conversation type? |
|
I'm on it! jpshackelford can track my progress at all-hands.dev |
SummaryI investigated whether the Laminar span double-ending issue exists in other conversation types beyond LocalConversation. Answer: Yes, RemoteConversation has the same issue. Analysis ResultsLocalConversation (Fixed in current PR):
RemoteConversation (Still has the issue):
The Problem Pattern in RemoteConversation
RecommendationRemoteConversation should implement the same fix as LocalConversation:
This would prevent the same span warnings when using RemoteConversation instances. |
…all conversation types - Moved span management logic from LocalConversation to BaseConversation - Added _span_ended tracking and proper observability checks to base class - Refactored LocalConversation to use base class span management methods - Refactored RemoteConversation to use base class span management methods - Eliminates code duplication and ensures consistent span handling - Fixes span double-ending warnings in both Local and Remote conversations - Added comprehensive test suite for BaseConversation span management - All 229 conversation tests passing Co-authored-by: openhands <openhands@all-hands.dev>
🔄 Improved Solution: Refactored to BaseConversationAfter investigating @jpshackelford's question about whether this issue exists in other conversation types, I discovered that RemoteConversation had the same double-ending problem. This led to a much better solution. 🔍 What I FoundRemoteConversation also had the span double-ending issue:
🏗️ Better Architecture: Centralized Span ManagementInstead of fixing each conversation type individually, I moved the span management logic to BaseConversation Changes:class BaseConversation(ABC):
def __init__(self):
self._span_ended = False
self._start_observability_span()
def _start_observability_span(self):
if should_enable_observability():
start_active_span("conversation")
def _end_observability_span(self):
if not self._span_ended and should_enable_observability():
end_active_span()
self._span_ended = TrueConversation Implementations:
✅ Benefits of This Approach
🧪 Testing
📊 ImpactBefore: Only LocalConversation was fixed, RemoteConversation still had the issue This refactoring provides a much more robust and maintainable solution that addresses the root cause rather than just the symptoms. 🎯 |
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like Feel free to include any additional details that might help me get this PR into a better state. You can manage your notification settings |
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| """Initialize the base conversation with span tracking.""" |
There was a problem hiding this comment.
I wonder if this works normally with pydantic validation and serialization/deserialization. It should? But I'm a little unsure about redefining __init__ when we rely on some init/restore magic. 😓
Maybe we could run all examples, including remote, to see all is good? I think we have a new workflow for that... WDYT?
There was a problem hiding this comment.
I have started the run here: https://github.com/OpenHands/software-agent-sdk/actions/runs/19106657774
|
@OpenHands Do a /codereview-roasted on this PR. Think deeply and follow execution paths, specially regarding conversation factory and agent-server conversation persistence. When you are done, respond using some markdown, some bold, some headings, bulleted lists, because it's more readable. |
|
I'm on it! enyst can track my progress at all-hands.dev |
Summary of /codereview-roasted on PR #1039What I did
Key Findings (high impact)
Additional Suggestions (lower impact)
Test Signals Observed
Recommended Next Steps
If you want, I can implement these minimal, targeted changes on this branch. |
Co-authored-by: openhands <openhands@all-hands.dev>
…6 scoping Three honest annotations, no behavior change: - LocalConversation seed: note it runs for *every* agent (regular and ACP), not just ACP — it covers callers that skip create_request() (canvas / TypeScript and the server-side agent_settings -> create_agent fold) and is idempotent with the create_request() lift. - get_all_secrets_as_env_vars: state that it injects the whole registry and that least-privilege scoping is deferred to #1039 task 6. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
Fixes #1038: Prevents double-ending of tracing spans that caused "Attempted to end active span, but stack is empty" warnings during LocalConversation cleanup.
Problem
When running SDK examples (or any code using
LocalConversation), warning messages appeared about attempting to end an active span when the stack is empty:Root Cause
The issue was caused by double-ending of tracing spans in the
LocalConversationlifecycle:__init__(line 152) when observability is enabledrun()'s finally block (line 312)close()(line 397)__del__callsclose()during garbage collection, it tries to end an already-ended spanSolution
Added proper span state tracking to prevent double-ending:
_span_endedflag to track whether the span has already been endedshould_enable_observability()before attempting to end spansrun()andclose()methodsChanges
Core Fix
openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:_span_ended: boolfield to track span state_span_ended = Falsein__init__not self._span_ended and should_enable_observability()before callingend_active_span()_span_ended = Trueafter ending spanTests
tests/sdk/conversation/local/test_span_double_ending.py:Testing
Verification
Before fix:
After fix:
Impact
LocalConversation@jpshackelford can click here to continue refining the PR
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.12-nodejs22golang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:b4f42ef-pythonRun
All tags pushed for this build
About Multi-Architecture Support
b4f42ef-python) is a multi-arch manifest supporting both amd64 and arm64b4f42ef-python-amd64) are also available if needed