-
Notifications
You must be signed in to change notification settings - Fork 571
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
Properly close session in AsyncInferenceClient
#2496
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
tests/cassettes/test_http_session_correctly_closed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
user-agent: | ||
- unknown/None; hf_hub/0.25.0.dev0; python/3.10.12; torch/2.4.0; tensorflow/2.17.0; | ||
fastcore/1.5.23 | ||
method: POST | ||
uri: https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct | ||
response: | ||
body: | ||
string: 'data: {"index":1,"token":{"id":279,"text":" the","logprob":-0.63378906,"special":false},"generated_text":" | ||
the","details":null} | ||
|
||
|
||
' | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Cache-Control: | ||
- no-cache | ||
Connection: | ||
- keep-alive | ||
Content-Type: | ||
- text/event-stream | ||
Date: | ||
- Fri, 30 Aug 2024 13:58:50 GMT | ||
Transfer-Encoding: | ||
- chunked | ||
Vary: | ||
- origin, access-control-request-method, access-control-request-headers, Origin, | ||
Access-Control-Request-Method, Access-Control-Request-Headers | ||
x-accel-buffering: | ||
- 'no' | ||
x-compute-characters: | ||
- '41' | ||
x-compute-type: | ||
- 2-a10-g | ||
x-request-id: | ||
- 8ZMGhj7Cj90UK12dSJhfj | ||
x-sha: | ||
- 5206a32e0bd3067aef1ce90f5528ade7d866253f | ||
status: | ||
code: 200 | ||
message: OK | ||
- request: | ||
body: null | ||
headers: | ||
user-agent: | ||
- unknown/None; hf_hub/0.25.0.dev0; python/3.10.12; torch/2.4.0; tensorflow/2.17.0; | ||
fastcore/1.5.23 | ||
method: POST | ||
uri: https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct | ||
response: | ||
body: | ||
string: 'data: {"index":1,"token":{"id":279,"text":" the","logprob":-0.63378906,"special":false},"generated_text":" | ||
the","details":null} | ||
|
||
|
||
' | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '129' | ||
Content-Type: | ||
- text/event-stream | ||
Date: | ||
- Fri, 30 Aug 2024 13:58:51 GMT | ||
Vary: | ||
- Origin, Access-Control-Request-Method, Access-Control-Request-Headers | ||
x-compute-type: | ||
- cache | ||
x-request-id: | ||
- gG5snagDwsUuzxQUSHpfs | ||
x-sha: | ||
- 5206a32e0bd3067aef1ce90f5528ade7d866253f | ||
status: | ||
code: 200 | ||
message: OK | ||
- request: | ||
body: null | ||
headers: | ||
user-agent: | ||
- unknown/None; hf_hub/0.25.0.dev0; python/3.10.12; torch/2.4.0; tensorflow/2.17.0; | ||
fastcore/1.5.23 | ||
method: POST | ||
uri: https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct | ||
response: | ||
body: | ||
string: 'data: {"index":1,"token":{"id":279,"text":" the","logprob":-0.63378906,"special":false},"generated_text":" | ||
the","details":null} | ||
|
||
|
||
' | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '129' | ||
Content-Type: | ||
- text/event-stream | ||
Date: | ||
- Fri, 30 Aug 2024 13:58:51 GMT | ||
Vary: | ||
- Origin, Access-Control-Request-Method, Access-Control-Request-Headers | ||
x-compute-type: | ||
- cache | ||
x-request-id: | ||
- 8PS-J7-J2QKF0FyOb9War | ||
x-sha: | ||
- 5206a32e0bd3067aef1ce90f5528ade7d866253f | ||
status: | ||
code: 200 | ||
message: OK | ||
- request: | ||
body: null | ||
headers: | ||
user-agent: | ||
- unknown/None; hf_hub/0.25.0.dev0; python/3.10.12; torch/2.4.0; tensorflow/2.17.0; | ||
fastcore/1.5.23 | ||
method: POST | ||
uri: https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3.1-8B-Instruct | ||
response: | ||
body: | ||
string: 'data: {"index":1,"token":{"id":279,"text":" the","logprob":-0.63378906,"special":false},"generated_text":" | ||
the","details":null} | ||
|
||
|
||
' | ||
headers: | ||
Access-Control-Allow-Credentials: | ||
- 'true' | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '129' | ||
Content-Type: | ||
- text/event-stream | ||
Date: | ||
- Fri, 30 Aug 2024 13:58:52 GMT | ||
Vary: | ||
- Origin, Access-Control-Request-Method, Access-Control-Request-Headers | ||
x-compute-type: | ||
- cache | ||
x-request-id: | ||
- _MFpbB44jCKcoM8wai66z | ||
x-sha: | ||
- 5206a32e0bd3067aef1ce90f5528ade7d866253f | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, shouldn't this just close the session as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well well well...
I got a hard time getting to make it work. The thing is that you don't know when
__del__
is called by Python's garbage collector. Sinceself.close
is an async function, you need an active event loop to make the close method run -which is not guaranteed. I first tried some hacks but gave up given the complexity compared to having a properclose
method or using an async context manager.This answer on stackoverflow also convinced me of doing things like this.