Failed to detach context #13184

Closed
opened 2026-02-21 19:10:57 -05:00 by yindo · 5 comments
Owner

Originally created by @laipz8200 on GitHub (Apr 21, 2025).

Originally assigned to: @laipz8200 on GitHub.

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.2.0

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

  1. Create a workflow
  2. Remove it, see celery logs

✔️ Expected Behavior

task finished

Actual Behavior

2025-04-21 13:25:49,391 DEBUG [__init__.py:193]  postrun signal task_id=3c40ca04-b83b-414e-bc3a-a23a6eec7159
2025-04-21 13:25:49,391 ERROR [__init__.py:156]  Failed to detach context
Traceback (most recent call last):
  File "/dify/api/.venv/lib/python3.11/site-packages/opentelemetry/context/__init__.py", line 154, in detach
    _RUNTIME_CONTEXT.detach(token)
  File "/dify/api/.venv/lib/python3.11/site-packages/opentelemetry/context/contextvars_context.py", line 50, in detach
    self._current_context.reset(token)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected an instance of Token, got None
Originally created by @laipz8200 on GitHub (Apr 21, 2025). Originally assigned to: @laipz8200 on GitHub. ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.2.0 ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce 1. Create a workflow 2. Remove it, see celery logs ### ✔️ Expected Behavior task finished ### ❌ Actual Behavior ``` 2025-04-21 13:25:49,391 DEBUG [__init__.py:193] postrun signal task_id=3c40ca04-b83b-414e-bc3a-a23a6eec7159 2025-04-21 13:25:49,391 ERROR [__init__.py:156] Failed to detach context Traceback (most recent call last): File "/dify/api/.venv/lib/python3.11/site-packages/opentelemetry/context/__init__.py", line 154, in detach _RUNTIME_CONTEXT.detach(token) File "/dify/api/.venv/lib/python3.11/site-packages/opentelemetry/context/contextvars_context.py", line 50, in detach self._current_context.reset(token) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: expected an instance of Token, got None ```
yindo added the 🐞 bug label 2026-02-21 19:10:57 -05:00
yindo closed this issue 2026-02-21 19:10:57 -05:00
Author
Owner

@TedaLIEz commented on GitHub (Apr 26, 2025):

Hi @laipz8200 , The patch here is fine as a safe guard. But I can't repro this error if I turn OpenTelemetry on for both API and worker mode even if I turned ext_otel_patch.py off.

We can further discuss this, I believe there is a discussion on this in https://github.com/open-telemetry/opentelemetry-python/issues/4163#issuecomment-2329836974

@TedaLIEz commented on GitHub (Apr 26, 2025): Hi @laipz8200 , The patch here is fine as a safe guard. But I can't repro this error if I turn OpenTelemetry on for both API and worker mode even if I turned `ext_otel_patch.py` off. We can further discuss this, I believe there is a discussion on this in https://github.com/open-telemetry/opentelemetry-python/issues/4163#issuecomment-2329836974
Author
Owner

@TedaLIEz commented on GitHub (Apr 26, 2025):

Here are the steps I take:

  1. Update the .env setting to turn OpenTelemetry with ENABLE_OTEL=true
  2. Run API with uv run flask run --host 0.0.0.0 --port=5001 --debug
  3. Run worker with uv run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
  4. Create a workflow
  5. Remote the workflow, check logs in both API and celery
@TedaLIEz commented on GitHub (Apr 26, 2025): Here are the steps I take: 1. Update the `.env` setting to turn OpenTelemetry with `ENABLE_OTEL=true` 2. Run API with `uv run flask run --host 0.0.0.0 --port=5001 --debug` 3. Run worker with `uv run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion` 4. Create a workflow 5. Remote the workflow, check logs in both API and celery
Author
Owner

@laipz8200 commented on GitHub (Apr 27, 2025):

  1. ENABLE_OTEL=true

Could you please try removing this configuration? It should help you reproduce the issue.

@laipz8200 commented on GitHub (Apr 27, 2025): > 1. ENABLE_OTEL=true Could you please try removing this configuration? It should help you reproduce the issue.
Author
Owner

@laipz8200 commented on GitHub (Apr 27, 2025):

By the way, as an optional feature, if ENABLE_OTEL is not set to true, the import should not be executed. Please refer to https://github.com/langgenius/dify/blob/56e15d09a9e94de963dd5fc38c7455a7aaee99f3/api/extensions/ext_sentry.py#L8

@laipz8200 commented on GitHub (Apr 27, 2025): By the way, as an optional feature, if ENABLE_OTEL is not set to true, the import should not be executed. Please refer to https://github.com/langgenius/dify/blob/56e15d09a9e94de963dd5fc38c7455a7aaee99f3/api/extensions/ext_sentry.py#L8
Author
Owner

@TedaLIEz commented on GitHub (Apr 27, 2025):

I see, will create a new PR to fix both. Thanks.

@TedaLIEz commented on GitHub (Apr 27, 2025): I see, will create a new PR to fix both. Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#13184