[Bugfix] Fix allocation & free logic of SingleWriterShmRingBuffer (#27117)

Signed-off-by: Kero Liang <kerorek@outlook.com>
Signed-off-by: Roger Wang <hey@rogerw.io>
Co-authored-by: donglu <donglu@cohere.com>
Co-authored-by: Roger Wang <hey@rogerw.io>
This commit is contained in:
Kero Liang
2025-10-28 23:01:24 +08:00
committed by GitHub
parent e88bdd60d9
commit 02af36df36
2 changed files with 75 additions and 4 deletions

View File

@@ -127,9 +127,7 @@ class SingleWriterShmRingBuffer:
if create:
# we are creating a buffer
self.metadata = {
self.monotonic_id_end: self.data_buffer_end
} # monotonic_id -> start address
self.metadata: dict[int, int] = {} # monotonic_id -> start address
self.shared_memory = shared_memory.SharedMemory(
create=True, size=self.data_buffer_size, name=name
)
@@ -288,7 +286,15 @@ class SingleWriterShmRingBuffer:
self.monotonic_id_start = (
self.monotonic_id_start + 1
) % self.ID_MAX
self.data_buffer_start = address
if self.monotonic_id_start in self.metadata:
# pointing to the start addr of next allocation
self.data_buffer_start += (
self.metadata[self.monotonic_id_start]
- self.data_buffer_start
) % self.data_buffer_size
else:
# no remaining allocation, reset to zero
self.data_buffer_start = self.data_buffer_end = 0
freed_bytes += metadata[1]
else:
# there are still readers, we cannot free the buffer