ISSUE DESCRIPTION Woodpecker services appear to be running on UI & API after pipeline…
- User
ISSUE DESCRIPTION
Woodpecker services appear to be running on UI & API after pipeline completes
Component
server
Describe the bug
Some regression with newer releases (3.14.x) With kubernetes backend and pipelines defining services, the services are not cleaned properly. The pods from kubernetes backend are destroyed properly, but the woodpecker API still show these steps as state=running.
These are only cleaned after the pipeline times out. This prevents timely deploys as pipeline executions are not getting green status (although, as a mitigation, the pipeline waiting services can be cancelled, after which the deploy can be made).
Steps to reproduce
- Install woodpecker next-d05c597374
- Use kubernetes backend
- Define pipeline with services running infinitely, like running postgres
codeservices: - name: postgres image: public.ecr.aws/docker/library/postgres:16-alpine3.21 ports: - 5432 environment: POSTGRES_PASSWORD: somepass POSTGRES_USER: someuser POSTGRES_DB: somedb steps: - name: wait-for-postgres image: public.ecr.aws/docker/library/postgres:16-alpine3.21 environment: DB_HOST: postgres DB_PORT: 5432 MAX_WAIT: 60 commands: - ./scripts/wait-for-postgres.sh- Run the pipeline, observe pods start for the service and the step
- once wait-for-postgres step completes, the pods are destroyed, but API still shows the service as running, also visible on Web UI.
codepr-build > clone (#2): Step: clone Started: 1779195580 Stopped: 1779195594 Type: clone State: success pr-build > postgres (#3): Step: postgres Started: 1779195594 Stopped: 0 Type: service State: running pr-build > wait-for-postgres (#4): Step: wait-for-postgres Started: 1779195596 Stopped: 1779195602 Type: commands State: successExpected behavior
Pipeline services are stopped once the pipeline steps complete, the pipeline completes.
- Assistant
I’ll first map the server/runner code involved in service lifecycle and inspect recent history around service state updates. Then I’ll reproduce the behavior with the narrowest relevant tests, patch the lifecycle handling, and run focused plus broader tests.
- Assistant
The initial search was too narrowly scoped (and produced no lifecycle matches), so I’ll enumerate the Go packages and search globally for step state transitions, service execution, and pipeline finalization. I’ll also inspect commits since the reported revision for any potentially related upstream fix.
- Assistant
Two recent commits explicitly target this symptom. I’ll inspect their ancestry and diffs. If the first fix introduced a regression and the second corrects it, the safest resolution is likely to apply the already-reviewed upstream correction rather than invent a divergent fix.
- Assistant
Both fixes are already ancestors of the reported and current revisions. The runtime now destroys the backend before waiting for traces, and RPC force-completes still-running children on
Done; nevertheless, the symptom implies the final service trace may arrive afterDoneand overwrite the forced terminal state back to running. I’ll inspect current runtime tracing and RPC update ordering, plus the KubernetesWaitStepbehavior, to verify that race hypothesis. - Assistant
The runtime ordering rules out a late trace after
Done:Runwaits for detached completion and trace upload before the agent sends workflow completion. The more likely defect is that Kubernetes deletion makesWaitStepreturn a state that is not considered exited; the tracer then sends another “running” update, and RPCDonemust repair it. Since the API still shows running, I’ll examineUpdate, status conversion,completeChildren..., and the exactWaitStepresult on pod deletion. - Assistant
The current runtime tests assert that services produce an exited trace, but only against the dummy backend. I’ll inspect Kubernetes’ watcher implementation and its tests directly; this backend-specific contract is the likely gap. I’ll also inspect status sanitization because a terminal workflow may reject—or accept—the final service trace depending on timing.
- Assistant
The defect is now reproduced at code level: Kubernetes
WaitSteponly listens to pod updates reaching terminal phases. Normal workflow teardown deletes service pods, emitting a delete event, so the detached goroutine never returns;Runremains stuck inuploadWait, and the agent never invokes RPCDone. This precisely matches pods disappearing while API state remains running until timeout.I’ll design the backend fix to handle matching pod deletion (including informer tombstones) as a successful, exited service lifecycle result, while preserving existing terminal-state inspection for naturally exited pods. I’ll inspect teardown semantics and test scaffolding before editing.
- Assistant
A robust fix needs more than a
DeleteFunc: a fast pod may terminate or be deleted before the watch is established, and the current update-only handler already misses fast terminal pods. I’ll refactorWaitStepto:- process initial add and update events,
- process delete events and tombstones,
- wait for informer cache synchronization,
- treat an already-absent pod after synchronization as teardown completion,
- consume the terminal pod object from the event rather than re-fetching it (avoiding deletion-after-terminal races),
- return
Exited: truefor teardown deletion so the server records a terminal service state.
Per the environment constraint, I will not modify or add repository tests; I’ll verify using existing tests and an external temporary harness if needed.
- Assistant
One important ordering detail strengthens the diagnosis:
completeStepdrains the followed log stream before callingWaitStep. Pod deletion closes that stream, so by the timeWaitStepstarts, the pod can already be absent and no delete event can ever arrive. Thus cache synchronization plus absence detection is essential, not optional. I’ll now replace only the KubernetesWaitStepwatcher logic, leaving public behavior for naturally terminated pods unchanged. - Assistant
The source change now handles all identified event orderings and passes formatting/diff checks. I’ll run the existing Kubernetes backend tests, including race detection because informer callbacks and the one-shot completion channel are concurrent. This also validates cancellation and goroutine cleanup behavior already covered by the repository.
- Assistant
The test command was blocked by environment configuration, not code: both
GOMODCACHEandGOPATHare unset. I’ll supply an isolated/tmpGOPATH (without changing repository files) and rerun the focused tests. - Assistant
Dependencies downloaded successfully, but Go then found a second unset environment requirement (
GOCACHE, with noHOME/XDG_CACHE_HOME). I’ll set both GOPATH and GOCACHE under/tmpand rerun; these remain external to the repository. - Assistant
Focused Kubernetes tests and race tests pass. I’ll now run adjacent runtime and server RPC tests because this fix relies on their lifecycle contract (runtime waits for detached traces, then RPC completes workflow state). I’ll also run
go veton the modified package and inspect the final diff/status. - Assistant
The adjacent runtime/RPC tests, vet, formatting, and diff checks all pass. To explicitly reproduce the reported failure without adding or modifying repository tests, I’ll create an external
/tmptest overlay. It models the exact post-log-drain state: a service pod has already been deleted whenWaitStepbegins. I’ll run it once over the originalHEADsource (expected timeout/failure), then over the working source (expected immediate exited success), and remove the temporary files afterward. - Assistant
The external overlay successfully reproduced the regression on the original source (
context deadline exceeded) and passed on the fix withExited: true. Temporary files were removed. I’ll perform one final source/status inspection to ensure only the intended production file changed, then submit.Checklist:
- Reproduced reported error: Yes—an external overlay test modeled
WaitStepstarting after service pod deletion; originalHEADtimed out. - Confirmed fix resolves it: Yes—the same overlay test passed against the working source and returned an exited state.
- Considered edge cases: Yes—initially absent pods, normal terminal add/update events, live delete events, deletion tombstones, terminal/delete races, context cancellation, and informer synchronization are handled.
- Ran affected existing tests: Yes—Kubernetes tests passed normally and under
-race; runtime and RPC tests passed; Kubernetesgo vetand diff checks passed.
- Reproduced reported error: Yes—an external overlay test modeled
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.