Skip to content

[mcp] fix: bound stops waiting on startup - #148

Merged
Wangmerlyn merged 4 commits into
mainfrom
codex/bound-startup-stop-wait
Jun 29, 2026
Merged

[mcp] fix: bound stops waiting on startup#148
Wangmerlyn merged 4 commits into
mainfrom
codex/bound-startup-stop-wait

Conversation

@Wangmerlyn

@Wangmerlyn Wangmerlyn commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • Bound stop_keep waits for sessions stuck in starting so stop requests do not hang indefinitely.
  • Preserve the no-missed-starts contract by marking timed-out starting jobs for quiet release after startup completes.
  • Update MCP/CLI docs, AGENTS.md lifecycle guidance, and the implementation plan.

Test Plan

  • PYTHONPATH=$PWD/src pytest tests/mcp/test_server.py::test_stop_keep_times_out_waiting_for_stuck_starting_session tests/mcp/test_server.py::test_stop_all_times_out_waiting_for_stuck_starting_session tests/mcp/test_server.py::test_timed_out_stop_of_starting_session_releases_after_startup_completes tests/mcp/test_server.py::test_timed_out_stop_of_failed_startup_does_not_affect_reused_job_id tests/mcp/test_server.py::test_stop_all_does_not_duplicate_starting_timeout_after_startup_settles tests/mcp/test_server.py::test_stop_keep_waits_for_starting_session tests/mcp/test_server.py::test_stop_all_waits_for_starting_session tests/mcp/test_server.py::test_stop_all_waits_only_for_sessions_starting_at_snapshot -q -> 8 passed
  • PYTHONPATH=$PWD/src pytest tests/mcp -q -> 184 passed
  • PYTHONPATH=$PWD/src pytest tests -q -> 626 passed, 11 skipped
  • pre-commit run --all-files -> passed
  • PYTHONPATH=$PWD/src mkdocs build -> passed with existing Material warning and unnav'd plan notices
  • git diff --check -> passed

Local Review

  • Local subagent review found stale AGENTS wording, stale verification counts, and a missing failed-start pending-stop regression.
  • All review findings were addressed; re-check found no new must-fix issue besides staging the plan file, which is included in this PR.

Summary by CodeRabbit

  • New Features
    • Stop actions now briefly wait for sessions that are still starting, then report a timed-out stop if startup does not finish in time.
    • Sessions that finish starting after a timed-out stop are now quietly released in the background.
  • Bug Fixes
    • Improved stop-all behavior so starting sessions are handled consistently and not left active.
    • Preserved normal behavior for startups that complete within the wait window, including retries after failed startups.
  • Documentation
    • Updated user-facing docs and guidelines to describe the new stop timing behavior and outcomes.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Wangmerlyn, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 21 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 94cff730-8289-48d9-945e-5d88a2969b8d

📥 Commits

Reviewing files that changed from the base of the PR and between 1f0fa67 and 1c8fca9.

📒 Files selected for processing (8)
  • AGENTS.md
  • README.md
  • docs/concepts/architecture.md
  • docs/guides/mcp.md
  • docs/plans/stop-starting-session-timeout.md
  • docs/reference/cli.md
  • src/keep_gpu/mcp/server.py
  • tests/mcp/test_server.py
📝 Walkthrough

Walkthrough

Adds bounded waiting and pending-stop tracking to KeepGPUServer for stop_keep and stop_all operations that arrive while a session is in the "starting" state. If startup does not settle within a 10-second timeout, the job is marked pending-stop and returned as timed_out; when startup later completes, a background quiet release is triggered. Five concurrency tests and documentation updates across AGENTS.md, MCP guide, CLI reference, and a new plan document are included.

Changes

Stop Starting Session Timeout

Layer / File(s) Summary
Plan and documentation
docs/plans/stop-starting-session-timeout.md, AGENTS.md, docs/guides/mcp.md, docs/reference/cli.md
New plan document defines the bounded-wait/pending-stop/quiet-release requirements. AGENTS.md, MCP guide, and CLI reference updated to describe timed_out response and deferred quiet release behavior.
Server timeout coordination
src/keep_gpu/mcp/server.py
Adds STARTUP_STOP_WAIT_TIMEOUT_SECONDS, _pending_stop_job_ids, and _wait_for_starting_jobs_or_mark_pending. Updates stop_keep (single and all) to call the helper and return timed_out on expiry. Updates start_keep to spawn a background quiet stop if the job was previously marked pending.
Concurrency and timeout tests
tests/mcp/test_server.py
Five new tests cover: stop_keep timeout on stuck starting session, stop_all timeout, deferred release after startup completes, failed-startup pending-stop cleanup, and stop-all deduplication.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Wangmerlyn/KeepGPU#59: Both PRs modify KeepGPUServer's start_keep/stop_keep handling in src/keep_gpu/mcp/server.py; this PR extends stop/start coordination for "starting" sessions on top of the MCP server behavior added there.
  • Wangmerlyn/KeepGPU#74: This PR's bounded-wait/pending-stop handling builds directly on the lifecycle "state truth" and additive timed_out/state reporting machinery introduced in that PR.
  • Wangmerlyn/KeepGPU#77: Both PRs modify KeepGPUServer's startup-phase logic—this PR for stop coordination, that PR for reserving custom job_ids during "starting" to prevent duplicates.

Poem

🐇 A session still starting, the stop-request knocks,
I wait just a moment, then check all the clocks.
If startup is sluggish and times out, no fret—
I mark you as pending, I won't forget!
When startup completes, a quiet release I'll send,
No ghost sessions lingering — that's how it ends. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately captures the main change: bounding stop requests while sessions are starting.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/bound-startup-stop-wait

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces bounded waiting for starting sessions during stop and stop-all requests to prevent indefinite blocking when a startup hangs. If a startup does not settle within the timeout budget, the stop request returns a timeout payload, and a background thread is spawned to release the session once startup eventually completes. The reviewer suggests removing the "quiet=True" argument from the background stop thread to ensure that session releases and potential failures are properly logged for auditability and observability.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/keep_gpu/mcp/server.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
docs/plans/stop-starting-session-timeout.md (1)

1-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Move this plan into a dedicated docs subfolder.

This is a detailed plan for a non-trivial lifecycle change, but it is added directly under docs/plans/ instead of under a descriptive folder such as docs/plans/stop-starting-session-timeout/plan.md. Grouping it under its own subdirectory will keep the related documentation easier to evolve together.

As per coding guidelines, "For complex features, bug investigations, or refactorings that require detailed documentation, create a dedicated subfolder under docs/ with a descriptive name and place all related documentation files there."

🤖 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 `@docs/plans/stop-starting-session-timeout.md` around lines 1 - 85, The plan
document is placed directly under docs/plans instead of in a dedicated feature
subfolder, which makes the related documentation harder to keep together. Move
this document into a descriptive subdirectory under docs/plans for the
stop-starting-session-timeout feature and update any references so the plan
remains the canonical document; use the existing Stop Starting Session Timeout
Plan heading and the docs/plans path as the main identifiers when relocating it.

Source: Coding guidelines

🤖 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.

Inline comments:
In `@src/keep_gpu/mcp/server.py`:
- Around line 213-225: The startup-wait logic in
_wait_for_starting_jobs_or_mark_pending and the stop-all path must stop keying
state only by reusable job_id strings. Introduce an immutable per-start token or
generation in the starting snapshot so each session start is uniquely tracked
even if the same job_id is reused, and use that token when deciding
still_starting and when updating _pending_stop_job_ids. Also update the stop-all
snapshot/release flow (the code around the related wait/stop helpers) to wait
briefly for startup to settle before returning not found or marking timed-out
sessions, so a later startup cannot be misattributed to an earlier stop request.

In `@tests/mcp/test_server.py`:
- Around line 1502-1507: The blocking fake controllers in the MCP server tests
are using timed waits, which can self-unblock and make the timeout-path
concurrency tests flaky. Update the stuck-controller helpers such as
BlockingStartController.keep and the other related fake controller methods in
this test module to wait deterministically until the cleanup signals are set,
rather than using a timeout-based wait. Keep the existing event signaling in the
test cleanup/finally blocks so the tests only proceed when the stop path
explicitly releases them.

---

Nitpick comments:
In `@docs/plans/stop-starting-session-timeout.md`:
- Around line 1-85: The plan document is placed directly under docs/plans
instead of in a dedicated feature subfolder, which makes the related
documentation harder to keep together. Move this document into a descriptive
subdirectory under docs/plans for the stop-starting-session-timeout feature and
update any references so the plan remains the canonical document; use the
existing Stop Starting Session Timeout Plan heading and the docs/plans path as
the main identifiers when relocating it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: faa356b3-c863-40a1-b3a6-c4ca8d470fed

📥 Commits

Reviewing files that changed from the base of the PR and between e1edcf8 and 1f0fa67.

📒 Files selected for processing (6)
  • AGENTS.md
  • docs/guides/mcp.md
  • docs/plans/stop-starting-session-timeout.md
  • docs/reference/cli.md
  • src/keep_gpu/mcp/server.py
  • tests/mcp/test_server.py

Comment thread src/keep_gpu/mcp/server.py Outdated
Comment thread tests/mcp/test_server.py
…top-wait

# Conflicts:
#	AGENTS.md
#	README.md
#	docs/concepts/architecture.md
#	docs/guides/mcp.md
#	docs/plans/stop-starting-session-timeout.md
#	docs/reference/cli.md
#	src/keep_gpu/mcp/server.py
#	tests/mcp/test_server.py
@Wangmerlyn

Copy link
Copy Markdown
Owner Author

CodeRabbit nitpick on plan location: keeping docs/plans/stop-starting-session-timeout.md in place. This matches the repository convention already used by the many existing docs/plans/*.md implementation plans and is the path now present on origin/main; moving it into a one-off subdirectory would create inconsistency for this plan-only artifact.

@Wangmerlyn
Wangmerlyn merged commit b7b4edf into main Jun 29, 2026
5 checks passed
@Wangmerlyn
Wangmerlyn deleted the codex/bound-startup-stop-wait branch June 29, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant