← Back to Pull requests

Clean up isolation resources when workload setup fails #7049

Closed
unknown closed this pull request without merging on Nov 26, 2026 09:35 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 21:59 UTC
Author

Summary

When a network-isolated workload fails during setup before its MCP container exists, its toolhive-<name>-internal network and <name>-dns container (plus egress, if it was created) are left behind. thv rm cannot remove them, because RemoveWorkload derives their names from the MCP container. Each leaked network uses up one of Docker's address pools, so after enough failures every new workload fails with "all predefined address pools have been fully subnetted".

  • DeployWorkload now has a deferred hook, registered after the internal network is created. It runs a new cleanupFailedIsolation when setup fails before createMcpContainer succeeds.

  • cleanupFailedIsolation removes only the sidecars this attempt started to create (DNS, and egress once its setup was reached; ingress is set up after the MCP container, so never), and only containers carrying the auxiliary workload label, so a user workload named like a sidecar (e.g. app-egress) survives. It then deletes the internal network. It is best-effort: failures are only logged, so the caller still gets the original error. It tries every such sidecar and always deletes the network, even if an earlier removal fails. It runs with context.WithoutCancel plus a 30 s timeout, so a cancelled request still cleans up without being able to hang.

  • Cleanup only runs for a new workload. A restart reaches DeployWorkload with the stopped MCP container still in place, so the network and sidecars belong to the existing workload and are left alone when a restart attempt fails. If the existence lookup itself fails, it is treated as existing.

  • Failures after the MCP container exists are unchanged, as the issue describes, because thv rm already cleans up through RemoveWorkload in that case. Non-isolated deploys and a failed createNetwork are unchanged too.
  • Fixes #7045

    Type of change

  • Bug fix

  • New feature

  • Refactoring (no behavior change)

  • Dependency update

  • Documentation

  • Other (describe):
  • Test plan

  • Unit tests: go vet and go test ./pkg/container/docker/ (the changed package; the full task test was not run)

  • E2E tests (task test-e2e)

  • Linting (task lint-fix)

  • Manual testing (describe below)
  • TestDeployWorkload_SetupFailureCleanup is one table (transport, isolation, deploy options, existing MCP container) that pins when a failed deploy removes the isolation resources: only for a new workload that fails before its MCP container exists (DNS creation, gateway discovery, egress setup, an invalid host port, or MCP container creation). Each case also checks the returned error. Cleanup does not run when network creation fails, when the deploy succeeds, after the MCP container exists (MCP IP lookup or ingress setup failing, which thv rm handles), without isolation, or on a restart (existing MCP container, or a failed existence lookup). TestDeployWorkload_CleanupSurvivesCancelledContextWithDeadline cancels the caller's context from inside the failing DNS step and checks that cleanup still runs, with a live context that has its own deadline. TestCleanupFailedIsolation runs the real cleanup against a fake Docker daemon: the attempted sidecars and the network are removed, a sidecar that was never attempted or a same-named container without the auxiliary workload label is left alone, a sidecar that fails to remove does not stop the others or the network deletion, and an absent network is not an error. The cleanup cases fail on main and pass with this change; the restart cases fail without the existing-container check.

    Does this introduce a user-facing change?

    Yes. A failed setup of a network-isolated workload no longer leaves its internal network and DNS/egress containers behind.

    Special notes for reviewers

    The other trigger in #7043 (removing a workload whose start is still in flight) is not addressed here.

    This change was prepared with the help of an AI coding assistant (Claude).

    Diff: 499 lines (+374 / -125) across 6 files Reviewer: pcbeingused333 Review duration: 32376m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 499 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.372
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7049"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.5% (MEDIUM) · Higher = larger blast radius
    47.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.8% · Safe to merge
    18.8%