Sigsegv during garbage collection under certian circumstances #259

Closed
opened 2026-02-15 16:29:18 -05:00 by yindo · 2 comments
Owner

Originally created by @Jacoblightning on GitHub (Apr 11, 2025).

There is a Python Sigsegv if garbage collection occurs too soon after iterating through a streamed chat.
(At least, I believe that's why it happens)

Minimal Reproducible Example

from ollama import chat

# Has to be here to segfault???
def colorSwitch(color): 
    print(color, end="", flush=True)


stream = chat(
    model="llama3.2", # I think it works with any but this is what I used.
    messages=[{"role": "user", "content": ""}],
    options={"seed":0}, # Does not need this but I figured it would be helpful
    stream=True,
)
# Any iteration works. I just simplified it down to this.
part = next(iter(stream))['message']['content']

Expected result

The script exits with no error

Actual Result

The script segfaults

Traceback with python -X faulthandler:

Fatal Python error: Segmentation fault

Current thread 0x0000726437060bc0 (most recent call first):
  Garbage-collecting
  File "lib/python3.13/site-packages/httpx/_client.py", line 158 in close
  File "lib/python3.13/site-packages/httpx/_models.py", line 972 in close
  File "lib/python3.13/site-packages/httpx/_client.py", line 877 in stream
  File "/usr/lib/python3.13/contextlib.py", line 162 in __exit__
  File "lib/python3.13/site-packages/ollama/_client.py", line 163 in inner

Environment

Python 3.13.2 on Arch linux

Originally created by @Jacoblightning on GitHub (Apr 11, 2025). There is a Python Sigsegv if garbage collection occurs too soon after iterating through a streamed chat. (At least, I believe that's why it happens) # Minimal Reproducible Example ```python from ollama import chat # Has to be here to segfault??? def colorSwitch(color): print(color, end="", flush=True) stream = chat( model="llama3.2", # I think it works with any but this is what I used. messages=[{"role": "user", "content": ""}], options={"seed":0}, # Does not need this but I figured it would be helpful stream=True, ) # Any iteration works. I just simplified it down to this. part = next(iter(stream))['message']['content'] ``` # Expected result The script exits with no error # Actual Result The script segfaults Traceback with `python -X faulthandler`: ``` Fatal Python error: Segmentation fault Current thread 0x0000726437060bc0 (most recent call first): Garbage-collecting File "lib/python3.13/site-packages/httpx/_client.py", line 158 in close File "lib/python3.13/site-packages/httpx/_models.py", line 972 in close File "lib/python3.13/site-packages/httpx/_client.py", line 877 in stream File "/usr/lib/python3.13/contextlib.py", line 162 in __exit__ File "lib/python3.13/site-packages/ollama/_client.py", line 163 in inner ``` # Environment Python 3.13.2 on Arch linux
yindo closed this issue 2026-02-15 16:29:18 -05:00
Author
Owner

@Jacoblightning commented on GitHub (Apr 11, 2025):

If this turns out to be a CPython error, please let me know. I already raised the issue Here (#132413)

@Jacoblightning commented on GitHub (Apr 11, 2025): If this turns out to be a CPython error, please let me know. I already raised the issue [Here (#132413)](https://github.com/python/cpython/issues/132413)
Author
Owner

@Jacoblightning commented on GitHub (Apr 11, 2025):

It was, in fact, not your bug. Sorry.

@Jacoblightning commented on GitHub (Apr 11, 2025): It was, in fact, not your bug. Sorry.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#259