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 start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the server directory and the agent directory to understand how steps/services are handled.
Assistant
Let me look at the key files - the agent's step state reporting and the server's rpc handling.
Assistant
Assistant
So
r.ctxis the workflow context. Let me look at how the runtime'sctxis set. It defaults tocontext.Background(). Let me see the option.go to see if ctx is set via options.Assistant
Now let me look at how the agent runs workflows and how the context is set up. Let me look at the agent's pipeline running code.
Assistant
Let me look at the kubernetes backend's WaitStep and DestroyWorkflow.
Assistant
Now I understand the kubernetes backend. When
DestroyWorkflowis called, it deletes all pods viastopPod. TheWaitStepfor a service (detached step) waits on the informer for the pod to reach a terminal state (Succeeded/Failed/Unknown). When the pod is deleted, the informer fires aDeleteFunc, notUpdateFunc. ThepodUpdatedonly handlesUpdateFunc. Let me verify this and check the git history for the regression.Assistant
Now I understand the regression. The fix in #6507 made
DestroyWorkflowhappen beforeuploadWait.Wait(), which works for the dummy backend because the dummy backend'sDestroyWorkflowcloses a channel that unblocksWaitStep. But the real kubernetes backend'sDestroyWorkflowdeletes the pods, and theWaitStepfor the service pod is blocked waiting on the informer for a terminal pod phase. When the pod is deleted, the informer fires aDeleteFunc(notUpdateFunc), so thefinishedchannel is never closed.Let me look at the backend interface contract and how other backends handle this.
Assistant
The contract says
DestroyWorkflowshould "Destroy steps still running in the background (detached steps and services)". The kubernetes backend does delete pods, butWaitStepdoesn't detect deletion. Let me check how the docker backend handles this.Assistant
For docker,
WaitStepusesContainerWaitwhich unblocks when the container is stopped/removed. But for kubernetes,WaitStepuses an informer that only watchesUpdateFunc— pod deletion doesn't trigger it. Let me look at the dummy backend to understand the expected state behavior.Assistant
Let me look at the test for unbounded service to understand expected state behavior.
Assistant
Let me look at how
stopPoddeletes pods in the kubernetes backend.Assistant
Let me look at the imports in kubernetes.go to see what's available.
Assistant
Now let me check if
apierrorsis available. It's not imported. I'll need to importk8s.io/apimachinery/pkg/api/errorsto checkIsNotFound. Actually,stopPoduseserrors.IsNotFound(err)— wait, that's from the standarderrorspackage? No, it must be a different import. Let me check pod.go imports.Assistant
Let me look at how the server/tracer maps the step state to step status, to decide what state to return.
Assistant
Interesting! There's a comment "for daemons that are killed" in
UpdateStepToStatusSkipped. Let me find where it's called.Assistant
There's a
completeChildrenIfParentCompletedfunction that marks running children as killed when a workflow completes. Let me find where it's called.Assistant
There's already a server-side mechanism
completeChildrenIfParentCompletedthat marks running children as killed/success when the workflow'sDoneRPC is called. So the real problem is the agent's workflowRunnever completes becauseuploadWait.Wait()blocks on the kubernetesWaitStepwhich doesn't detect pod deletion. Let me check the delete options used.Assistant
Now I understand the full picture. The kubernetes
WaitSteponly watchesUpdateFunc, so whenDestroyWorkflowdeletes the service pod (graceful deletion withdeletionTimestamp, phase stays Running),finishednever closes andWaitStepblocks until the workflow timeout. This blocksuploadWait.Wait(), so the workflow never completes and theDoneRPC (which hascompleteChildrenIfParentCompleted) is never called.Let me verify the stdlib
errorsusage in kubernetes.go before modifying.Assistant
kubernetes.gouses stdliberrors.Join. I'll alias the k8s apierrors package asapierrors. Let me look at the existing kubernetes tests forWaitStep.Assistant
Let me look at the test helpers to understand the test setup.
Assistant
Let me write a reproduction test to confirm the bug before fixing.
Assistant
The reproduction confirms the bug:
WaitStepdoes not return after pod deletion. Now let me apply the fix to the kubernetesWaitStep.Assistant
Now let me modify the
WaitStepfunction. First, let me add theapierrorsimport.Assistant
The file isn't currently open in the editor (I used --file). The error says "Text not found in displayed lines" because the editor view is at line 282. Let me use
gototo navigate first, or useeditcommand with the line range. Actually, let me just use theeditcommand with line numbers since I know the import block. Let me check the exact import lines.Assistant
Now let me modify the
WaitStepfunction body. Let me navigate to it.Assistant
Now let me run the reproduction test again to confirm the fix works.
Assistant
The fix works for the pod deletion case. Let me also test the graceful deletion (
deletionTimestamp) path, which is what happens in real k8s withGracePeriodSecondsset.Assistant
All tests pass — graceful delete (deletionTimestamp), immediate delete, normal success, and context cancel all work. Let me run the full kubernetes backend test suite to ensure nothing is broken.
Assistant
Now let me run the runtime tests to make sure the existing service/detached workflow tests still pass.
Assistant
Hmm, "no test files" - that's odd. Let me check. Maybe the test files are in a different build tag or the package name is different. Let me check.
Assistant
The runtime tests use the
testbuild tag. Let me run them with the tag.Assistant
All runtime tests pass. Now let me clean up my reproduction test files before submitting (the instructions say not to add new tests to the repository).
Assistant
Let me consider potential races. Let me check the default
StopTimeout(grace period) for the kubernetes backend.Assistant
The default grace period is 20 seconds. There's a potential race where the pod is already gone or being deleted when
WaitStep's informer starts (sinceWaitStepis called afterwaitForLogs()returns, which happens when the container terminates during deletion). Let me add an initial existence/deletion check for robustness. Let me view the current state of the function.Assistant
Let me add an initial check after
si.Start(stop)to handle the race where the pod is already gone or being deleted whenWaitStepis called.Assistant
Now let me recreate the reproduction tests, including a test for the race scenario where the pod is already gone before
WaitStepis called.Assistant
All tests pass, including the race scenario. Let me run the full kubernetes backend test suite and verify the build compiles.
Assistant
The web error is unrelated (missing embedded dist files). Let me build the relevant packages directly.
Assistant
Now let me run the runtime tests once more and clean up the repro test file.
Assistant
Let me run the full kubernetes test suite once more to confirm everything passes after cleanup.
Assistant
Let me review the final diff.
Assistant
I want to refine the ordering of checks after the
Get. Currently, thedeletionTimestampcheck comes before reading the terminated container state, which could override a real failure exit code if a pod fails AND is being torn down at the same time (rare race). Let me restructure so the real terminated state takes precedence, and only fall back to the "killed" success state when there's no terminated state.Assistant
Now let me recreate the repro tests and run them again to verify the refactored logic.
Assistant
All tests pass, including the important
TestWaitStepFailedExitCodePreservedwhich verifies a real failure exit code isn't masked by a deletion timestamp. Let me clean up the repro test file and run the full suite once more.Assistant
Let me verify the final git status to confirm only the intended file is modified.
Assistant
Only
pipeline/backend/kubernetes/kubernetes.gois modified, and no untracked files remain. Let me do a final review of the completeWaitStepfunction.Assistant
The code looks coherent. Let me run the broader pipeline backend tests to be thorough.
Assistant
The dummy backend uses a build tag. Let me check and run with the appropriate tag.
Assistant
All backend tests pass. Let me also run the server rpc tests to be thorough (I didn't touch server code, but confirming nothing broke).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.