Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(integrations): log a mail source whose loop stops with an error (#474)

Rufus5bf5dbaa3279
diff --git a/packages/core/openexecutive/integrations/mail_sources.py b/packages/core/openexecutive/integrations/mail_sources.py
index 2967ec78..5236218b 100644
--- a/packages/core/openexecutive/integrations/mail_sources.py
+++ b/packages/core/openexecutive/integrations/mail_sources.py
@@ -131,10 +131,22 @@ def start_mail_sources() -> list[asyncio.Task[None]]:
continue
if on:
logger.info("mail source %s: starting", source.name)
- tasks.append(asyncio.create_task(source.run(), name=f"mail-source-{source.name}"))
+ task = asyncio.create_task(source.run(), name=f"mail-source-{source.name}")
+ task.add_done_callback(_log_if_crashed)
+ tasks.append(task)
return tasks
+def _log_if_crashed(task: asyncio.Task[None]) -> None:
+ """A source's loop that ended with an error stops reading mail: say so
+ when it happens, not at shutdown."""
+ if task.cancelled():
+ return
+ exc = task.exception()
+ if exc is not None:
+ logger.error("%s stopped: no more mail is read from it", task.get_name(), exc_info=exc)
+
+
async def replay_held_mail(message: Any, request: Any) -> bool:
"""The ``email`` replayer: a held message goes back to the source that
read it, and the mailbox's own (no source named) to the email poller."""
diff --git a/packages/core/tests/unit/test_mail_sources.py b/packages/core/tests/unit/test_mail_sources.py
index 11802bbf..5adb7116 100644
--- a/packages/core/tests/unit/test_mail_sources.py
+++ b/packages/core/tests/unit/test_mail_sources.py
@@ -349,3 +349,27 @@ def test_a_source_session_never_shares_the_mailboxs(turn: dict[str, Any]) -> Non
mail.session_id = ""
_handle(mail)
assert turn["turns"][0]["session"].session_id == "email:relay:t1"
+
+
+def test_a_crashed_source_is_logged(
+ registry: dict[str, MailSource], caplog: pytest.LogCaptureFixture
+) -> None:
+ async def _run() -> None:
+ raise RuntimeError("network down")
+
+ mail_sources.register_mail_source(MailSource(name="relay", run=_run))
+
+ async def _start() -> None:
+ tasks = mail_sources.start_mail_sources()
+ await asyncio.gather(*tasks, return_exceptions=True)
+ await asyncio.sleep(0)
+
+ # The app's logging setup stops ``openexecutive`` loggers propagating, so
+ # once any test has built the app, caplog only sees what reaches it here.
+ mail_sources.logger.addHandler(caplog.handler)
+ try:
+ asyncio.run(_start())
+ finally:
+ mail_sources.logger.removeHandler(caplog.handler)
+ assert "mail-source-relay stopped" in caplog.text
+