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 start by exploring the repository structure to understand the codebase and locate relevant files. Let's first get an overview.
- Assistant
Let me explore the repository structure to understand the codebase better.
- Assistant
Let me explore the repository structure more systematically.
- Assistant
Let me look at how the pipeline handles step status updates, particularly for services.
- Assistant
Let me look at the agent runner to understand how services are handled.
- Assistant
Let me look at the pipeline runtime to understand how workflows and services are executed.
- Assistant
Let me look at the pipeline runtime to understand how it runs workflows with services.
- Assistant
Let me look at the runtime.go and workflow.go files to understand how the pipeline runtime handles service steps.
- Assistant
Let me continue exploring the pipeline runtime and backend code to understand the issue better.
- Assistant
Now let me look at the step execution logic to understand how steps are handled, especially services.
- Assistant
Now let me understand how services are compiled into stages/steps:
- Assistant
Let me continue reading the compiler to see how services are compiled into stages.
- Assistant
Now I understand the structure. Services are compiled as their own stage. Let me check how the
Step.Detachedflag is set for service steps. - Assistant
Let me look at how service steps are compiled:
- Assistant
Now let me look at the kubernetes backend to see how
WaitStepandDestroyWorkflowinteract: - Assistant
Let me look at the kubernetes backend to understand how WaitStep and DestroyWorkflow work.
- Assistant
Now I have a good understanding of the flow. Let me trace through the exact issue:
- Services are compiled as separate stage, executed as detached steps
- When all pipeline stages complete,
destroyWorkflowFunc()is called, which callsDestroyWorkflow→stopPodfor ALL pods including services - Then
r.uploadWait.Wait()waits for detached step goroutines - The detached step goroutine is stuck in
WaitStepbecause the kubernetes backend's informer only hasUpdateFunchandler, noDeleteFunchandler - When a pod is deleted, no update event fires, so
WaitStephangs until the workflow context times out
Let me verify by looking at
WaitStepandstopPod: - Assistant
Let me look at the stopPod function.
- Assistant
Let me also check other backends:
- Assistant
Let me look at the docker and local backend WaitStep implementations to see if they have the same issue.
- Assistant
Now I understand the root cause. Let me look at the runtime tests to understand the test patterns:
- Assistant
Now let me look at the runtime tests that involve services:
- Assistant
There are already tests for unbounded services and detached steps! Let me look at
TestWorkflowUnboundedServiceDoesNotHangto understand the expected behavior: - Assistant
Let me look at how the dummy backend handles
WaitStepandDestroyStep: - Assistant
Let me check the
StopTimeoutdefault and flags: - Assistant
Let me check the flags definition to see the default value.
- Assistant
Now I understand the issue clearly. Let me trace through the problem:
- When pipeline stages complete,
destroyWorkflowFunc()is called which callsDestroyWorkflow() DestroyWorkflow()in kubernetes backend callsstopPod()which callsDelete()withDeletePropagationBackground- With background propagation, the pod object is removed from the API server immediately
- The
WaitStep()in kubernetes only registers anUpdateFuncon the informer - it does NOT handleDeleteFuncevents - So the detached step's goroutine hangs in
WaitStep()until the workflow context times out - This blocks
r.uploadWait.Wait(), preventing the workflow from completing
Let me verify by looking at the
WaitStepmore carefully: - When pipeline stages complete,
- Assistant
Now I fully understand the bug. Let me look at how the tests pass currently:
- Assistant
Now I have a complete understanding of the issue. Let me run the existing tests first to see the current state:
- Assistant
Let me set up the Go environment first.
- Assistant
Let me set up GOPATH.
- Assistant
Let me set HOME too.
- Assistant
Now I understand the full picture. Let me check git log for recent changes related to this area:
- Assistant
Let me look at the workflow.go file again to understand the exact issue.
- Assistant
Now I have a full understanding of the bug. Let me trace through the exact issue:
- Services are compiled as separate stages with
Detached: true - When a detached step executes,
runDetachedStepstarts the container and returns immediately; a background goroutine callscompleteStepwhich callsWaitStep - After all stages complete,
destroyWorkflowFunc()is called which callsDestroyWorkflow→stopPodfor ALL pods (including service pods) - Then
r.uploadWait.Wait()waits for the detached step goroutine to finish - The detached step goroutine is stuck in
WaitStepbecause:- If the pod is deleted BEFORE
WaitStepstarts watching, the informer never sees the pod - If the pod is deleted WHILE
WaitStepis watching, onlyUpdateFuncis handled, notDeleteFunc
- If the pod is deleted BEFORE
WaitStepblocks until the workflow context times out
The fix needs to happen in the kubernetes backend's
WaitStepto handle pod deletions. Let me implement the fix: - Services are compiled as separate stages with
- Assistant
Now let me see the rest of WaitStep:
- Assistant
Now I have a clear understanding of the bug. Let me also look at the
completeStepfunction in detail to understand the full flow: - Assistant
Let me check how
stepToPodNameworks and look at the overall informer logic more carefully: - Assistant
Let me check the utils.go for isImagePullBackOffState and isInvalidImageName:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the exact flow:
The Bug:
- Service steps are compiled as detached steps in their own stage
runDetachedStep()starts the container, spawns a goroutine forcompleteStep()→WaitStep(), and returns immediately- Next stages (pipeline steps) run and complete
destroyWorkflowFunc()is called which callsDestroyWorkflow()→stopPod()for ALL pods (including services)r.uploadWait.Wait()waits for the detached goroutine- The detached goroutine's
completeStep()callswaitForLogs()first, thenWaitStep()
The key race condition:
WaitStep()in the kubernetes backend uses an informer that only hasUpdateFunchandler. When the pod is deleted (byDestroyWorkflow()):- If
WaitStep()starts BEFORE the pod is deleted: the informer should catch the terminal state transition via - this usually works
- Assistant
Now I need to understand the exact issue. Let me look at the
runDetachedStepflow more carefully - the key question is whetherWaitStepgets called before or after the pod is destroyed: - Assistant
Now I need to understand the complete flow more precisely. The key insight is:
- Service steps run in a stage as
Detached-runDetachedStepstarts the pod and spawns a background goroutine - The background goroutine calls
completeStep()which first callswaitForLogs()(blocking), thenWaitStep()(blocking) - When all stages complete,
destroyWorkflowFunc()is called which destroys ALL pods (including service pods) - Then
r.uploadWait.Wait()blocks until the background goroutine finishes
The problem: When
DestroyWorkflowdeletes the service pod, the background goroutine'sWaitStep()may:- Not have started yet (still in
waitForLogs()) - Already be waiting in the informer, but the informer only handles
UpdateFunc, notDeleteFunc
In both cases, the informer may never detect the pod's termination because:
- The pod may already be deleted before the informer starts watching
- The informer misses
DeleteFuncevents
Let me now implement the fix in the kubernetes backend's
WaitStep: - Service steps run in a stage as
- Assistant
Let me look at the
completeStepandrunDetachedStepflow one more time carefully: - Assistant
Now I have a clear understanding of the issue. Let me also check the Docker backend for comparison:
- Assistant
Now I have a complete understanding of the bug. Let me trace through the exact issue:
- Services are compiled as
Detached: truein their own stage runDetachedStepstarts the container, spawns a background goroutine that callscompleteStep, and returns immediately- The stage completes quickly since all steps are detached
- The next stages (build steps) run and complete
destroyWorkflowFunc()is called →DestroyWorkflow→ deletes ALL pods (including service pods)r.uploadWait.Wait()waits for the detached step goroutines
The problem: The detached step goroutine is stuck in
WaitStep. When the pod is deleted byDestroyWorkflow, the kubernetes informer only handlesUpdateFunc(notDeleteFunc), so thefinishedchannel is never closed.WaitStepblocks until the workflow context times out.The Docker backend doesn't have this issue because
ContainerWaitproperly handles container removal.Let me implement the fix:
- Services are compiled as
- Assistant
Let me try to open the file properly.
- Assistant
Now I'll implement the fix. The key changes to
WaitStepare:- Add
DeleteFunchandler - when the pod is deleted, close thefinishedchannel - Handle "not found" after deletion - when the pod is already gone, return a reasonable state instead of an error
- Add initial pod check - after starting the informer, verify the pod still exists (handles race where pod was deleted before informer started)
- Add
- Assistant
Let me use the correct edit syntax.
- Assistant
Let me open the file first.
- Assistant
Let me use the edit command with the correct file parameter syntax.
- Assistant
Let me check if
kube_errorsis imported and use a different approach to make the edit: - Assistant
Let me use sed to make the changes since the edit tool is having issues:
- Assistant
Let me verify the changes were applied correctly:
- Assistant
Let me check the rest of the function:
- Assistant
Good, the changes look correct. Let me verify the import was added:
- Assistant
The import was added but in the wrong position. Let me fix the import ordering:
- Assistant
Let me verify the imports:
- Assistant
Good, the imports look correct. Now let me try to compile the code to make sure it compiles correctly:
- Assistant
The code compiles. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
All runtime tests pass. Let me also run the kubernetes backend tests:
- Assistant
All tests pass. Let me also check that there are no unused imports:
- Assistant
Let me also verify the full diff of my changes:
- Assistant
The diff looks good. Let me now run all the relevant tests to make sure nothing is broken:
- Assistant
Let me also run the broader pipeline tests:
- Assistant
All tests pass. Let me also make sure the broader build still works:
- Assistant
The build error is about missing web dist files (unrelated to my changes). Let me verify it exists before my changes:
- Assistant
Good - the build error is pre-existing and not related to my changes. Let me also build the specific packages that matter:
- Assistant
Everything builds and all tests pass. Let me do a final review of the complete changes:
- Assistant
The changes look correct and complete. Let me run one final comprehensive test:
- Assistant
All tests pass. Let me verify all the runtime tests pass as well:
- Assistant
Hmm, let me check the output more carefully.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.