Cannot run the event loop while another loop is running #12081

Closed
opened 2026-02-21 19:05:40 -05:00 by yindo · 2 comments
Owner

Originally created by @lijiaqi0121 on GitHub (Mar 25, 2025).

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

0.15.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I created a chatflow and set up text-to-speech (TTS) and autoplay, so that when I call the chat-message interface, it can return both the message event and the tts_message event. However, I occasionally encounter situations where the tts_message event is not returned. In the returned stream, there is only the tts_message_end event but no tts_message

The following is the error log content from dify-api-1:

Exception in thread Thread-3323 (invoke_remote):
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.12/threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "/app/api/core/model_runtime/model_providers/tongyi/tts/tts.py", line 83, in invoke_remote
    SpeechSynthesizer.call(
  File "/app/api/.venv/lib/python3.12/site-packages/dashscope/audio/tts/speech_synthesizer.py", line 146, in call
    for part in response:
                ^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/dashscope/api_entities/websocket_request.py", line 71, in <genexpr>
    return (item for item in response)
                             ^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/dashscope/common/utils.py", line 96, in async_to_sync
    for message in iter_over_async(async_generator, loop):
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/dashscope/common/utils.py", line 86, in iter_over_async
    done, obj = loop.run_until_complete(get_next())
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 667, in run_until_complete
    self._check_running()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 628, in _check_running
    raise RuntimeError(
RuntimeError: Cannot run the event loop while another loop is running

✔️ Expected Behavior

The chat-message interface is capable of returning both the message and tts_message events

Actual Behavior

Now it only returns message and tts_message_end

Originally created by @lijiaqi0121 on GitHub (Mar 25, 2025). ### 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 0.15.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I created a chatflow and set up text-to-speech (TTS) and autoplay, so that when I call the chat-message interface, it can return both the message event and the tts_message event. However, I occasionally encounter situations where the tts_message event is not returned. In the returned stream, there is only the tts_message_end event but no tts_message The following is the error log content from dify-api-1: ``` Exception in thread Thread-3323 (invoke_remote): Traceback (most recent call last): File "/usr/local/lib/python3.12/threading.py", line 1075, in _bootstrap_inner self.run() File "/usr/local/lib/python3.12/threading.py", line 1012, in run self._target(*self._args, **self._kwargs) File "/app/api/core/model_runtime/model_providers/tongyi/tts/tts.py", line 83, in invoke_remote SpeechSynthesizer.call( File "/app/api/.venv/lib/python3.12/site-packages/dashscope/audio/tts/speech_synthesizer.py", line 146, in call for part in response: ^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/dashscope/api_entities/websocket_request.py", line 71, in <genexpr> return (item for item in response) ^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/dashscope/common/utils.py", line 96, in async_to_sync for message in iter_over_async(async_generator, loop): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/dashscope/common/utils.py", line 86, in iter_over_async done, obj = loop.run_until_complete(get_next()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/asyncio/base_events.py", line 667, in run_until_complete self._check_running() File "/usr/local/lib/python3.12/asyncio/base_events.py", line 628, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ``` ### ✔️ Expected Behavior The chat-message interface is capable of returning both the message and tts_message events ### ❌ Actual Behavior Now it only returns message and tts_message_end
yindo added the 🐞 bug label 2026-02-21 19:05:40 -05:00
yindo closed this issue 2026-02-21 19:05:40 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 25, 2025):

I couldn't find a specific solution to the "Cannot run the event loop while another loop is running" error in the Dify repository. However, this error typically occurs when trying to run an asyncio event loop within an already running loop, which is common in environments like Jupyter notebooks or when using certain frameworks.

To address this issue, you might consider the following approaches:

  1. Use nest_asyncio: This package allows you to nest event loops, which can be helpful in environments where an event loop is already running. You can install it via pip and use it as follows:

    import nest_asyncio
    nest_asyncio.apply()
    
  2. Refactor Code: If possible, refactor your code to ensure that the event loop is only started once. This might involve restructuring your code to avoid calling asyncio.run() or similar functions multiple times.

  3. Check for Existing Event Loops: Before starting a new event loop, check if one is already running and use it instead.

If these suggestions don't resolve the issue, you might want to look into how the async_to_sync and iter_over_async functions are being used in your code, as they are involved in converting asynchronous generators to synchronous iterations, which might be contributing to the problem.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Mar 25, 2025): <!-- Answer --> I couldn't find a specific solution to the "Cannot run the event loop while another loop is running" error in the Dify repository. However, this error typically occurs when trying to run an asyncio event loop within an already running loop, which is common in environments like Jupyter notebooks or when using certain frameworks. To address this issue, you might consider the following approaches: 1. **Use `nest_asyncio`**: This package allows you to nest event loops, which can be helpful in environments where an event loop is already running. You can install it via pip and use it as follows: ```python import nest_asyncio nest_asyncio.apply() ``` 2. **Refactor Code**: If possible, refactor your code to ensure that the event loop is only started once. This might involve restructuring your code to avoid calling `asyncio.run()` or similar functions multiple times. 3. **Check for Existing Event Loops**: Before starting a new event loop, check if one is already running and use it instead. If these suggestions don't resolve the issue, you might want to look into how the `async_to_sync` and `iter_over_async` functions are being used in your code, as they are involved in converting asynchronous generators to synchronous iterations, which might be contributing to the problem. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/232c7ba9-1487-463c-8a81-89f7299984b8?feedback_type=other)</sup>
Author
Owner

@love-plaid-shirt commented on GitHub (Apr 27, 2025):

我和你情况一样,也是用的0.15.3。还在寻找答案。

@love-plaid-shirt commented on GitHub (Apr 27, 2025): 我和你情况一样,也是用的0.15.3。还在寻找答案。
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#12081