Skip to content

Commit 4392586

Browse files
authored
fix: drain non-empty buffers on ExtractionWorker shutdown (#78)
Short conversations that haven't hit the 800-token threshold would silently lose their buffered extraction requests when shutdown() cancelled the pending _debounced_process timers. Fix collects all keys with pending requests before cancelling timers, then awaits asyncio.gather over _process(key) for each — using the existing semaphore — before clearing remaining state.
1 parent 12da55b commit 4392586

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

vektori/utils/async_worker.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,30 @@ async def _extract_one(req: ExtractionRequest) -> None:
155155
await asyncio.gather(*[_extract_one(r) for r in buf.requests])
156156

157157
async def shutdown(self, timeout: float = 30.0) -> None:
158-
"""Cancel pending timers and wait for any in-flight tasks."""
158+
"""Drain buffered extractions, then cancel pending timers and clear state."""
159+
# Collect keys with buffered-but-not-yet-fired requests before touching timers
160+
keys_to_drain = [k for k, buf in self._buffers.items() if buf.requests]
161+
162+
# Cancel timers for those keys first so _debounced_process can't double-fire
163+
for key in keys_to_drain:
164+
t = self._timers.get(key)
165+
if t and not t.done():
166+
t.cancel()
167+
168+
# Drain remaining buffers under the existing concurrency semaphore
169+
if keys_to_drain:
170+
try:
171+
await asyncio.wait_for(
172+
asyncio.gather(
173+
*[self._process(key) for key in keys_to_drain],
174+
return_exceptions=True,
175+
),
176+
timeout=timeout,
177+
)
178+
except asyncio.TimeoutError:
179+
logger.warning("Extraction worker shutdown timed out while draining buffers")
180+
181+
# Cancel any remaining timers (e.g. in-flight tasks not in keys_to_drain)
159182
pending = [t for t in self._timers.values() if not t.done()]
160183
for t in pending:
161184
t.cancel()
@@ -166,6 +189,6 @@ async def shutdown(self, timeout: float = 30.0) -> None:
166189
timeout=timeout,
167190
)
168191
except asyncio.TimeoutError:
169-
logger.warning("Extraction worker shutdown timed out")
192+
logger.warning("Extraction worker shutdown timed out waiting for in-flight tasks")
170193
self._buffers.clear()
171194
self._timers.clear()

0 commit comments

Comments
 (0)