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
I'll start by exploring the repository structure to understand the codebase and then find the relevant code related to this issue.
Assistant
Let me explore the issue. The problem is about services not being properly stopped after pipeline steps complete. Let me look at the pipeline and server directories, especially the step lifecycle handling.
Assistant
Now I see the
runDetachedStepfunction. This is what's used for services (detached steps). Let me understand the issue.When a step is detached (like a service), the
runDetachedStepfunction starts the step and returns immediately. The background goroutine waits for the step to complete. But for services, the step runs indefinitely. When the pipeline completes (all non-detached steps complete), the workflow'sRunmethod callsdestroyWorkflowFunc()which destroys the workflow (stops all containers including services).But the issue is that the detached step's background goroutine calls
completeStepwhich callsWaitStepfirst. TheWaitStepis waiting for the container to finish. WhenDestroyWorkflowis called, it destroys the container, soWaitStepshould return. ThencompleteStepwould proceed toDestroyStep.Wait, but the issue is the trace. Let me think about the timing.
When the pipeline's blocking steps complete,
Runcalls which destroys the workflow. This stops the service containers. The detached goroutine's should return after the container is destroyed. Then it traces the step state.Assistant
Let me look at the runtime_test.go for the detached step tests, especially around line 1156 and 1290-1360 to understand the contract.
Assistant
Let me look at the unbounded service test more closely and the dummy backend.
Assistant
These tests
TestWorkflowUnboundedServiceDoesNotHangandTestWorkflowUnboundedDetachedDoesNotHangcheck that the workflow doesn't hang. But the issue here is different - the workflow doesn't hang (it tears down the service via DestroyWorkflow), but the service step's state is not properly traced as stopped/exited.Assistant
Now I see the issue. In the kubernetes backend,
WaitStepuses a shared informer that watches for pod updates. Thefinishedchannel is closed when the pod reachesPodSucceeded,PodFailed, orPodUnknownstate (or image pull backoff).Assistant
Now I understand the dummy backend. In
DestroyWorkflow, it closes thewcchannel (the workflow stop channel). InWaitStep, thesleepWithContextfunction selects onwc(the stop channel). So whenDestroyWorkflowis called, thewcchannel is closed, andWaitStepreturns. That's why the dummy backend works -WaitStepis unblocked byDestroyWorkflow.But in the kubernetes backend,
DestroyWorkflowcallsstopPodwhich deletes the pod, butWaitStep's informer only watchesUpdateFuncand checks for terminal phases. When a pod is deleted, it might transition through "Terminating" but the phase might not be Succeeded/Failed. Actually, deleting a pod with grace period - the pod goes into "Terminating" status but the phase stays whatever it was (Running). The informer might not even get an UpdateFunc with a terminal phase - it might get a DeleteFunc instead.Assistant
Let me look at
stopPodand the pod-related functions.Assistant
Let me check the git history for recent changes to the workflow/step runtime code.
Assistant
Let me look at the docker backend's WaitStep and DestroyWorkflow for comparison.
Assistant
So in the docker backend,
WaitStepusesContainerWaitwhich blocks until the container exits. WhenDestroyWorkflowcallsDestroyStep(which stops and kills the container),ContainerWaitreturns (because the container was killed). So docker'sWaitStepis unblocked byDestroyWorkflow/DestroyStep.In the kubernetes backend,
WaitStepuses an informer watching for the pod's terminal phase. WhenDestroyWorkflowdeletes the pod, the pod doesn't transition to a terminal phase (it just gets deleted), soWaitStepstays blocked.So the fix is to make the kubernetes backend's
WaitStepdetect pod deletion. We should add aDeleteFunchandler to the informer that closes thefinishedchannel.Let me look at the existing kubernetes tests to understand the testing approach.
Assistant
Now I understand the testing approach. Let me write a reproduction test to confirm the bug.
Assistant
My sed mangled the file. Let me rewrite it properly.
Assistant
The bug is confirmed - WaitStep does not return after pod deletion. But my test had a pod name mismatch (
wp-del-01vsstep-del-01). Let me checkstepToPodName.Assistant
Let me fix my test to use the correct pod name (use
stepToPodName).Assistant
The bug is confirmed.
WaitStepdoes not return after the pod is deleted. Now let me understand how the server interprets the step state, so I return an appropriate state. Let me look at the tracer and state handling.Assistant
Let me look at the server side to understand how step state determines "running" vs finished.
Assistant
Let me look at
UpdateStepStatusandCalcStepStatus.Assistant
Let me look at how the workflow
Donehandles remaining running steps (theUpdateStepToStatusSkipped"for daemons that are killed").Assistant
Let me look at how the workflow
DoneRPC handles steps still running.Assistant
Interesting! There's already a
completeChildrenIfParentCompletedfunction. Let me look at it.Assistant
Let me check the
withUnboundedServicehelper to understand the dummy contract for services stopped by DestroyWorkflow.Assistant
Now I understand the full picture. The root cause: the agent's
Runblocks onr.uploadWait.Wait()waiting for the detached service goroutine, whoseWaitStepis stuck because the kubernetes informer never detects the pod deletion done byDestroyWorkflow. This delays theDoneRPC by up to the workflow timeout (1 hour), leaving service steps shown as "running".The fix is in the kubernetes backend's
WaitStep: it must detect when the pod is deleted (byDestroyWorkflow) and return, matching the dummy backend contract (returnExited: true, ExitCode: 0so the service is marked as success).Let me look at the imports and full WaitStep.
Assistant
Now let me implement the fix. I'll modify
WaitStepto handle pod deletion.Assistant
Now let me run my reproduction test to confirm the fix works.
Assistant
The fix works for the basic case. But I realize there's a potential race condition with the flag-based approach: if a pod reaches a terminal phase AND is then deleted, the deletion handler could set
podDeleted=truebefore the goroutine reads it, causing a failed pod to be reported as success. Let me use a more robust Get-based approach instead. Let me first check howerrors.IsNotFoundis used in this file.Assistant
So in pod.go, the import is
"k8s.io/apimachinery/pkg/api/errors"aliased aserrors. In kubernetes.go, the import is"errors"(standard). So in kubernetes.go,errors.IsNotFounddoesn't exist. I need to import the k8s api errors package. But there's a name conflict with the standarderrorspackage. Let me check how it's handled in pod.go.Assistant
In pod.go, the standard
errorsis NOT imported, only the k8sapi/errors. In kubernetes.go, the standarderrorsIS imported. Let me check usages oferrorsin kubernetes.go.Assistant
Let me reconsider the approach. The flag-based approach works for the realistic cases. But let me verify the deletion grace period behavior by checking
newDefaultDeleteOptions.Assistant
Let me view the current state of my edited WaitStep.
Assistant
There's a stray comment
// the completion results.before the function. Let me check the lines before 282.Assistant
Good, the
// the completion results.is part of the existing doc comment. Now let me refinepodUpdatedso terminal phases take precedence over deletion (to avoid the unlikely race where a crashed pod is also deleted). Let me update the deletionTimestamp branch to only set the flag when the pod is not in a terminal phase.Assistant
Now let me rebuild and run the tests.
Assistant
Now let me run the existing kubernetes backend tests to make sure nothing broke.
Assistant
All existing tests pass. Let me write a more thorough test to verify edge cases (terminal phase still returns real exit code; terminal-then-deleted returns real exit code).
Assistant
All edge cases pass. Now let me run the pipeline runtime tests to make sure the contract still holds.
Assistant
Now let me verify the local backend doesn't have a similar issue (for completeness, though the issue is about kubernetes).
Assistant
The local backend's
WaitStepusesProcess.Wait()which unblocks whenDestroyWorkflowkills the process, so it's fine. The kubernetes backend was the buggy one. My fix addresses it.Now let me remove the temporary repro test file (per instructions, I must not add tests).
Assistant
Now let me review my final change.
Assistant
The diff looks good. Let me run the full test suite for the affected packages and
go vet.Assistant
Let me ensure the whole project still builds.
Assistant
That error is a pre-existing issue with the web frontend embed (dist/* not built), unrelated to my change. Let me confirm by building only the relevant non-web packages.
Assistant
Let me confirm this build failure is pre-existing (unrelated to my change) by stashing my change and testing.
Assistant
Confirmed: the
web/web.gobuild failure is pre-existing and unrelated to my change.Let me check the default
StopTimeoutvalue, sincewaitForLogs()runs beforeWaitStepand the log stream (Follow) ends only when the pod is fully removed (after the grace period).Assistant
The default
StopTimeoutis 20 seconds (grace period). My fix removes the ~1 hourWaitStepblock (the dominant delay reported in the issue). The remaining ~20s is the configured graceful stop timeout (forwaitForLogsto drain the Follow log stream during pod termination), which is expected and configurable behavior — not the bug reported.Let me run broader tests to ensure nothing is broken.
Assistant
All pipeline tests pass. Let me also run the agent tests.
Assistant
All tests pass. Let me do a final review of the complete modified
WaitStepfunction.Assistant
The fix looks complete. Let me run
gofmtto ensure formatting is correct.Assistant
gofmt is clean and go vet passes. Let me do a final review of the complete diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.