Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 22 additions & 8 deletions src/bub/channels/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@ def _reset_timer(self, timeout: float) -> None:
self._timer.cancel()
self._timer = self._loop.call_later(timeout, self._event.set)

def _clear_processing(self, task: asyncio.Task | None = None) -> None:
if task is None or self._in_processing is task:
self._in_processing = None

def _ensure_processing(self) -> None:
if self._in_processing is None or self._in_processing.done():
task = asyncio.create_task(self._process())
task.add_done_callback(self._clear_processing)
self._in_processing = task

async def _process(self) -> None:
await self._event.wait()
message = ChannelMessage.from_batch(self._pending_messages)
self._pending_messages.clear()
self._in_processing = None
current = asyncio.current_task()
try:
await self._event.wait()
if not self._pending_messages:
return
message = ChannelMessage.from_batch(self._pending_messages)
self._pending_messages.clear()
finally:
self._clear_processing(current)
await self._handler(message)

async def __call__(self, message: ChannelMessage) -> None:
Expand All @@ -60,9 +75,8 @@ async def __call__(self, message: ChannelMessage) -> None:
"session.message received active session_id={}, content={}", message.session_id, message.content
)
self._reset_timer(self.debounce_seconds)
if self._in_processing is None:
self._in_processing = asyncio.create_task(self._process())
elif self._last_active_time is not None and self._in_processing is None:
self._ensure_processing()
elif self._last_active_time is not None and (self._in_processing is None or self._in_processing.done()):
logger.info("session.receive followup session_id={} message={}", message.session_id, message.content)
self._reset_timer(self.max_wait_seconds)
self._in_processing = asyncio.create_task(self._process())
self._ensure_processing()
51 changes: 51 additions & 0 deletions tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,3 +2017,54 @@ async def complete(text: str) -> list[str]:
assert await complete(",model relay:o") == ["relay:o3"]
assert await complete(",model openai:gpt-4o extra") == []
assert fetched == ["available for openai:gpt-4o", "relay"]


@pytest.mark.asyncio
async def test_buffered_handler_recovers_if_processing_task_is_cancelled_or_fails() -> None:
handled: list[str] = []
received = asyncio.Event()

async def receive(message: ChannelMessage) -> None:
handled.append(message.content)
received.set()

handler = BufferedMessageHandler(receive, active_time_window=10, max_wait_seconds=10, debounce_seconds=0.05)
await handler(_message("first", is_active=True))
assert handler._in_processing is not None

handler._in_processing.cancel()
with contextlib.suppress(asyncio.CancelledError):
await handler._in_processing
assert handler._in_processing is None

await handler(_message("second", is_active=True))
await asyncio.wait_for(received.wait(), timeout=1)
assert handled == ["first\nsecond"]


@pytest.mark.asyncio
async def test_buffered_handler_recovers_after_batch_error(monkeypatch: pytest.MonkeyPatch) -> None:
handled: list[str] = []
received = asyncio.Event()

async def receive(message: ChannelMessage) -> None:
handled.append(message.content)
received.set()

handler = BufferedMessageHandler(receive, active_time_window=10, max_wait_seconds=10, debounce_seconds=0.01)
original_from_batch = ChannelMessage.from_batch

def fail_once(messages: list[ChannelMessage]) -> ChannelMessage:
monkeypatch.setattr(ChannelMessage, "from_batch", original_from_batch)
raise RuntimeError("boom")

monkeypatch.setattr(ChannelMessage, "from_batch", fail_once)
await handler(_message("bad", is_active=True))
assert handler._in_processing is not None
with pytest.raises(RuntimeError, match="boom"):
await handler._in_processing
assert handler._in_processing is None

await handler(_message("ok", is_active=True))
await asyncio.wait_for(received.wait(), timeout=1)
assert handled == ["bad\nok"]