Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(canary): await Client.list_schedules before async-for (cron schedule sweep)
trohitgfacbfc888395
diff --git a/server/services/events/admin_handlers.py b/server/services/events/admin_handlers.pyindex 7c51b0de..8749f378 100644--- a/server/services/events/admin_handlers.py+++ b/server/services/events/admin_handlers.py@@ -146,7 +146,12 @@ async def handle_list_canary_schedules(schedules: List[Dict[str, Any]] = []try:- async for desc in wrapper.client.list_schedules(query=query):+ # ``Client.list_schedules`` is ``async def`` — must await it+ # before iterating. Distinct from ``Client.list_workflows``+ # which returns the iterator directly. See+ # https://python.temporal.io/temporalio.client.Client.html#list_schedules+ iterator = await wrapper.client.list_schedules(query=query)+ async for desc in iterator:schedules.append(_describe_schedule(desc))except Exception as exc: # noqa: BLE001logger.warning(diff --git a/server/services/temporal/schedules.py b/server/services/temporal/schedules.pyindex 4c326c00..84b83420 100644--- a/server/services/temporal/schedules.py+++ b/server/services/temporal/schedules.py@@ -180,7 +180,15 @@ async def delete_cron_schedules_for_deployment(deleted = 0try:- async for desc in client.list_schedules(query=query):+ # ``Client.list_schedules`` is ``async def`` (returns a coroutine+ # that resolves to ``ScheduleAsyncIterator``), unlike+ # ``Client.list_workflows`` which is a plain function returning+ # the iterator directly. Calling ``async for`` on the coroutine+ # raises "'async for' requires an object with __aiter__".+ # Reference:+ # https://python.temporal.io/temporalio.client.Client.html#list_schedules+ iterator = await client.list_schedules(query=query)+ async for desc in iterator:sched_id = desc.idtry:handle = client.get_schedule_handle(sched_id)diff --git a/server/tests/test_admin_handlers.py b/server/tests/test_admin_handlers.pyindex bca72356..e2c7d1a0 100644--- a/server/tests/test_admin_handlers.py+++ b/server/tests/test_admin_handlers.py@@ -142,14 +142,35 @@ class TestListCanarySchedules:@pytest.mark.asyncioasync def test_visibility_query_filters_cron_kind(self, monkeypatch):+ """``Client.list_schedules`` is ``async def`` (returns coroutine+ resolving to ``ScheduleAsyncIterator``) — distinct from+ ``Client.list_workflows`` which returns the iterator directly.+ Stub must match the real SDK shape: a coroutine that returns+ an async iterator. Pre-fix the stub was an async generator+ function which masked an ``async for`` over a coroutine bug —+ production code raised ``'async for' requires an object with+ __aiter__ method, got coroutine`` on every deployment cancel.+ """from services.events.admin_handlers import handle_list_canary_schedulesrecorded_queries: List[str] = []- async def fake_list(query):+ class _FakeScheduleIterator:+ def __init__(self, ids):+ self._ids = list(ids)++ def __aiter__(self):+ return self++ async def __anext__(self):+ if not self._ids:+ raise StopAsyncIteration+ sched_id = self._ids.pop(0)+ return MagicMock(id=sched_id, search_attributes=None)++ async def fake_list(query, **kwargs):recorded_queries.append(query)- for sched_id in ["cron-schedule-wf-1-a", "cron-schedule-wf-1-b"]:- yield MagicMock(id=sched_id, search_attributes=None)+ return _FakeScheduleIterator(["cron-schedule-wf-1-a", "cron-schedule-wf-1-b"])client = MagicMock()client.list_schedules = fake_list@@ -170,6 +191,35 @@ class TestListCanarySchedules:assert "EventWorkflowId='wf-1'" in qassert "EventTriggerKind='cron'" in q+ def test_handler_awaits_list_schedules_before_async_for(self):+ """Regression: the production code MUST await+ ``client.list_schedules(...)`` before iterating. ``list_schedules``+ is ``async def`` in the temporalio SDK; raw ``async for`` over+ the coroutine raises ``'async for' requires an object with+ __aiter__ method, got coroutine`` — observed in prod on every+ deployment cancel before the Wave 13 follow-up fix.+ """+ import inspect++ from services.events import admin_handlers++ src = inspect.getsource(admin_handlers)+ # The handler must NOT have a bare ``async for ... in+ # wrapper.client.list_schedules(...)``. It must capture the+ # iterator via ``await`` first.+ import re++ bare_pattern = re.compile(+ r"async\s+for\s+\w+\s+in\s+\w+\.list_schedules\s*\(",+ )+ assert not bare_pattern.search(src), (+ "admin_handlers contains a bare ``async for ... in "+ "client.list_schedules(...)`` — list_schedules is async def "+ "in temporalio; raw async for over the coroutine fails. "+ "Use ``iterator = await client.list_schedules(query=...)`` "+ "then ``async for desc in iterator:`` instead."+ )+# ---------------------------------------------------------------------------# get_workflow_failure_historydiff --git a/server/tests/test_cron_canary.py b/server/tests/test_cron_canary.pyindex 9ce78265..e206c51b 100644--- a/server/tests/test_cron_canary.py+++ b/server/tests/test_cron_canary.py@@ -177,6 +177,31 @@ class TestCreateCronSchedule:assert schedule_id == "cron-schedule-wf-1-cron-1"+class _FakeScheduleIterator:+ """Match the temporalio SDK's ``ScheduleAsyncIterator`` shape —+ an explicit async iterator object, NOT an async generator.++ ``Client.list_schedules`` is ``async def`` in the real SDK: it+ returns a coroutine that resolves to ``ScheduleAsyncIterator``.+ Pre-fix stubs used ``async def fake(query): yield ...`` (an async+ generator function), which when called returns an async generator+ directly — making ``async for fake(...)`` work AND hiding the+ real bug that production code was doing exactly that against the+ real coroutine-returning method.+ """++ def __init__(self, ids):+ self._ids = list(ids)++ def __aiter__(self):+ return self++ async def __anext__(self):+ if not self._ids:+ raise StopAsyncIteration+ return MagicMock(id=self._ids.pop(0))++class TestDeleteCronSchedulesForDeployment:@pytest.mark.asyncioasync def test_query_filters_by_workflow_id(self):@@ -187,10 +212,13 @@ class TestDeleteCronSchedulesForDeployment:recorded_queries: List[str] = []deleted_ids: List[str] = []- async def fake_list(query):+ async def fake_list(query, **kwargs):+ # ``async def`` returning the iterator — matches real+ # ``Client.list_schedules`` signature. NOT ``yield``.recorded_queries.append(query)- for sched_id in ["cron-schedule-wf-1-a", "cron-schedule-wf-1-b"]:- yield MagicMock(id=sched_id)+ return _FakeScheduleIterator(+ ["cron-schedule-wf-1-a", "cron-schedule-wf-1-b"],+ )def fake_get_handle(sid):handle = MagicMock()@@ -223,9 +251,10 @@ class TestDeleteCronSchedulesForDeployment:delete_cron_schedules_for_deployment,)- async def fake_list(query):- for sid in ["cron-schedule-wf-1-good", "cron-schedule-wf-1-bad"]:- yield MagicMock(id=sid)+ async def fake_list(query, **kwargs):+ return _FakeScheduleIterator(+ ["cron-schedule-wf-1-good", "cron-schedule-wf-1-bad"],+ )def fake_get_handle(sid):handle = MagicMock()@@ -248,6 +277,35 @@ class TestDeleteCronSchedulesForDeployment:# + skipped.assert count == 1+ def test_source_awaits_list_schedules_before_async_for(self):+ """Regression: ``Client.list_schedules`` is ``async def`` in+ the temporalio SDK. Bare ``async for ... in client.list_schedules(...)``+ raises ``'async for' requires an object with __aiter__ method,+ got coroutine`` at runtime — observed in prod on every deployment+ cancel before the Wave 13 follow-up.++ The fix captures the iterator via ``await`` first:+ iterator = await client.list_schedules(query=query)+ async for desc in iterator:+ ...+ """+ import inspect+ import re++ from services.temporal import schedules as schedules_mod++ src = inspect.getsource(schedules_mod.delete_cron_schedules_for_deployment)+ bare_pattern = re.compile(+ r"async\s+for\s+\w+\s+in\s+\w+\.list_schedules\s*\(",+ )+ assert not bare_pattern.search(src), (+ "delete_cron_schedules_for_deployment contains bare "+ "``async for ... in client.list_schedules(...)``. "+ "list_schedules is ``async def`` in temporalio — must "+ "``await`` to get the iterator first. See "+ "https://python.temporal.io/temporalio.client.Client.html#list_schedules"+ )+# ---------------------------------------------------------------------------# DeploymentManager cron canary integration