fix(reaper): recover from stopped, orphaned and unready reapers #3868
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3868"
general
weight: 0.35
stevenh
<!-- Type of change
Please label this PR with one of the existing labels, depending on the scope of your change
-->
What does this PR do?
<!-- Mandatory
Explain here the changes you made on the PR. Please explain the WHAT: patterns used, algorithms implemented, design architecture, etc.
-->
Makes the reaper recover from a reaper container that will never become ready, instead of waiting on it until the 60 second startup timeout. A reaper found by another test process that has exited, is dead or being removed, or has stayed created but not started for longer than a 10 second grace period, measured against the daemon's clock, is removed and a new one is created. A paused or restarting reaper, or one whose age can't be determined, is kept and retried, as other test processes may depend on it.
Readiness is now checked with a handshake with Ryuk itself: the wait strategy sends the session's filters and waits for Ryuk's ACK, giving each attempt one second and the whole wait 15 seconds. The previous log and port checks could pass without Ryuk listening, as the docker-proxy accepts connections on a published port regardless, and the Ryuk image has no shell for the internal port check. A reaper whose port Docker never published fails the handshake straight away and is force removed and recreated. The connection Ryuk acknowledged is handed to the Reaper as its first client, so a slow daemon can't let a new reaper reach its reconnection timeout before anything connects to it.
Running a new reaper is no longer cancelled with the caller's context, as abandoning a create that the daemon had completed left a reaper that was created but never started, blocking its name for the rest of the session. The run is bounded by its own timeout instead, logs to the default logger rather than the caller's once the caller has returned, and removes its container if it fails. The reaper image is pulled beforehand with the caller's context, under the same name CreateContainer would use, and the run uses the provider's client, so a client set with SetClient is honoured. RYUK_PORT is now passed to the container, so Ryuk listens on the port that is exposed.
Reaper.Connect, and the connections the spawner makes, now wait up to five seconds for Ryuk to acknowledge the session's filters, and return an error if it doesn't. Previously the handshake ran in the background and a failure was only logged, leaving the session's resources unregistered for cleanup. Callers of the deprecated NewReaper, such as the compose module, now see that error. A reaper that accepts the connection but closes it without acknowledging, as Ryuk does once it starts shutting down, is recreated rather than failing.
Podman returns an internal error, rather than not found, for a container removed while a request for it is being handled, such as a reaper removed automatically once it exits. That made the spawner fail permanently instead of recreating the reaper, so it is now treated like a container not found, which also lets terminating such a container succeed.
Reusing a reaper now makes half the Docker API requests it did, as the lookup uses the caller's provider rather than a new client for each attempt, and the reaper's endpoint is the one the readiness probe connected to rather than being inspected again. A new reaper's run also starts with the caller's default network, rather than finding it again.
Why is it important?
<!-- Mandatory
Explain here the WHY, or the rationale/motivation for the changes.
-->
When multiple test processes of one
go test ./...invocation share a session, they share one reaper, and Ryuk exits after its reconnection timeout once no client is connected. A large multi-package run has natural gaps with no active containers, so the reaper legitimately dies mid-run. The next package that needed a container then found the stopped container and waited on it for the full 60 second startup timeout, failing withreaper: from container "xxxx": wait for reaper xxxx: context deadline exceeded. Because package scheduling on a fixed CI runner is roughly deterministic, the same package kept failing, making a library race look like a flaky test.The same 60 second wait happened for a newly created reaper when Docker Desktop didn't publish its port, and a reaper orphaned in the created state made every later lookup in the session fail. Each of these turned a recoverable reaper problem into a failed test run.
Related issues
<!-- Recommended
Link related issues below. Insert the issue link or reference after the word "Closes" if merging this should automatically close it.
-->
How to test this PR
Run the reaper tests, and the container tests that use the reaper, against a local Docker daemon. This passed in about 87 seconds against Docker Desktop 29.9.2:
go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.14.0 run -c .golangci.yml .reports no issues.The unpublished port case doesn't reproduce on demand, and didn't occur in 1,200 unmodified runs, so it was stress tested with a Docker API proxy that reports no published ports for one in four Ryuk containers. Running
TestReaper_reuseItFromOtherTestProgramUsingDockerthrough it, 9 of 30 runs failed after about 60 seconds before this change, and none of 60 failed after it, recovering from 17 unpublished reapers.<!-- Optional
Follow-ups
Add here any thought that you consider could be identified as an actionable step once this PR is merged.
-->
Follow-ups
TestReaper_reuseItFromOtherTestProgramUsingDockeralways uses the test session's reaper and stops it on cleanup, so concurrent copies of it stop the reaper other copies are using. It only runs in one process in CI, but would need a reaper of its own to be run concurrently. Connecting a reused reaper to the requested network is tracked separately in #3941.