Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
jeongsu-an opened this issue Oct 27, 2024 · 2 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@jeongsu-an
Copy link

jeongsu-an commented Oct 27, 2024

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

@spokeydokeys
Copy link
Contributor

I'm suffering from this bug in github workflows. The workflow stalls at the end of my script and if I print out threads at that point there is a non-daemon, AnyIO thread that is left over and not closing.

I tested @jeongsu-an's PR branch and it fixed the issue and did not introduce any other issues.

@RobertCraigie
Copy link
Collaborator

This will be fixed in #1872! Thanks to @spokeydokeys :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants