Skip to content

Conversation

fengju0213
Copy link
Collaborator

Description

Root cause

Synchronous functions wrapped by with_timeout use a new thread (thread + join) when a timeout is set.
If such functions call asyncio.create_task(...), the new thread has no running event loop, causing RuntimeError: no running event loop and downstream confusing errors.

Fix

In the sync branch of with_timeout, detect a running event loop via asyncio.get_running_loop().
If a loop is running, execute inline (no thread switch, no sync timeout) to preserve the asyncio context.
If no loop is running, keep the original behavior: spawn a thread and enforce timeout with join.

Checklist

Go over all the following points, and put an x in all the boxes that apply.

  • I have read the CONTRIBUTION guide (required)
  • I have linked this PR to an issue using the Development section on the right sidebar or by adding Fixes #issue-number in the PR description (required)
  • I have checked if any dependencies need to be added or updated in pyproject.toml and uv lock
  • I have updated the tests accordingly (required for a bug fix or a new feature)
  • I have updated the documentation if needed:
  • I have added examples if this is a new feature

If you are unsure about any of these, don't hesitate to ask. We are here to help!

Copy link
Contributor

coderabbitai bot commented Sep 26, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch enhance_with_timeout

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Wendong-Fan Wendong-Fan enabled auto-merge (squash) October 5, 2025 10:16
@Wendong-Fan Wendong-Fan disabled auto-merge October 5, 2025 10:19
@Wendong-Fan Wendong-Fan merged commit de59078 into master Oct 5, 2025
11 of 12 checks passed
@Wendong-Fan Wendong-Fan deleted the enhance_with_timeout branch October 5, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants