Skip to content

Commit 8df4e98

Browse files
ctessumclaude
andcommitted
Stop the ISRM tutorial's run_isrm from hanging on a silent worker
The EarthSciLab event stream sends a keep-alive every 15 s, so watch()'s 240 s socket timeout never fired on a quiet stream and it never fell back to GET /runs/{id}. When a cloud worker died without reporting back, run_isrm waited forever. After 60 s with no real event, watch() now checks the run directly and returns if it has ended. Same change as EarthSciML/isrm.esm dbf4b12, in both the post's code block and its notebook copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d9c9a7f commit 8df4e98

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎website/blog/2026-09-09-isrm-esm.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,21 +519,38 @@ def quote(doc, observeds):
519519
return estimate["price"]
520520

521521

522+
# How long the stream may go without a real event before we ask the run itself.
523+
QUIET_SECONDS = 60.0
524+
525+
522526
def watch(session, run_id):
523527
"""Follow a run to a terminal event, surviving a dropped connection.
524528
525529
The stream replays everything already recorded before it streams, so a
526530
reconnect sees what it missed — including a terminal event that landed while
527531
we were disconnected.
532+
533+
The server's keep-alives hold the connection open even when no events come,
534+
so a quiet stream never times out on its own. After `QUIET_SECONDS` without
535+
an event we check the run directly; that is also what tells the server to
536+
look for a worker that died without reporting back.
528537
"""
529538
started = time.time()
530539
while True:
531540
try:
532541
with session.stream(f"/runs/{run_id}/events", timeout=240.0) as resp:
542+
last_event = time.time()
533543
for line in resp:
534544
line = line.decode("utf-8", "replace").strip()
535545
if not line.startswith("data:"):
546+
if time.time() - last_event > QUIET_SECONDS:
547+
last_event = time.time()
548+
run = session.get(f"/runs/{run_id}")
549+
if run["status"] in TERMINAL:
550+
print()
551+
return {"type": run["status"]}
536552
continue
553+
last_event = time.time()
537554
event = json.loads(line[5:]).get("kind") or {}
538555
kind = event.get("type")
539556
if kind == "progress":

‎website/static/blog/2026-09-09-isrm-esm/isrm_esm.ipynb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,21 +525,38 @@
525525
" return estimate[\"price\"]\n",
526526
"\n",
527527
"\n",
528+
"# How long the stream may go without a real event before we ask the run itself.\n",
529+
"QUIET_SECONDS = 60.0\n",
530+
"\n",
531+
"\n",
528532
"def watch(session, run_id):\n",
529533
" \"\"\"Follow a run to a terminal event, surviving a dropped connection.\n",
530534
"\n",
531535
" The stream replays everything already recorded before it streams, so a\n",
532536
" reconnect sees what it missed — including a terminal event that landed while\n",
533537
" we were disconnected.\n",
538+
"\n",
539+
" The server's keep-alives hold the connection open even when no events come,\n",
540+
" so a quiet stream never times out on its own. After `QUIET_SECONDS` without\n",
541+
" an event we check the run directly; that is also what tells the server to\n",
542+
" look for a worker that died without reporting back.\n",
534543
" \"\"\"\n",
535544
" started = time.time()\n",
536545
" while True:\n",
537546
" try:\n",
538547
" with session.stream(f\"/runs/{run_id}/events\", timeout=240.0) as resp:\n",
548+
" last_event = time.time()\n",
539549
" for line in resp:\n",
540550
" line = line.decode(\"utf-8\", \"replace\").strip()\n",
541551
" if not line.startswith(\"data:\"):\n",
552+
" if time.time() - last_event > QUIET_SECONDS:\n",
553+
" last_event = time.time()\n",
554+
" run = session.get(f\"/runs/{run_id}\")\n",
555+
" if run[\"status\"] in TERMINAL:\n",
556+
" print()\n",
557+
" return {\"type\": run[\"status\"]}\n",
542558
" continue\n",
559+
" last_event = time.time()\n",
543560
" event = json.loads(line[5:]).get(\"kind\") or {}\n",
544561
" kind = event.get(\"type\")\n",
545562
" if kind == \"progress\":\n",

0 commit comments

Comments
 (0)