- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.4k
Description
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
On Windows, if you try getting chat completions the openai library will occasionally throw an error saying RuntimeError: Event loop is closed. This seems to originate from httpx trying to close the connection. I noticed it primarily happens on short lived async functions that are run in rapid succession. It also only seems to happen if the AsyncOpenAI client is created multiple times. If it's created once (like outside the loop in the example code below) then the issue doesn't happen. I believe this may or may not be related to httpx's connection pooling trying to reuse a connection that no longer exists. Here is a related issue from httpx's repo.
Here are the logs from the exception in the code snippet provided in this issue.
Task exception was never retrieved
future: <Task finished name='Task-6' coro=<AsyncClient.aclose() done, defined at c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpx\_client.py:2011> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpx\_client.py", line 2018, in aclose
    await self._transport.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpx\_transports\default.py", line 385, in aclose
    await self._pool.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 313, in aclose
    await self._close_connections(closing_connections)
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 305, in _close_connections
    await connection.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpcore\_async\connection.py", line 171, in aclose
    await self._connection.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpcore\_async\http11.py", line 265, in aclose
    await self._network_stream.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\httpcore\_backends\anyio.py", line 54, in aclose
    await self._stream.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\anyio\streams\tls.py", line 202, in aclose
    await self.transport_stream.aclose()
  File "c:\Users\Ethan\tonic_validate_tester\venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 1191, in aclose
    self._transport.close()
  File "C:\Users\Ethan\AppData\Local\Programs\Python\Python311\Lib\asyncio\proactor_events.py", line 109, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Ethan\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 761, in call_soon
    self._check_closed()
  File "C:\Users\Ethan\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 519, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
To Reproduce
- Run the code snippet provided on Windows 11; Python v3.11
- Sometime during while the for loop is running you should see in the logs that the event loop is closed
Code snippets
async def test() -> None:
    chat_completion = await client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": "Say this is a test",
            }
        ],
        model="gpt-3.5-turbo",
    )
for i in range(50):
    # Note we create the client inside this loop as it triggers the bug
    client = AsyncOpenAI()
    asyncio.run(test())OS
Windows 11
Python version
Python v3.11.7
Library version
openai v1.12.0