Clean up isolation resources when workload setup fails #7049
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
pcbeingused333
Summary
When a network-isolated workload fails during setup before its MCP container exists, its
toolhive-<name>-internalnetwork and<name>-dnscontainer (plus egress, if it was created) are left behind.thv rmcannot remove them, becauseRemoveWorkloadderives 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".DeployWorkloadnow has a deferred hook, registered after the internal network is created. It runs a newcleanupFailedIsolationwhen setup fails beforecreateMcpContainersucceeds.cleanupFailedIsolationremoves 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 withcontext.WithoutCancelplus a 30 s timeout, so a cancelled request still cleans up without being able to hang.DeployWorkloadwith 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.thv rmalready cleans up throughRemoveWorkloadin that case. Non-isolated deploys and a failedcreateNetworkare unchanged too.Fixes #7045
Type of change
Test plan
go vetandgo test ./pkg/container/docker/(the changed package; the fulltask testwas not run)task test-e2e)task lint-fix)TestDeployWorkload_SetupFailureCleanupis 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, whichthv rmhandles), without isolation, or on a restart (existing MCP container, or a failed existence lookup).TestDeployWorkload_CleanupSurvivesCancelledContextWithDeadlinecancels 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.TestCleanupFailedIsolationruns 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 onmainand 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).