Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion livekit-agents/livekit/agents/utils/codecs/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ def read(self, size: int = -1) -> bytes:
if data:
# shrink the buffer to remove already-read data
remaining = self._buffer.read()
self._buffer = io.BytesIO(remaining)

# Reuse the same buffer instead of reallocating
self._buffer.seek(0)
self._buffer.truncate()
self._buffer.write(remaining)
self._buffer.seek(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works.

would future bytes be written at 0? overwriting remaining? reading and writing are happening at the same position, which isn't what we want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works.

would future bytes be written at 0? overwriting remaining? reading and writing are happening at the same position, which isn't what we want

Thanks for the review. After a deeper look, you’re right.

While the truncate-based approach reduces allocations, it relies on implicit BytesIO cursor behavior and isn’t robust enough for a concurrency primitive.
Alternative designs (e.g. ring buffers) would break PyAV’s file-like contract or require a larger redesign.

I’ll revert this change and keep the existing behavior for correctness.


return data

if self._eof:
Expand Down