🐛 Bug Report: llama streams: close() before reading never ends the span; aclose() leaves the HTTP response (connection) open
Maintainer thường phản hồi trong vòng 1 ngày
@Suddha-Abhitej đang làm issue này rồi.
Từ ngày 13/10/2026.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 68/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- ollama, python
- Lĩnh vực
- backend, observability
Hướng nghiên cứu
Start in opentelemetry/instrumentation/ollama/__init__.py at _accumulate_streaming_response and _aaccumulate_streaming_response, then check the Ollama streaming tests. Reproduce the sync close() before reading and async aclose() after one chunk using the supplied script. Done means both paths end their spans and the async path closes the underlying HTTP response promptly.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Which component is this bug for?
Ollama Instrumentation
📜 Description
For stream=True, the Ollama instrumentation returns its own generator, _accumulate_streaming_response / _aaccumulate_streaming_response (opentelemetry/instrumentation/ollama/__init__.py, lines 90 and 161 at 0.62.4), which iterates over the Ollama stream and ends the span in its finally. Two problems follow from that:
-
Sync, closed before reading. The span is created before the generator, but a generator that is closed before its first
next()never runs its body, so thetry/finallythat ends the span is never entered.stream.close()before reading (the app decides it does not need the answer) leaves the span open forever: it is never exported and stays referenced. -
Async,
aclose().await stream.aclose()closes the instrumentation's async generator (itsfinallyends the span), but not the Ollama async stream it iterates over: async generators are not closed when an enclosingasync foris interrupted. The Ollama stream holds the httpx response, so the response, and its connection, stays open until the stream is garbage-collected. Without the instrumentation,aclose()closes it immediately. In a server that cancels streamed answers this holds connections from the pool.
A fix would be to end the span (and close the wrapped stream) in the wrapper's close path: e.g. a small wrapper class whose close() / aclose() end the span if the iteration never started, and to call response.close() / await response.aclose() in the finally blocks.
👟 Reproduction steps
pip install ollama opentelemetry-instrumentation-ollama opentelemetry-sdk httpx
import asyncio, gc, json, httpx, ollama
from importlib.metadata import version
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
from opentelemetry.instrumentation.ollama import OllamaInstrumentor
class Spans(SpanProcessor):
def __init__(self): self.started, self.ended = {}, {}
def on_start(self, span, parent_context=None): self.started[span.context.span_id] = span.name
def on_end(self, span): self.ended[span.context.span_id] = span.name
def open(self): return [n for k, n in self.started.items() if k not in self.ended]
spans = Spans()
provider = TracerProvider()
provider.add_span_processor(spans)
OllamaInstrumentor().instrument(tracer_provider=provider)
CLOSED = []
class Body(httpx.SyncByteStream, httpx.AsyncByteStream): # NDJSON body; records whether the client closed it
def __init__(self, lines): self.lines = lines
def __iter__(self): yield from self.lines
async def __aiter__(self):
for x in self.lines: yield x
def close(self): CLOSED.append(True)
async def aclose(self): CLOSED.append(True)
def respond(request): # local stand-in for the Ollama server: a 3-part streamed answer
chat = request.url.path.endswith("/api/chat")
parts = [{"model": "m", "created_at": "2026-01-01T00:00:00Z", "done": False,
**({"message": {"role": "assistant", "content": w}} if chat else {"response": w})} for w in ("Hel", "lo", "!")]
parts.append({"model": "m", "created_at": "2026-01-01T00:00:00Z", "done": True, "done_reason": "stop",
**({"message": {"role": "assistant", "content": ""}} if chat else {"response": ""})})
return httpx.Response(200, headers={"content-type": "application/x-ndjson"},
stream=Body([(json.dumps(p) + "\n").encode() for p in parts]))
print(f"ollama {version('ollama')} | opentelemetry-instrumentation-ollama {version('opentelemetry-instrumentation-ollama')}")
client = ollama.Client(host="http://mock", transport=httpx.MockTransport(respond))
stream = client.generate(model="m", prompt="hi", stream=True)
stream.close() # the app decides it does not need the answer
gc.collect()
print("sync: close() before reading -> spans never ended:", spans.open())
async def main():
aclient = ollama.AsyncClient(host="http://mock", transport=httpx.MockTransport(respond))
stream = await aclient.chat(model="m", messages=[{"role": "user", "content": "hi"}], stream=True)
await stream.__anext__()
await stream.aclose() # the app stops reading after the first part
print("async: aclose() after 1 chunk -> HTTP response closed:", bool(CLOSED))
asyncio.run(main())
👍 Expected behavior
close() before reading ends the span (the call was made, it was just not read), and aclose() closes the underlying Ollama stream, so its HTTP response is closed right away, as it is without the instrumentation.
👎 Actual Behavior with Screenshots
ollama 0.6.3 | opentelemetry-instrumentation-ollama 0.62.4
sync: close() before reading -> spans never ended: ['ollama.completion']
async: aclose() after 1 chunk -> HTTP response closed: False
The same script without OllamaInstrumentor().instrument(...) prints HTTP response closed: True.
🤖 Python Version
3.12
📃 Provide any additional context for the Bug.
opentelemetry-instrumentation-ollama 0.62.4 (the latest release), ollama 0.6.3. Not covered by the open PRs #4127 (exceptions) or #4223 (semantic conventions).
👀 Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Are you willing to submit PR?
None
- Ngôn ngữ chính
- Python
- Star
- 7.5k
- Fork
- 1.2k
- Merge trung bình
- 1 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của traceloop/openllmetry
-
🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returnsCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởbug python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
traceloop/openllmetry#4717 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentationCó thể đã có người làm @Quantum-Enigma-dot đã nhận 21 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
traceloop/openllmetry#4665 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completionsCó thể đã có người làm @jayfeng7 đã nhận 23 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
traceloop/openllmetry#4660 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug Report: Failed dataset deletes still remove local rows and columnsCó thể đã có người làm @inchang-ing đã nhận 32 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
traceloop/openllmetry#4630 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penaltyCó thể đã có người làm @shantam77 đã nhận 38 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
traceloop/openllmetry#4598 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của traceloop/openllmetry
Issue tương tự
-
Layout accepts a step threshold of 0 or less, then in-toto-verify crashes with StopIterationCó thể đã có người làm @BahaaMattar đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs discussion
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openfga/openfga.dev#1436 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
usb-device-hid: MouseInterface reports bInterfaceProtocol 1 (keyboard) instead of 2Có thể đã có người làm @Duhan07 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
micropython/micropython-lib#1176 ·