Skip to content

imcp2-deploy: bound what the hosted deploy tools hold and how they stop - #249

Closed
raymondk wants to merge 4 commits into
deploy-5-hostedfrom
deploy-5b-hardening
Closed

raymondk wants to merge 4 commits into
deploy-5-hostedfrom
deploy-5b-hardening

Conversation

@raymondk

Copy link
Copy Markdown
Contributor

Summary

Stacked on #243. Addresses the review findings there that are about what the shared server can be made to hold, and what happens to a deploy when the server or the client goes away. Nothing here changes what a deploy does when it runs to the end.

Finding on #243 Change
A session can loop mint → PUT and fill the state filesystem One upload per session at a time (a new one replaces the held one; a second PUT while one is arriving answers 409), and a store-wide budget, MCP_DEPLOY_MAX_STORED_UPLOAD_BYTES (default eight archives' worth), reserved before the body is read (507 past it). An archive counts until its deploy lets go of it.
A dropped receive future leaves a partial file nothing sweeps A guard owns the file and the reservation until the upload is registered.
A restart orphans uploads; expiry only runs on store activity Making the store removes the archives an earlier process left, and a sweep runs every minute.
An interrupted deploy keeps its whole extracted directory The directory is cut down to .icp/data/mappings (the ids). A start does the same for deploys a killed process left, and records older than seven days are removed.
The concurrency cap is per mount (2× with MCP_SERVE_BETA=1) and per mcp_router() call One Deploys per process holds the slots, the upload store and the stop signal; McpServer::with_deploy now takes it.
TimeoutStopSec=30 kills running deploys on restart On the shutdown signal, running deploys are told to stop before the drain: each ends at its next step and answers with the canisters it created. A deploy asked for meanwhile is refused.
A disconnect during preflight is not noticed The stop is checked before extraction, project load, bundle check and seeding; stopped there, the call is refused with "nothing was created" and no directory is kept.
The router test calls mainnet The made-up session's grant is set as lapsed, so the identity is refused locally.

API changes

  • imcp2::deploy::Deploys is new; McpServer::with_deploy takes it instead of a DeployConfig. DeployConfig gains max_stored_upload_bytes.
  • imcp2_deploy: DeploySlots, IcDeployTools::with_slots and ::stopped_by, Uploads::with_budget and ::sweep, prune_abandoned_deploys, remove_old_deploy_records; ReceiveError gains Busy and Full.
  • imcp2-local is affected in one way: an interrupted deploy's directory now holds only the id record. The skill text already points at that file.

Not in this PR

The sync-plugin memory limit, the crates.io packaging of imcp2 with an unpublished imcp2-deploy, and correlating notifications/cancelled with a running deploy are still open on #243.

Testing

cargo test --workspace --all-targets and cargo test -p imcp2 --features deploy --all-targets pass, clippy is clean on both. New tests cover each row above; the interruption test is now deterministic (it stops the deploy from the log line that says it started).

🤖 Generated with Claude Code

https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV

What review found a write-enabled session, a restart or a disconnect could
leave behind on the shared server, each closed:

- Uploads have a budget. A session has one upload at a time (a PUT while
  another of its own is arriving is refused, and one that begins replaces
  the archive it was holding), and every archive the store has counts
  against MCP_DEPLOY_MAX_STORED_UPLOAD_BYTES, reserved before the body is
  read. A PUT abandoned mid-body takes its partial file and reservation
  with it.
- A start clears the run before: uploads nothing can deploy are removed,
  and a cut-off deploy's directory is cut down to the ids it recorded. A
  sweep every minute removes lapsed uploads on an idle server and id
  records older than seven days.
- An interrupted deploy keeps its id record, not its whole bundle.
- The deploy slots and the upload store are the process's: /mcp and
  /mcp-beta, and every router built from either, share one Deploys.
- On the shutdown signal running deploys are told to stop before the
  drain, so each answers with the canisters it created instead of being
  killed with the unit; a deploy asked for meanwhile is refused.
- A stop during extraction, loading or checking ends the deploy there,
  before anything is created.
- The hosted router test no longer asks Internet Identity for a
  delegation: the made-up session's grant has lapsed, so it is refused
  locally.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
@raymondk
raymondk requested a review from a team as a code owner November 18, 2026 18:07
Copilot AI max review requested due to automatic review settings November 18, 2026 18:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical cancellation and cleanup flaws can orphan uploads and extracted bundles or lose accepted canister-creation results, risking resource exhaustion and duplicate funded canisters on retry.

5 open issues
What changed in this PR

This PR hardens hosted deployment resource limits and lifecycle handling, building on #243 without changing the intended successful deploy flow.

Changes:

  • Adds process-wide upload budgets and shared deployment slots across endpoints.
  • Connects shutdown and disconnect signals to deploy interruption and preflight checks.
  • Adds startup and periodic cleanup, with supporting tests and documentation.
File Description
src/​main.rs Parses upload budgets, initializes shared deploy state, and signals shutdown.
src/​lib.rs Accepts shared Deploys state when configuring MCP servers.
src/​deploy.rs Adds shared state, periodic cleanup, upload error responses, and router tests.
README.md Documents upload limits, interruption, and record retention.
deploy/​native/​README.md Describes native deployment limits and restart behavior.
deploy/​native/​imcp2.service Updates comments describing graceful shutdown; service settings are unchanged.
crates/​imcp2-deploy/​static/​skills/​deploy-with-mcp.md Updates upload replacement, retry, and restart guidance.
crates/​imcp2-deploy/​src/​uploads.rs Adds storage reservations, receive guards, replacement rules, and cleanup tests.
crates/​imcp2-deploy/​src/​lib.rs Exposes shared slots and connects server stop signals to deploy tools.
crates/​imcp2-deploy/​src/​deploy.rs Adds preflight stop checks, interrupted-directory pruning, and record expiry helpers.

Review effort: Max

Comment thread crates/imcp2-deploy/src/deploy.rs Resolved
Comment thread crates/imcp2-deploy/src/uploads.rs Resolved
Comment thread src/main.rs Resolved
Comment thread crates/imcp2-deploy/src/lib.rs Resolved
Comment thread crates/imcp2-deploy/src/uploads.rs Resolved
…e deploy runs, a refused replacement, and read-only bundle directories

- A stop no longer drops a deploy mid-call. A canister call in flight may
  already have created and funded a canister that only its answer names,
  so the calls are wrapped (`UntilStopped`): one in flight runs to its
  answer, which the deploy records as always, and any asked for after the
  stop is refused unmade, so the run ends by itself within a step. It is
  given twenty seconds to, and the answer says so if a call was given up
  on. A run that has made no call is still dropped where it is.
- The stop is in place before the deploy's identity is fetched, and that
  wait gives way to it: a request no longer holds its slot and its upload
  through a network wait, deaf to a stopping server.
- A session's replacement upload that is refused for room leaves the
  upload it held: the held bytes count as room, and the held archive goes
  only once the new one is let in.
- Extraction gives every file and directory the owner's own mode instead
  of the archive's, so a directory an archive marks read-only cannot leave
  a tree the server cannot remove.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
Copilot AI max review requested due to automatic review settings November 18, 2026 23:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread crates/imcp2-deploy/src/deploy.rs Resolved
Comment thread crates/imcp2-deploy/src/deploy.rs Resolved
if !begun.load(Ordering::SeqCst) {
return None;
}
tokio::time::timeout(grace, run).await.ok()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed that the outer timeout cannot fire while a plugin holds the thread, and not fixed here. Two things bound it today: a plugin's canister calls go through the same wrapped calls, so each is refused once the stop is set, and the upstream runtime enforces its own epoch deadline on plugin CPU time. A hard deadline inside the runner has to come from icp-sync-plugin; leaving this open to track that.

Comment thread crates/imcp2-deploy/src/host.rs Resolved
Comment thread crates/imcp2-deploy/src/uploads.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
…ctory has one owner, and an interrupted deploy claims no more than it knows

- A stopping process waits for its deploys to have ended before it exits
  (`Deploys::stopped`, the grace and a margin): a deploy waiting on a call
  in flight whose client has gone holds no connection for the drain to
  wait on, and the process would have exited under it.
- A deploy that finishes after it was asked to stop keeps its record, as
  an interrupted one does: the client that went away never read its ids.
- An interrupted deploy's answer passes on what its run ended with, and
  warns when a call went unanswered that the canister it was creating may
  exist unlisted; the skill says what to do about it before a retry.
- The deploy directory is locked before a start clears it, so a second
  server over a state directory in use is refused instead of removing the
  first one's uploads and deploys. `Deploys::new` returns a `Result`.
- An extracted entry's mode follows what it is on disk, not its header:
  an old-style header names a directory as a regular file ending in `/`.
- An upload's file is created before the first wait, so an abandoned
  request cannot have the file appear after its cleanup has run.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
Copilot AI max review requested due to automatic review settings November 19, 2026 01:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread src/deploy.rs Resolved
…mes no command that cannot find a lost canister

- The lock on the deploy directory is held by a router's state too. A
  router outlives the `Deploys` it was made from if its embedder lets it,
  still receiving uploads and running deploys there; with the lock gone, a
  second server would have cleared them as leftovers.
- The skill no longer offers `icp canister list` as the way to find a
  canister whose creation went unanswered: it lists the project's
  canister names, not what an identity controls. It says that nothing here
  can find it, and that the user must before a retry.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XSXgo38kH12wagL3rstXfV
Copilot AI max review requested due to automatic review settings November 19, 2026 02:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because ownership-lifetime and parallel-create error handling risks could cause live-file deletion or unsafe retries, while key recovery guarantees remain unverified by the tests.

1 open issue
2 resolved since last review

Review effort: Max

@raymondk

Copy link
Copy Markdown
Contributor Author

Superseded by the stack #254 → #259, which carries the same code in six PRs from main, with the deploy tools living in imcp2-core instead of a separate crate. The hardening is part of #255, #256 and #258.

@raymondk raymondk closed this Nov 19, 2026
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.

2 participants