fix: unblock post-shell status probe when connection is lost#857
Conversation
The status probe after shell exit can block indefinitely when the exporter becomes unreachable without a TCP RST (e.g. network partition, firewall drop, suspended process). Race the probe against the status monitor's connection_lost flag so the CLI exits cleanly once the monitor detects the stuck connection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe shell’s post-run status probe now stops when connection loss is detected and marks the monitor accordingly if no probe result is returned. Tests cover both normal probe completion and cancellation during a blocked probe. ChangesConnection probe cancellation
Estimated code review effort: 2 (Simple) | ~12 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
python/packages/jumpstarter-cli/jumpstarter_cli/shell.py (1)
355-391: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider extracting the cancellation-aware probe into a helper.
_run_shell_with_lease_asyncis already flagged# noqa: C901; the new task-group + nested closure adds another layer of nesting/branching to an already large function. Pulling this block into a small helper (e.g.,_probe_status_or_mark_lost(client, monitor)returning the resolved status orNone) would keep the cancellation logic testable in isolation and reduce the parent function's complexity.♻️ Sketch of extracted helper
+async def _probe_status_cancel_on_loss(client, monitor): + """Probe status, cancelling if the monitor detects connection loss.""" + probe_status = None + async with anyio.create_task_group() as probe_tg: + + async def cancel_when_connection_lost(): + while not monitor.connection_lost: + await anyio.sleep(1) + probe_tg.cancel_scope.cancel() + + probe_tg.start_soon(cancel_when_connection_lost) + probe_status = await client.get_status_async() + probe_tg.cancel_scope.cancel() + return probe_status🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@python/packages/jumpstarter-cli/jumpstarter_cli/shell.py` around lines 355 - 391, The cancellation-aware connection probe in _run_shell_with_lease_async is adding too much nesting and branching to an already complex function. Extract the task-group probe logic, including cancel_when_connection_lost and the probe_status handling, into a small helper such as _probe_status_or_mark_lost(client, monitor) that returns the resolved status or None. Keep the lease.lease_ended and unexpected ExporterStatus handling in the caller, and have the helper own the cancellation and connection_lost marking so the probe logic is easier to test and the parent function stays simpler.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@python/packages/jumpstarter-cli/jumpstarter_cli/shell.py`:
- Around line 355-391: The cancellation-aware connection probe in
_run_shell_with_lease_async is adding too much nesting and branching to an
already complex function. Extract the task-group probe logic, including
cancel_when_connection_lost and the probe_status handling, into a small helper
such as _probe_status_or_mark_lost(client, monitor) that returns the resolved
status or None. Keep the lease.lease_ended and unexpected ExporterStatus
handling in the caller, and have the helper own the cancellation and
connection_lost marking so the probe logic is easier to test and the parent
function stays simpler.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2a44bcc5-3c1b-49cf-9a69-8cee40731e87
📒 Files selected for processing (2)
python/packages/jumpstarter-cli/jumpstarter_cli/shell.pypython/packages/jumpstarter-cli/jumpstarter_cli/shell_test.py
- probe stays blocked while connection_lost is False (negative check) - probe is cancelled once connection_lost becomes True (positive check)
6c4ea12 to
0d2f24f
Compare
- Extract the inline task-group + polling-watcher pattern from the post-shell probe into _cancel_if_connection_lost() helper - Add unit tests for the helper (normal completion and connection loss)
Summary
After the user exits
jmp shell, a status probe is sent to detect exporter restarts before triggering the afterLease hook. If the exporter becomes unreachable without sending a TCP RST (e.g. network partition, firewall drop, suspended process), this probe blocks indefinitely — hanging the CLI after the shell exits.The status monitor already handles this correctly: it detects stuck connections after 20 consecutive timeouts and sets
connection_lost = True, logging progressive warnings to the user. However, the probe runs as an independent gRPC call and never observes the monitor's flag.Fix
Race the probe against a lightweight watcher task that polls
monitor.connection_lostevery second. When the monitor marks the connection as lost, the watcher cancels the probe's task group scope, unblocking the CLI. The probe succeeds normally in the healthy case and cancels the watcher itself.This preserves the existing monitor behaviour — users still see the progressive timeout warnings before the CLI exits cleanly.
Test
Added
test_probe_cancelled_when_connection_losttoTestRunShellWithLeaseAsync:connection_lostis False (5s negative phase)connection_lostbecomes True (positive phase)fail_after(10)to fail hard instead of hanging if the fix regresses