Unable to close connections #218

Closed
opened 2026-02-15 16:28:48 -05:00 by yindo · 11 comments
Owner

Originally created by @eliranwong on GitHub (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.

Originally created by @eliranwong on GitHub (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.
yindo closed this issue 2026-02-15 16:28:48 -05:00
Author
Owner

@eliranwong commented on GitHub (Feb 10, 2025):

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

@eliranwong commented on GitHub (Feb 10, 2025): 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
Author
Owner

@ParthSareen commented on GitHub (Feb 12, 2025):

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

@ParthSareen commented on GitHub (Feb 12, 2025): Hey! Can you point to your usage + what the OpenAI equivalent is? Thanks!
Author
Owner

@eliranwong commented on GitHub (Feb 12, 2025):

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.

@eliranwong commented on GitHub (Feb 12, 2025): 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.
Author
Owner

@ParthSareen commented on GitHub (Feb 12, 2025):

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

@ParthSareen commented on GitHub (Feb 12, 2025): Could you try this and see if it helps - just as an experiment not recommending for actual usage: `client._client.close()`
Author
Owner

@eliranwong commented on GitHub (Feb 12, 2025):

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

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

@eliranwong commented on GitHub (Feb 12, 2025): I tried your suggestion `_client.close()` with Ollama python library and got error: AttributeError: 'Client' object has no attribute 'close'
Author
Owner

@ParthSareen commented on GitHub (Feb 12, 2025):

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

@ParthSareen commented on GitHub (Feb 12, 2025): Cool cool, thanks for testing. Will look into this and possibly expose something. Thanks for the report!
Author
Owner

@eliranwong commented on GitHub (Feb 13, 2025):

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.

@eliranwong commented on GitHub (Feb 13, 2025): 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.
Author
Owner

@vedbobo commented on GitHub (Feb 18, 2025):

Front end engineer closed

@vedbobo commented on GitHub (Feb 18, 2025): Front end engineer closed
Author
Owner

@BrainStone commented on GitHub (Feb 28, 2025):

You can try client._client._client.close()

@BrainStone commented on GitHub (Feb 28, 2025): You can try `client._client._client.close()`
Author
Owner

@eliranwong commented on GitHub (Feb 28, 2025):

You can try client._client._client.close()

It works, many thanks.

@eliranwong commented on GitHub (Feb 28, 2025): > You can try `client._client._client.close()` It works, many thanks.
Author
Owner

@20246688 commented on GitHub (Mar 17, 2025):

client._client.aclose()?

@20246688 commented on GitHub (Mar 17, 2025): client._client.aclose()?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#218