OTEL context token detached across asyncio/thread boundary in memory client + Strands session_manager
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- python
- Domain
- observability
Research direction
Start in bedrock_agentcore/memory/client.py around create_event and client.py:484, then inspect bedrock_agentcore/memory/integrations/strands/session_manager.py around line 406. Trace each OpenTelemetry attach/detach pair across async and thread boundaries, and reproduce the reported runtime logging. Done means the memory and Strands flows retain tracing without Failed to detach context errors.
Written by the indexing model from the issue text.
Description
Summary
When using bedrock-agentcore (1.6.2) with the Strands integration inside an AgentCore runtime, the OpenTelemetry runtime logs a steady stream of Failed to detach context errors:
ERROR [opentelemetry.context] [__init__.py:157] - Failed to detach context
Traceback (most recent call last):
File "/var/task/opentelemetry/context/__init__.py", line 155, in detach
_RUNTIME_CONTEXT.detach(token)
File "/var/task/opentelemetry/context/contextvars_context.py", line 53, in detach
self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x...> at 0x...> was created in a different Context
The errors are logged from spans owned by:
bedrock_agentcore.memory.client(aroundcreate_event/client.py:484)bedrock_agentcore.memory.integrations.strands.session_manager(session_manager.py:406, aroundCreated agent: default in session: …)
Functionally tracing still works (events are created, agents start), but ERROR-level log spam pollutes CloudWatch and obscures real failures.
Root cause (best read)
The SDK calls opentelemetry.context.attach(ctx) to set span context, then opentelemetry.context.detach(token) to restore. Per PEP 567, every asyncio.Task (and every threadpool worker) gets its own copy of the contextvars state, so a token created in one execution context is not valid to reset() in another.
This shows up specifically when:
- A boto3 sync call (e.g.,
CreateEvent) is invoked from inside anasyncmethod and ends up resuming on a different thread / task than the one that calledattach. - A Strands session manager hook (e.g.
Created agentevent) attaches in one task and detaches after anawaitresumes on another task.
Repro environment
bedrock-agentcore==1.6.2bedrock-agentcore-starter-toolkit==0.3.5- AgentCore-managed runtime (Lambda-style),
aws-opentelemetry-distroenabled - Strands
Agentconstructed withAgentCoreMemorySessionManager - Concurrent module-builder agents launched from a
ThreadPoolExecutor
Expected
detach() should not raise (or the SDK should not call it across context boundaries). Either:
- Use
with use_span(...)/set_valuepatterns that don't require explicitdetach, or - Capture the originating context and only detach if the current context still owns the token, or
- Wrap the detach in a try/except
ValueErrorsince a stale token can be safely ignored.
Workaround consumers can apply
from opentelemetry import context as _ctx
_orig_detach = _ctx.detach
def _safe_detach(token):
try:
_orig_detach(token)
except ValueError:
pass
_ctx.detach = _safe_detach
…installed before importing bedrock_agentcore. This is purely cosmetic and should live in the SDK instead.
Suggested fix
In bedrock_agentcore.memory.client and bedrock_agentcore.memory.integrations.strands.session_manager, wrap each context.detach(token) call with a try/except ValueError (or refactor to use opentelemetry.trace.use_span so no manual token management is needed across await boundaries).
- Dominant language
- Python
- Stars
- 764
- Forks
- 149
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/bedrock-agentcore-sdk-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/bedrock-agentcore-sdk-python#496 · 1 comment ·
-
Pydantic deprecation warning: Support for class-based `config` is deprecated, use ConfigDict instead Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
aws/bedrock-agentcore-sdk-python#320 · 2 comments · 3 reactions ·
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
All issues in aws/bedrock-agentcore-sdk-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100