A supervised loop that crashes reports its node down

A push session that failed before it listened left only a task_crashed
event. The wrapper in _run_supervised now reports down with the exception,
unless the loop already said why it is down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgBfkm4rCmtd1T4vHWjEPx
This commit is contained in:
2026-10-15 09:42:01 +02:00
co-authored by Claude Opus 5.5
parent 13567f8990
commit e98808e9d8
2 changed files with 51 additions and 2 deletions
+21 -2
View File
@@ -90,6 +90,7 @@ class Node:
"_pipeline",
"synchronous",
"_on_health",
"_health",
"supervisor",
"fingerprint",
)
@@ -105,6 +106,9 @@ class Node:
self.f = f
self._pipeline: Pipeline | None = None
self._on_health: Callable[[Node, str, str | None], None] | None = None
# What this node reported since its background loop last started, so
# a crash does not overwrite the reason the loop gave for being down.
self._health: str | None = None
# Set by the controller before start(); a node built on its own (tests,
# previews) runs its loops unsupervised.
self.supervisor: Supervisor | None = None
@@ -206,11 +210,25 @@ class Node:
Returns the task only when unsupervised, since that is the one case
where the caller has to cancel it itself.
A loop that raises reports the node down with the exception, unless it
already said why it is down. A push session that fails before it
listens would otherwise show up as a crash event and nowhere else.
"""
async def reported() -> None:
self._health = None
try:
await factory()
except Exception as exc:
if self._health != "down":
self.report_health("down", f"{type(exc).__name__}: {exc}")
raise
if self.supervisor is not None:
self.supervisor.spawn(f"{self.id}:{name}", self.flow, factory)
self.supervisor.spawn(f"{self.id}:{name}", self.flow, reported)
return None
return asyncio.create_task(factory())
return asyncio.create_task(reported())
@staticmethod
async def _cancel_task(task: asyncio.Task[None]) -> None:
@@ -230,6 +248,7 @@ class Node:
def report_health(self, status: str, detail: str | None = None) -> None:
"""Say how this node's connection is doing: ok, degraded or down."""
self._health = status
if self._on_health is not None:
self._on_health(self, status, detail)