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 analyze this issue. The bug is about Woodpecker CI services not being cleaned up properly after pipeline completion when using the Kubernetes backend. The services show as "running" even after the pods have been destroyed.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the pipeline and server directories to understand how services are managed and cleaned up.
Assistant
Let me look at the kubernetes backend and understand how services are managed:
Assistant
I made an error in my tool call. Let me fix the syntax - I should use
--fileinstead offile.Assistant
The open command doesn't take named arguments. Let me just pass the file path as a positional argument.
Assistant
Now I can see the Kubernetes backend. The key observation is in
DestroyWorkflow- it only iterates overstage.Steps, notstage.Services. Let me check the types to understand the config structure:Assistant
I notice that
Stageonly hasStepsbut noServices. Let me check how services are represented in the system:Assistant
So services are steps with
Type: StepTypeService. Now let me look at the agent runner to understand how pipeline steps are executed and when services are destroyed:Assistant
Now let me look at the pipeline runtime to understand how steps and services are executed:
Assistant
Now I can see the issue! In
workflow.go, therunStagefunction executes all steps in parallel, including services (which are detached). After all stages complete,destroyWorkflowFunc()is called which invokesDestroyWorkflow.Let me look at the
DestroyWorkflowfunction in the Kubernetes backend more closely. I noticed it only iterates overstage.Steps, but I need to verify that services are indeed steps withType: StepTypeService. Let me check if there's a separate services list or if services are mixed in the steps:Assistant
Now I understand the structure. Looking at the compiler:
- Clone steps are added first in their own stage
- Services are added in their own stage with
StepTypeService - Pipeline steps are added in subsequent stages
Now let me look at how the service steps are handled. Services have
Detached: trueso they run in the background. Let me check thecreateProcessfunction to see how services are configured:Assistant
Now I understand the full picture:
- Services are compiled as steps with
Type: StepTypeServiceandDetached: true - Detached steps are run in the background via
runDetachedStep - When a detached step completes, it calls
completeStepwhich callsDestroyStepto destroy the container - However, the trace that marks the step as "stopped" happens asynchronously in the goroutine
Let me check the server side to understand how step states are updated. The issue reports that the API shows services as still "running" even after the pods are destroyed. This could be a timing issue or a tracing issue:
- Services are compiled as steps with
Assistant
Now let me look at the runtime tracer to see how step states are communicated to the server:
Assistant
Let me look at the tracing module:
Assistant
Let me now look at how the tracer is created on the agent side and how it reports step states to the server:
Assistant
Now let me understand the flow better. Let me trace the issue from start to finish:
- When services run, they're marked as
Detached: trueand run inrunDetachedStep runDetachedStepstarts the step, then runscompleteStepin a goroutine- After all pipeline steps complete,
destroyWorkflowFunc()is called which callsDestroyWorkflow DestroyWorkflowin the kubernetes backend only destroys pods forstage.Steps
But wait, services ARE in
stage.Steps. Let me re-check theDestroyWorkflowfunction:- When services run, they're marked as
Assistant
The previous command didn't have a tool call. Let me continue investigating the issue by looking at the workflow execution flow more carefully.
Assistant
Now I understand the issue. Looking at the workflow execution flow:
- Services are started as detached steps (
runDetachedStep) - The goroutine for each service calls
WaitStepto wait for the pod to complete - When the main pipeline steps finish,
DestroyWorkflowis called which destroys all pods including service pods - BUT
WaitStepin the kubernetes backend only listens forUpdateFuncevents, notDeleteFunc - When the pod is deleted, the informer fires a
DeleteFuncevent which is not handled - So
WaitStepblocks forever (until context timeout), and the step state is never updated to the server
The fix is to add
DeleteFuncto the informer event handler inWaitStep. Let me verify this is the correct approach:- Services are started as detached steps (
Assistant
I can see the issue now. The
WaitStepfunction only handlesUpdateFuncevents in the informer. WhenDestroyWorkflowdeletes the service pods, the informer sees aDeleteFuncevent which is NOT handled. SoWaitStepblocks forever, and the step status is never updated to the server.Let me also check the
TailStepfunction for a similar issue:Assistant
I've found the bug. In
WaitStepfor the Kubernetes backend, only theUpdateFuncinformer event is handled. WhenDestroyWorkflowdeletes the service pods, the informer fires aDeleteFuncevent which is not handled, causingWaitStepto block indefinitely. This means the step state is never updated to the server.Let me also check if there are any tests related to this:
Assistant
I've identified the bug. In
WaitStepfor the Kubernetes backend, only theUpdateFuncinformer event is handled. WhenDestroyWorkflowdeletes service pods, the informer fires aDeleteFuncevent which is not handled, causingWaitStepto block indefinitely. This means the step state is never updated to the server, and services remain shown as "running".Let me implement the fix:
Assistant
I need to see more of the WaitStep function to understand the full context, then I'll add the DeleteFunc handler to the informer event handlers.
Assistant
Now I fully understand the issue. The
WaitStepfunction only handlesUpdateFuncevents in the informer. WhenDestroyWorkflowdeletes service pods, the informer fires aDeleteFuncevent which is not handled, soWaitStepblocks forever and the step state is never updated to the server.Let me implement the fix:
Assistant
Now I understand the full issue. The fix is to add a
DeleteFunchandler to the informer inWaitStep. When the pod is deleted (which happens whenDestroyWorkflowis called), thefinishedchannel should be closed soWaitStepcan return.Assistant
Let me try the edit command with the correct format:
Assistant
I notice that when
WaitStepreturns after the pod is deleted, the code will try toGetthe pod which will fail since the pod no longer exists. I need to handle this case. Let me update the code to track whether the pod was deleted and handle it appropriately:Assistant
Now I see the issue more clearly. When
WaitStepfinishes (either by pod update, deletion, or context cancellation), it tries to get the pod to retrieve the exit code. But if the pod was deleted byDestroyWorkflow, the Get call will fail.I need to:
- Track whether the pod was deleted
- If the pod was deleted, return a state indicating the step was killed (canceled)
Let me rewrite the
WaitStepfunction to handle this case:Assistant
Let me verify the complete changes look correct:
Assistant
The changes look good. Now let me verify the code compiles and run the existing tests to make sure I haven't broken anything:
Assistant
I need to set up the Go environment properly before building:
Assistant
The user seems to have sent an empty message. Let me continue with my work on verifying the code changes compile correctly. The previous error was about web assets, not about my changes. Let me try to build just the kubernetes backend package.
Assistant
Let me set the required environment variables for Go to work.
Assistant
The code compiles successfully. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
The tests pass. Let me also run the runtime tests to make sure the changes don't affect other parts:
Assistant
Wait, there should be test files. Let me check:
Assistant
There are test files but they might not be included. Let me run them with the correct flags:
Assistant
Let me investigate why the tests aren't being found:
Assistant
I need to check the test file to see why tests aren't running.
Assistant
The tests have a build tag
//go:build test. Let me run them with the correct tag:Assistant
The tests are running. Let me wait for the full output to see if they pass.
Assistant
The tests pass. Now let me verify my changes are complete by reviewing the diff and running a broader test:
Assistant
The fix looks good. Let me run more tests to make sure nothing else is broken:
Assistant
Now let me also run the server pipeline tests since they're related to step status updates:
Assistant
Let me verify that the
types.Statestruct has anErrorfield:Assistant
Good, the
Statestruct has anErrorfield. Now let me verify that my changes work correctly by examining the flow:- When services are started, they run as detached steps
- When
DestroyWorkflowis called, it deletes the service pods - With my fix, the
WaitStepfunction now listens forDeleteFuncevents - When a pod is deleted,
WaitStepreturns immediately with a killed state - The step status is then updated to the server, showing the service as stopped
Let me also check if there are any other backends that might have the same issue:
Assistant
The user hasn't asked me to do anything specific, so I should continue with my analysis. I was checking if other backends have the same issue. Let me look at the Docker backend to see how it handles WaitStep.
Assistant
The Docker backend uses
ContainerWait()which properly handles container removal - it will return when the container stops or is removed. This is different from the Kubernetes backend which only watched forUpdateFuncevents.Let me now verify my complete fix by running all the relevant tests:
Assistant
The only failure is in the dummy backend which has build constraints excluding all Go files - this is a pre-existing issue, not caused by my change. The kubernetes backend tests pass.
Let me run the tests with the test tag to get more coverage:
Assistant
All tests pass. Let me verify the complete fix one more time and also check if there are any edge cases I might have missed:
Assistant
Let me verify the final state of my changes by viewing the complete diff and make sure everything is correct before submitting.
Assistant
My fix is complete and verified. Let me submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.