Skip to content

Conversation

@DJRHails
Copy link

@DJRHails DJRHails commented Dec 14, 2025

Quick claude code PR if it's helpful; just using this locally when I ran into an issue; threadpool feels a bit heavyweight, but it solved my issue.


Problem

When using VCRpy with httpcore and pytest-asyncio (especially with Python 3.13+), the following error occurs:

RuntimeError: await wasn't used with future

This happens because when pytest-asyncio already has an event loop running, _run_async_function uses asyncio.ensure_future() which returns a Future object but never awaits it. The synchronous caller then receives a Future instead of the actual result, causing the RuntimeError.

Solution

This PR fixes the issue by running the async function in a new thread with its own event loop when an event loop is already running. This ensures the synchronous caller receives the actual result instead of a Future object.

The fix uses concurrent.futures.ThreadPoolExecutor to:

  1. Detect if an event loop is already running
  2. If yes, spawn a new thread with its own event loop
  3. Run the async function in that thread
  4. Block and return the result to the synchronous caller

When pytest-asyncio has an event loop running, the original code used
asyncio.ensure_future() which returns a Future but never awaits it,
causing 'RuntimeError: await wasn't used with future'.

This fix runs the async function in a new thread with its own event
loop when an event loop is already running, ensuring the synchronous
caller gets the actual result instead of a Future object.

Fixes compatibility with Python 3.13+ and pytest-asyncio.
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.

1 participant