Skip to content

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

@jeongsu-an

Description

@jeongsu-an

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

  • This is an issue with the Python library

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

Use the OpenAI Python library in an asynchronous environment.
Make an asynchronous API request that triggers the _request method.
Run tests using pytest.
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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions