Skip to content

flask: Context Detach Failure in Streaming Responses #3937

@fatelei

Description

@fatelei

Describe your environment

OS: (e.g, Ubuntu)
Python version: 3.9.10
Package version: 0.48b0

What happened?

langgenius/dify#25673

2025-09-14 13:36:34.608 ERROR [Dummy-16] [__init__.py:156] - Failed to detach context
Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.12/site-packages/opentelemetry/context/__init__.py", line 154, in detach
    _RUNTIME_CONTEXT.detach(token)
  File "/app/api/.venv/lib/python3.12/site-packages/opentelemetry/context/contextvars_context.py", line 50, in detach
    self._current_context.reset(token)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: <Token used var=<ContextVar name='current_context' default={} at 0x7f1507563470> at 0x7f1505b11b40> has already been used once

Steps to Reproduce

  1. Create an advanced chat application with a workflow in Dify
  2. Run Workflow In Console
  3. Check the application logs for OpenTelemetry errors

Expected Result

no error

Actual Result

raise runtime error

Additional context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions