Skip to content
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

Unable to close connections #441

Open
eliranwong opened this issue Feb 10, 2025 · 7 comments · May be fixed by #444
Open

Unable to close connections #441

eliranwong opened this issue Feb 10, 2025 · 7 comments · May be fixed by #444

Comments

@eliranwong
Copy link

eliranwong commented Feb 10, 2025

After running ollama python library, I got resource warning ... socket not closed ... 11434 ...

I need a way to explicitly close the connection. However, the ollama client does not have a close method as openai client does.

Right now, I need to use openai library to connect to ollama server, to avoid the warning. with the openai library, I can close the connection with a close method.

I hope the ollama python library offers a way to close the connection. Appreciate your good work. Thanks.

@eliranwong
Copy link
Author

Remarks: I know there is a keep_alive option, but in my case I just want to close connection instead of unloading the model from memory

@ParthSareen
Copy link
Contributor

Hey! Can you point to your usage + what the OpenAI equivalent is? Thanks!

@eliranwong
Copy link
Author

My usage

case 1 - using ollama python package

import ollama
response = ollama.chat(...)
...

case 1 results in warning on exit:

sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 34886), raddr=('127.0.0.1', 11434)>

case 2 - using openai equivalent

from openai import OpenAI
client = OpenAI(api_key="ollama", base_url="http://localhost:11434/v1")
client.chat.completions.create(...)

client.close()

In case 2, the final line client.close() saves me from the resource warning.

@ParthSareen
Copy link
Contributor

Could you try this and see if it helps - just as an experiment not recommending for actual usage:
client._client.close()

@eliranwong
Copy link
Author

I tried your suggestion _client.close() with Ollama python library and got error:

AttributeError: 'Client' object has no attribute 'close'

@ParthSareen
Copy link
Contributor

Cool cool, thanks for testing. Will look into this and possibly expose something. Thanks for the report!

@eliranwong
Copy link
Author

I am looking forward to the fix. Currently, I am working on a project that supports 14 AI backends, of which only Ollama and Cohere are experiencing connection issues, while the rest are functioning properly. Since I have set Ollama as the default backend, the issue is somewhat embarrassing.

@ParthSareen ParthSareen linked a pull request Feb 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants