openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
23.16k stars 3.27k forks source link

AnyIO worker threads not terminating when using asyncify with get_platform #1826

Closed jeongsuAn closed 4 weeks ago

jeongsuAn commented 4 weeks ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

When using the OpenAI Python library (version 1.52.2), I encountered an issue where AnyIO worker threads are not terminating properly after asynchronous requests. This leads to tests hanging and not exiting when using pytest. The problem occurs because the library utilizes asyncify(get_platform) to call the synchronous get_platform function in an asynchronous context, causing AnyIO to create non-daemon worker threads that remain alive after the request completes.

To Reproduce

  1. Use the OpenAI Python library in an asynchronous environment.
  2. Make an asynchronous API request that triggers the _request method.
  3. Run tests using pytest.
  4. Observe that after the tests complete, the process hangs and does not exit due to lingering AnyIO worker threads.

Code snippets

The issue arises in the _request method where asyncify is used:

In openai/_openai_client.py


async def _request(
    self,
    cast_to: Type[ResponseT],
    options: FinalRequestOptions,
    *,
    stream: bool,
    stream_cls: type[_AsyncStreamT] | None,
    retries_taken: int,
) -> ResponseT | _AsyncStreamT:
    if self._platform is None:
        # `get_platform` can make blocking IO calls so we
        # execute it earlier while we are in an async context
        self._platform = await asyncify(get_platform)()
    # ... rest of the method ...

Expected behavior

The AnyIO worker threads should terminate after completing their tasks, allowing the program or tests to exit cleanly without hanging.



### OS

macOS

### Python version

Python 3.12.4

### Library version

1.52.2