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 locate relevant files related to the issue.
- Assistant
Let me understand the issue better. The bug is about services (like postgres) not being properly cleaned up after a pipeline completes. The API/UI still shows them as "running" even though the pods are destroyed. This is specifically with the Kubernetes backend.
Let me explore the relevant code areas:
- How pipeline completion is handled
- How service states are updated
- The workflow/step execution logic
- Assistant
Let me explore the pipeline and server directories to understand the architecture.
- Assistant
Let me make one command at a time.
- Assistant
Let me look at the agent and pipeline directories to understand how workflows and steps are executed.
- Assistant
Let me look at the key files related to pipeline workflow execution and state management.
- Assistant
Now let me look at the pipeline runtime to understand how workflows execute and manage services.
- Assistant
Let me look at the workflow.go file to understand how services are handled when a workflow completes.
- Assistant
Let me understand the issue better. The problem is that when a pipeline with services completes (all steps finish), the services continue to show as "running" in the UI/API even though their pods are destroyed. This is a state reporting issue.
Let me look at how step states are reported back, particularly for services. Let me check the
executeStepfunction and how the tracer works. - Assistant
Now I need to understand the issue better. Let me look at how the pipeline workflow handles services (detached steps) and the Kubernetes backend implementation.
- Assistant
Let me look at the kubernetes backend implementation, particularly the WaitStep and DestroyWorkflow methods.
- Assistant
Now I understand the issue! Let me look more closely at the
WaitStepmethod in the Kubernetes backend. The problem is clear - it only handlesUpdateFuncevents, notDeleteFuncevents. When pods are deleted byDestroyWorkflow, the informer never receives a state change that triggers thefinishedchannel. - Assistant
Now let me look at the tracer to understand how the step state is reported back.
- Assistant
Now I understand the flow. Let me look at how the Docker backend handles this case to compare.
- Assistant
Let me also look at the docker backend's WaitStep:
- Assistant
Now I need to understand the full lifecycle. Let me look at
stopPodand also how the workflow decides which steps are detached (services). - Assistant
Let me look at the stopPod function to understand how pods are deleted.
- Assistant
Now I understand the problem. Let me look at what happens in the
WaitStepfunction for the kubernetes backend when a pod is deleted. ThepodUpdatedfunction only handlesUpdateFuncevents and checks for terminal phases (Succeeded, Failed, Unknown), but whenDestroyWorkflowdeletes the pod, the pod is simply deleted - it may not go through those terminal phases. The informer only hasUpdateFunc, noDeleteFunc.Let me verify this understanding by looking at the backend type definition:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the flow:
- When a pipeline runs, stages execute sequentially. Each stage runs steps in parallel.
- Service steps (like postgres) are
Detachedsteps - they start and return immediately viarunDetachedStep() - The actual waiting for completion happens in a background goroutine via
completeStep() - After all stages complete,
destroyWorkflowFunc()callsDestroyWorkflow()which deletes all pods - But the detached step's
completeStep()goroutine is still waiting inWaitStep() - In the kubernetes backend,
WaitStep()only has anUpdateFunchandler, noDeleteFunc - When the pod is deleted, it doesn't transition to Succeeded/Failed/Unknown - it's just removed
- So
WaitStep()never returns until the context times out
The fix is to add a
DeleteFunchandler in the kubernetesWaitStep()so it detects when the pod is deleted.Let me also look at the tests to understand expected behavior:
- Assistant
Let me also check the
TailStepto see if it has the same pattern: - Assistant
Now I have a clear understanding of the bug. Let me trace through the flow:
- Services are detached steps (
step.Detached == true), which means they're started inrunDetachedStep()and run in a background goroutine. - Regular steps (like
wait-for-postgres) are blocking steps that run inrunBlockingStep(). - After all stages complete in
workflow.go,destroyWorkflowFunc()callsDestroyWorkflow()which deletes ALL pods (including service pods). - Then
r.uploadWait.Wait()blocks waiting for the detached step goroutine to finish. - The detached step goroutine calls
completeStep()which callsWaitStep(r.ctx, step, r.taskUUID). - The bug: In the Kubernetes backend's
WaitStep, onlyUpdateFuncis registered on the informer. WhenDestroyWorkflowdeletes the service pod, the informer fires aDeleteFuncevent, but there's no handler for it. Thefinishedchannel is never closed.
- Services are detached steps (
- Assistant
Let me check the existing tests for WaitStep in kubernetes backend.
- Assistant
Now I fully understand the issue. Let me trace through the exact flow:
- Services are detached steps (
step.Detached == true), started viarunDetachedStep()instep.go runDetachedStepstarts the step and launchescompleteStepin a background goroutinecompleteStepcallsWaitStep(r.ctx, step, taskUUID)which blocks waiting for the pod- After all stages complete,
destroyWorkflowFunc()callsDestroyWorkflow()which deletes all pods (including service pods) - THE BUG: In
WaitStep, the informer only hasUpdateFunc, noDeleteFunc. When the pod is deleted, thefinishedchannel is never closed because the pod doesn't transition through Succeeded/Failed/Unknown phases - it's simply removed. WaitStepblocks untilctx.Done()which only fires when the workflow context times out.- After
destroyWorkflowFunc(),r.uploadWait.Wait()blocks waiting for all goroutines, but the service's goroutine is stuck in .
- Services are detached steps (
- Assistant
Let me look at the runtime test file to understand the test expectations.
- Assistant
Now let me also check the
runtime_test.gomore closely to understand how services are expected to behave: - Assistant
Now I have a clear understanding of the full issue flow. Let me also check the Docker backend's WaitStep to understand the contrast:
- Assistant
Now I have a clear picture. Let me trace the exact issue:
- In
workflow.goRun(), after all stages complete,destroyWorkflowFunc()is called which callsDestroyWorkflow(), deleting all pods including service pods. - Then
r.uploadWait.Wait()is called to wait for all detached step goroutines to finish. - The service step's goroutine calls
completeStep()which callsWaitStep(r.ctx, step, taskUUID). - THE BUG: In the kubernetes
WaitStep, the informer only hasUpdateFunc. When the pod is deleted (byDestroyWorkflow), theUpdateFuncis NOT triggered. The informer should also have aDeleteFunchandler to close thefinishedchannel when the pod is deleted. - So
WaitStephangs until the workflow context times out.
The fix is to add a
DeleteFunchandler to the informer inWaitStep. But I also need to handle the case where afterWaitStepreturns (due to pod deletion), the GET call for pod status might fail because the pod is gone.Let me also check the
runtime_test.gofor the service step test more carefully: - In
- Assistant
Now I understand the issue perfectly. Let me implement the fix. The problem is in the kubernetes backend's
WaitStepmethod - it only registers anUpdateFunchandler on the pod informer, but whenDestroyWorkflowdeletes the pod, aDeleteFuncevent is fired, not anUpdateFunc. The service pod never transitions through Succeeded/Failed/Unknown (it's just deleted), so thefinishedchannel is never closed, andWaitStepblocks until the context times out.Let me add a
DeleteFunchandler: - Assistant
Now I have a clear understanding. The fix needs two things:
- Add a
DeleteFunchandler to the informer inWaitStepso that when a pod is deleted (not just updated), thefinishedchannel is closed. - Handle the case where after
WaitStepreturns due to pod deletion, the GET call to fetch pod status might fail because the pod is already gone.
Let me implement the fix:
- Add a
- Assistant
Now I fully understand the issue. Let me trace the exact problem:
- Services are
Detachedsteps.runDetachedSteplaunches a goroutine that callscompleteStep→WaitStep(r.ctx, step, r.taskUUID). - After all stages complete,
destroyWorkflowFunc()callsDestroyWorkflow()which deletes all pods (including service pods). - Then
r.uploadWait.Wait()waits for the detached step goroutine to finish. - THE BUG: In
WaitStep, the informer only hasUpdateFunc, noDeleteFunc. When the pod is deleted byDestroyWorkflow, a delete event fires (not update), sofinishedchannel is never closed. TheWaitStepblocks until the workflow context times out. - Since
r.uploadWait.Wait()blocks, the workflowRun()never returns, andtraceStep(which reports final state to server) never gets called for the service.
Let me implement the fix:
- Services are
- Assistant
I see there's a duplicate line. Let me fix that:
- Assistant
I see the duplicate. Let me remove lines 339-340:
- Assistant
The empty heredoc is causing issues. Let me use sed to remove those two lines.
- Assistant
Now I need to handle the case where the pod is already deleted when we try to get its status. Let me update the code after the
selectblock: - Assistant
Now I need to handle the case where the pod was deleted (not found) when we try to get its status after
WaitStepreturns. When the pod is deleted byDestroyWorkflow, the GET call might fail. Let me update the code: - Assistant
Now I need to make sure the
errorspackage is imported. Let me check the imports: - Assistant
Good,
errorsis imported. But I need to check if I'm using the correctIsNotFoundfunction. Let me check howstopPodhandles not found errors: - Assistant
errors.IsNotFoundis fromk8s.io/apimachinery/pkg/api/errors, not from the standarderrorspackage. Let me check howstopPodhandles it. - Assistant
I see -
errors.IsNotFoundis fromk8s.io/apimachinery/pkg/api/errors, not the standarderrorspackage. I need to add the correct import. Let me fix this: - Assistant
I need to import
kube_api_errors "k8s.io/apimachinery/pkg/api/errors"and usekube_api_errors.IsNotFound(err)instead oferrors.IsNotFound(err). - Assistant
I see duplicates got created. Let me fix the imports properly:
- Assistant
Let me fix the imports properly:
- Assistant
Now I need to update the
errors.IsNotFoundcall to usekube_api_errors.IsNotFound: - Assistant
Let me navigate to line 357 and then do the replace.
- Assistant
Now I can replace
errors.IsNotFoundwithkube_api_errors.IsNotFoundat line 357. - Assistant
Good. Now let me verify the complete
WaitStepfunction looks correct: - Assistant
The
WaitStepfunction looks correct now. Let me verify the file compiles correctly: - Assistant
Let me check the go environment
- Assistant
Need to set HOME too for the build cache.
- Assistant
The build passes. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Existing tests pass. Let me run all the kubernetes backend tests:
- Assistant
All kubernetes backend tests pass. Now let me also run the pipeline runtime tests:
- Assistant
Hmm, it says no test files, but there are test files. Let me check:
- Assistant
Hmm, it says no test files. Let me check what Go version is available and check if the test files have a build tag or something.
- Assistant
Let me check what Go version is being used:
- Assistant
The runtime test files might be using build tags. Let me check.
- Assistant
Let me check if there's something with go1.26 that has changed test file naming conventions... Or maybe the test file has a build tag.
- Assistant
The test files are there but Go says no test files. Let me check the go.mod:
- Assistant
Let me check if there's a test file naming issue or some build tags.
- Assistant
Let me check if the test files have some build tags or are using a different testing library.
- Assistant
The test files have
//go:build testbuild tag. So I need to pass-tags testto run them. - Assistant
All tests pass. Let me now verify my changes with a diff:
- Assistant
The diff looks clean and correct. Let me verify that
cache.DeletedFinalStateUnknownis correctly referenced - thecachepackage is already imported: - Assistant
Good, the
cachepackage is already imported. Let me run the full test suite once more to make sure everything is clean: - Assistant
All tests pass. Let me also run the agent tests:
- Assistant
All tests pass. Let me also run any broader pipeline tests:
- Assistant
All tests pass. Now let me review the full final state of the changed file one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.