Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unclosed client session #570

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
wanyang6-tal opened this issue Aug 11, 2023 · 3 comments
Closed

Unclosed client session #570

wanyang6-tal opened this issue Aug 11, 2023 · 3 comments
Labels
bug Something isn't working fixed in v1 Issues addressed by the v1 beta

Comments

@wanyang6-tal
Copy link

wanyang6-tal commented Aug 11, 2023

Describe the bug

When I use FastAPI for forwarding and set stream=True, for example: async for res in await openai.ChatCompletion.acreate(**kwargs), when this request is terminated before it completes, an exception occurs as follows: [asyncio] [ERROR]: Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x7f0ca458c490>.

To Reproduce

1. Write a FastAPI request using the acreate method to return data in streaming format.
2. Use a packet sending tool to make a request to the written API, terminate the request while receiving data.
3. Wait for a moment, and the exception will be displayed in the terminal.

Code snippets

No response

OS

Linux

Python version

Python 3.11.4

Library version

openai 0.27.8

### Tasks
@wanyang6-tal wanyang6-tal added the bug Something isn't working label Aug 11, 2023
@perryHV
Copy link

perryHV commented Oct 8, 2023

Hii @wanyang6-tal I want to work on this issue please assign me.

@wanyang6-tal
Copy link
Author

Hii @wanyang6-tal I want to work on this issue please assign me.

sorry, I don't have permission

@RobertCraigie RobertCraigie added the fixed in v1 Issues addressed by the v1 beta label Oct 18, 2023
@RobertCraigie
Copy link
Collaborator

This is likely fixed in our upcoming v1 beta as we've moved to an explicit client instance based API which makes controlling the HTTP instances much easier.

from openai import OpenAI

client = OpenAI()
# make requests
client.close()  # close manually

# or using a context manager
with OpenAI() as client:
  # make requests
  ...

It should be noted that we highly recommend using one client instance and re-using it between requests as we can then re-use HTTP connections leading to a massive performance boost.

If you're using FastAPI then you could configure a shutdown event to close the client instance cleanly, https://fastapi.tiangolo.com/advanced/events/?h=shut#shutdown-event.

Please give the beta a try and share any feedback in the linked discussion!

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

No branches or pull requests

3 participants