Skip to content

Commit 1ce9552

Browse files
fix(agentex): end SSE stream when the task row no longer exists
The periodic status recheck's get_task raises ItemDoesNotExist for a hard-deleted task (e.g. retention cleanup). That was caught by the generic handler and retried with backoff indefinitely, spinning error events and holding SSE + DB connections until the client disconnected. Catch ItemDoesNotExist specifically and end the stream — a vanished row is terminal. Other exceptions still fall through to the transient retry path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e019c3b commit 1ce9552

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

agentex/src/domain/use_cases/streams_use_case.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fastapi import Depends
66
from pydantic import ValidationError
77

8+
from src.adapters.crud_store.exceptions import ItemDoesNotExist
89
from src.adapters.streams.adapter_redis import DRedisStreamRepository
910
from src.api.schemas.task_stream_events import TaskStreamEvent
1011
from src.config.dependencies import DEnvironmentVariables
@@ -167,7 +168,15 @@ async def stream_task_events(
167168
# in case a terminal event's publish was lost.
168169
if current_time - last_status_check >= ping_interval:
169170
last_status_check = current_time
170-
task = await self.task_service.get_task(id=task_id)
171+
try:
172+
task = await self.task_service.get_task(id=task_id)
173+
except ItemDoesNotExist:
174+
# Row permanently gone (e.g. retention) — end, don't retry.
175+
logger.info(
176+
f"Ending SSE stream for task {task_id}: "
177+
"task no longer exists"
178+
)
179+
return
171180
if task.status in TERMINAL_TASK_STATUSES:
172181
logger.info(
173182
f"Ending SSE stream for task {task_id}: "

0 commit comments

Comments
 (0)