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
services: - 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.
pr-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.
System Info
next-d05c597374 kubernetes backend, bitbucket datacenter forgeAdditional context
No response
Validations
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Assistant
Let me explore the repository structure to understand the codebase and find the relevant code related to pipeline services and step lifecycle management.
Assistant
Now I understand the issue. When a service (detached step) is running, the
runDetachedStepstarts it and returns immediately. The background goroutine waits for the step to complete viacompleteStep, which callsWaitStep. For services like postgres that run indefinitely,WaitStepblocks forever. The pipeline then finishes (all blocking steps complete), anddestroyWorkflowFuncis called which destroys the workflow (and the pods). But the background goroutine for the detached step is still blocked inWaitStep, sotraceStepis never called with a final state, leaving the service step as "running" in the API.Let me look at the DestroyWorkflow implementation for kubernetes backend to understand how it destroys the workflow.
Assistant
Now I understand the problem. Let me trace the flow:
-
For a detached (service) step,
runDetachedStepstarts a goroutine that callscompleteStep, which callsWaitStep. For an infinite-running service like postgres,WaitStepblocks forever (waiting for the pod to reach a terminal state). -
The blocking steps complete, the pipeline finishes,
destroyWorkflowFuncis called, which callsDestroyWorkflow, which stops all pods including the service pod. -
When the service pod is destroyed by
DestroyWorkflow, theWaitStepin the background goroutine's informer should detect the pod reaching state (since it's being deleted). Actually, when a pod is deleted, does the informer get an update event? It gets a delete event, not an update event. The only handles . So when the pod is deleted, the informer receives a delete event, but is only registered for , not . So is never closed.
-
Assistant
Let me look at the docker backend's WaitStep to understand the comparison.
Assistant
Let me check the git history for changes to the runtime/step lifecycle to understand the regression.
Assistant
Now I understand the design intent. The
Fix workflow hang on servicescommit (#6507) established a contract:DestroyWorkflowmust cause theWaitStepof running (detached/service) steps to return. The dummy backend implements this via a stop channel closed inDestroyWorkflow. The docker backend implements it implicitly (removing the container causesContainerWaitto return). But the kubernetes backend does NOT implement this contract — itsWaitSteponly listens to the informer'sUpdateFuncfor pod phase changes to Succeeded/Failed/Unknown and context cancellation. WhenDestroyWorkflowdeletes the pod,WaitStepdoesn't detect it.Let me look at the helper functions and pod utils in the kubernetes backend.
Assistant
Let me look at the dummy backend's full
WaitStepto understand the expected contract behavior.Assistant
So for the dummy backend, when
DestroyWorkflowcloses thewcchannel:- For a service step:
sleepWithContext(ctx, wc, testServiceTimeout)returnsfalse(thecase <-stopreturns false). Then theelsebranch runs and returns an error "WaitStep fail due to timeout of service after 1 second" with&backend_types.State{Error: err}.
Wait, that returns an error. But hold on, let me re-check the test. Let me look at the test
TestWorkflowWithServiceStepand the unbounded service test to understand what the expected final state is.- For a service step:
Assistant
Let me find the unbounded service test added in #6507.
Assistant
Let me look at the kubernetes backend tests to understand the test setup.
Assistant
Let me look at how the server processes the trace state to determine step status.
Assistant
This is very informative. There's a
UpdateStepToStatusSkippedfunction with a comment "for daemons that are killed". Let me find where it's called.Assistant
There's a
completeChildrenIfParentCompletedthat marks running children asStatusKilled. Let me see where this is called - it's probably the workflow completion handler. Let me look at the "Done" handler in rpc.go.Assistant
I now have a clear understanding. The root cause: in the kubernetes backend,
WaitSteponly listens to the informer'sUpdateFuncfor pod phase transitions to Succeeded/Failed/Unknown and context cancellation. WhenDestroyWorkflowdeletes a service pod,WaitStepdoesn't detect it — so the detached goroutine never completes,r.uploadWait.Wait()hangs,Runnever returns, the agent never callsDone, and the server keeps the service step as "running" until the pipeline times out.Let me check the imports and apierrors usage in the kubernetes backend.
Assistant
Now let me look at the exact
WaitStepcode to edit it.Assistant
Let me look at how the tracer maps
backend_types.Statetorpc.StepState.Assistant
Let me look at the tracer creation to understand how step state (backend_types.State) is mapped to rpc.StepState.
Assistant
Now I have a full understanding. Let me implement the fix in the kubernetes backend's
WaitStep. First, add the import for k8s api errors.Assistant
Now let me add the import for k8s api errors.
Assistant
Now let me modify the
WaitStepfunction to detect pod deletion.Assistant
The WaitStep podUpdated is at line 294. Let me view it again with line numbers.
Assistant
Now let me rewrite the
WaitStepfunction with deletion detection.Assistant
Let me build to make sure it compiles.
Assistant
GOMODCACHE is empty (it printed the current directory contents). Let me check go env more carefully.
Assistant
GOMODCACHE and GOPATH are not set in go env. But the modules are in /root/go/pkg/mod. Let me set GOPATH and GOMODCACHE explicitly.
Assistant
The build succeeds. Let me view the full modified
WaitStepto verify correctness.Assistant
The changes look good. Now let me run the existing kubernetes tests to make sure I didn't break anything.
Assistant
Now let me create a temporary test to reproduce the issue and verify the fix. I'll create a test file, run it, and remove it before submitting.
Assistant
Both tests pass with my fix. Now let me verify that the tests would FAIL without my fix, to confirm the fix addresses the issue. Let me temporarily revert the
WaitStepchanges via git stash.Assistant
The tests FAIL without my fix (WaitStep hangs, matching the issue) and PASS with my fix. This confirms the fix resolves the issue.
Now let me run the full kubernetes test suite and the runtime tests to make sure nothing is broken.
Assistant
The dummy backend build failed. Let me check why — this might be unrelated to my change.
Assistant
The dummy backend has a
//go:build testconstraint. The runtime tests need thetesttag. Let me run the runtime tests with thetesttag.Assistant
The runtime tests pass. Now let me run the broader build and the server rpc tests to ensure nothing broke.
Assistant
The web embed error is pre-existing (dist/* not built, unrelated to my change). Let me verify the exit code and build the relevant packages.
Assistant
The only build error is the pre-existing
web/web.goembed issue (requires building the web UI with npm). Let me confirm it's pre-existing by stashing my changes.Assistant
Let me check the default
StopTimeout(grace period) used byDestroyWorkflow.Assistant
Default
StopTimeoutis 20 seconds. So afterDestroyWorkflowdeletes the pod, there's a grace-period window. However, there's a subtle timing concern:waitForLogs()(called beforeWaitStepincompleteStep) blocks until the service's log stream ends, which happens when the pod is terminated during deletion. By the timeWaitStepstarts its informer, the pod may already have itsDeletionTimestampset but no further Update events may fire until the pod is fully removed (Delete event after the grace period) — causing up to a 20s delay, or a hang in the force-delete race.To make the fix robust and avoid relying solely on informer events, let me add an initial
Getcheck after starting the informer.Assistant
Now let me add an initial
Getcheck after starting the informer, soWaitStepdoesn't solely depend on informer events (handles the race where the pod is already being deleted/gone when the informer starts).Assistant
Let me verify the change and rebuild/retest.
Assistant
Both still pass. Let me add a test for the race scenario: the pod is already gone (force-deleted) before
WaitStepis even called, to verify the initial Get check handles it.Assistant
Let me fix the unused variables in the test.
Assistant
All four tests pass. The "PodAlreadyGone" and "PodAlreadyDeleting" tests return in ~0.00s, confirming the initial poll handles the race. Let me run the full existing kubernetes test suite and runtime tests once more to confirm nothing broke.
Assistant
All existing tests pass, including the goroutine-leak test. Now let me run the runtime tests and the agent tests to ensure the broader lifecycle still works.
Assistant
Let me check the CHANGELOG to see if I should add an entry (following project convention).
Assistant
The CHANGELOG is organized by released version with no "unreleased" section, so I won't modify it. Now let me remove the temporary test file before submitting.
Assistant
Let me do a final review of the diff.
Assistant
The diff looks good. Let me do a final build and full kubernetes test run to confirm everything works.
Assistant
Everything passes. Let me verify the final git status is clean (only the source change, no temp files).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.