Skip to content

Failed to detach context/Token has already been used once errors with instrument_anthropic() #1607

@francisjervis

Description

@francisjervis

Description

I am getting intermittent (apparently non-deterministic) opentelemetry errors when instrumenting the Anthropic client with Logfire inside a Flask app. This may well be the same issue discussed here: open-telemetry/opentelemetry-python#3919 based on the error logs posted there.

ERROR:opentelemetry.context:Failed to detach context
Traceback (most recent call last):
  File "./.venv/lib/python3.11/site-packages/flask/helpers.py", line 132, in generator
    yield from gen
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./.venv/lib/python3.11/site-packages/opentelemetry/context/__init__.py", line 155, in detach
    _RUNTIME_CONTEXT.detach(token)
  File "..venv/lib/python3.11/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach
    self._current_context.reset(token)
RuntimeError: <Token used var=<ContextVar name='current_context' default={} at 0x10cbb99e0> at 0x163a1ef40> has already been used once

Python, Logfire & OS Versions, related packages (not required)

Python 3.11, Mac OS 26.2 (25C56)

Logfire 4.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions