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. Let's first look at the root.
- Assistant
Let me understand the issue better. The problem is that when a pipeline with services completes, the service steps remain in "running" state in the API/UI, even though the pods are destroyed. This seems to be a bug in how service step completion is tracked.
Let me explore the relevant parts of the codebase:
- How pipelines and their steps are managed
- How service steps are handled
- How step states are updated when a pipeline completes
- Assistant
Let me search more specifically for pipeline-related code and look at the agent/server code that handles step completion.
- Assistant
Let me look at the server pipeline code, specifically around step status updates and service cleanup.
- Assistant
Let me look at the agent runner to understand how it handles services and pipeline completion.
- Assistant
Let me look at the agent runner and understand how services are handled.
- Assistant
Let me look at the pipeline runtime to understand how workflows are run and how steps are managed.
- Assistant
Let me look at the pipeline runtime to understand how steps and services are handled during execution.
- Assistant
Let me look at the key files to understand the issue. The issue is that services are not being cleaned up properly - they remain in "running" state after pipeline completes. Let me look at:
- The model definitions for steps2. The pipeline runtime execution flow
- How steps are updated when workflow completes
- Assistant
Let me look at the pipeline runtime to understand how steps are executed and how services are handled.
- Assistant
Let me find the pipeline runtime directory.
- Assistant
Let me look at the pipeline directory structure to find the runtime.
- Assistant
Let me look at the runtime and step execution code.
- Assistant
Let me look at the workflow runtime to understand how steps are managed.
- Assistant
Now I need to understand how steps are executed and how their state is reported back. Let me look at the step execution code and the tracer code.
- Assistant
Now I need to understand the issue. The problem is that services are running as detached steps, and when the pipeline completes, they remain in "running" state in the API. Let me look at the tracer and how it reports state to the server.
- Assistant
Now let me look at the server-side handling of step updates - specifically how the Update method processes step state updates and what happens when the workflow finishes.
- Assistant
Now let me look at how the Done method handles workflow completion and what happens with running service steps. Let me examine the completeChildrenIfParentCompleted method more closely:
- Assistant
Let me see the
Donefunction fully and thecompleteChildrenIfParentCompletedmethod. - Assistant
Let me look at the
completeChildrenIfParentCompletedmethod: - Assistant
Let me look at the
UpdateStepToStatusSkippedfunction andmodel.StatusKilled: - Assistant
Now let me look at the step model to understand the
Running()method. - Assistant
Now let me look at the
Running()method on the step model to understand how it works: - Assistant
Let me look at how
StepListFromWorkflowFindandWorkflowGetTreework to understand whenworkflow.Childrengets populated: - Assistant
Let me understand how the pipeline is compiled into stages and understand the flow:
- Assistant
Now I need to check how services are compiled. Let me look at
createProcessto see if services are set as detached: - Assistant
Let me look at the kubernetes backend to understand how WaitStep and DestroyWorkflow work:
- Assistant
No kubernetes backend files? Let me check if they are in a separate repo.
- Assistant
Let me look at the backend directory structure:
- Assistant
Let me look at the backend interface and kubernetes implementation:
- Assistant
Let me look at the Kubernetes backend implementation:
- Assistant
Let me look at the kubernetes backend implementation to understand how steps are tracked.
- Assistant
Let me look at the
checkWorkflowAllowsStepUpdateandcheckWorkflowStatefunctions that are called during RPC updates: - Assistant
Let me check the
checkWorkflowAllowsStepUpdatefunction. - Assistant
Now I understand the flow better. Let me check the git history for recent changes related to this issue:
- Assistant
Let me trace through the flow more carefully. The key issue seems to be about how services are cleaned up. Let me look at how the pipeline stages work:
- Assistant
Let me look at
stopPodto understand how pods are deleted: - Assistant
Let me look at the stopPod function:
- Assistant
Let me check the default stop timeout:
- Assistant
Let me check the default value for the stop timeout:
- Assistant
Let me now understand the real flow of what happens when a service step finishes. Let me look at the Docker backend's WaitStep for comparison:
- Assistant
Now I understand the flow better. Let me trace the exact issue. Let me look at what happens with services during pipeline execution:
- Assistant
Let me look at the docker backend's WaitStep for comparison:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the flow:
- Service steps are compiled as
Detached: truein their own stage - When
runStageruns the service stage,runDetachedStepstarts the service and spawns a goroutine forcompleteStep, then returns immediately - Next stages (pipeline steps) run and complete
destroyWorkflowFunc()is called, which callsDestroyWorkflow→stopPodfor all steps including servicesr.uploadWait.Wait()waits for detached goroutines to finish
The problem is in
WaitStep- it only registersUpdateFuncon the pod informer, notDeleteFunc. WhendestroyWorkflowFunc()deletes the service pods, the pod deletion triggers aDeleteFuncevent (notUpdateFunc), soWaitStepnever detects the pod is gone. It hangs until the workflow context times out.Let me verify this by looking at the
WaitStepimplementation: - Service steps are compiled as
- Assistant
Now I have a clear picture of the bug. Let me look at the full
WaitStepmethod: - Assistant
Now let me understand the flow more carefully. The issue is about services not being cleaned up. Let me look at the
runStagefunction to understand how stages are executed: - Assistant
Let me look at the
GetShutdownCtxfunction: - Assistant
Now I have a clear understanding of the bug. Let me trace the exact issue:
- Services run as detached steps (in their own stage), spawning a goroutine via
runDetachedStep - The goroutine runs
completeStep, which first callswaitForLogs()thenWaitStep(r.ctx, ...) - After all stages complete,
destroyWorkflowFunc()is called which deletes all pods (including service pods) r.uploadWait.Wait()blocks until the detached goroutine finishes
The problem: After the service pod is destroyed by
destroyWorkflowFunc(), theWaitStepinformer (which only watchesUpdateFunc, notDeleteFunc) may never fire thefinishedchannel because:- When a pod is deleted, the
DeleteFuncevent fires (notUpdateFunc) - If the pod is already garbage collected by the time the informer starts (after
waitForLogsreturns), the informer never sees the pod at all
This causes
WaitStepto hang until the workflow context times out.Let me also check
TailStepfor the log stream issue: - Services run as detached steps (in their own stage), spawning a goroutine via
- Assistant
Now I understand the full picture. Let me check how the flow works for service steps:
- Assistant
Now I understand the exact issue. Let me trace through the flow one more time:
- Service stage runs -
runDetachedStepstarts the pod and spawns a goroutine - The goroutine calls
completeStepwhich callswaitForLogs()FIRST (blocking) - Other stages run (wait-for-postgres completes)
destroyWorkflowFunc()is called - deletes ALL pods including the service pod- Service pod gets destroyed → log stream ends →
waitForLogs()returns - NOW
WaitStep(r.ctx, step, r.taskUUID)is called - But the pod is already gone! The informer starts but never sees the pod
WaitStepblocks untilctx.Done()(workflow timeout)
The fix is in
WaitStep: we need to handleDeleteFuncand also handle the case where the pod is already gone when the informer starts.Let me write the fix:
- Service stage runs -
- Assistant
Let me look at the full import block:
- Assistant
Now let me check how "not found" errors are detected in the codebase:
- Assistant
Let me check which import provides
errors.IsNotFound: - Assistant
Let me check the import block in pod.go to find the errors package.
- Assistant
Now let me also look at
isImagePullBackOffStateandisInvalidImageName: - Assistant
Now I have all the information needed. Let me write the fix. The core issue is in
WaitStep- it only watches forUpdateFuncbut notDeleteFunc. When a service pod is destroyed bydestroyWorkflowFunc(), the pod may be garbage collected and the informer never fires thefinishedchannel.The fix needs to:
- Add
DeleteFunchandling to close thefinishedchannel when the pod is deleted - Handle the case where the pod is already gone (not found) after
finishedfires
- Add
- Assistant
Let me check if the kubernetes.go file already imports the k8s errors package:
- Assistant
Now let me implement the fix. I need to:
- Add the k8s
api/errorsimport - Add
DeleteFunchandler to the informer inWaitStep - Handle the case where the pod is already gone when trying to GET it after
finishedfires
- Add the k8s
- Assistant
Now let me implement the fix:
- Assistant
I accidentally duplicated the import. Let me fix that:
- Assistant
Wait,
kube_core_v1was already imported elsewhere. Let me check: - Assistant
Good, let me verify the current import state is correct:
- Assistant
Good, imports look correct. Let me verify no duplicate
kube_core_v1exists: - Assistant
Wait, let me check git to make sure I'm not duplicating:
- Assistant
Good, only the new import line was added. Now let me modify the
WaitStepfunction: - Assistant
Now let me edit the
WaitStepfunction to addDeleteFunchandling and handle the pod-not-found case: - Assistant
I see there's a stray
}on line 331. Let me fix that and update the event handler registration and the pod-not-found handling: - Assistant
Now let me verify the complete function looks correct:
- Assistant
Good, the function looks correct. But I realize there's still an edge case: if the pod is already garbage collected BEFORE the informer starts, the
DeleteFuncwon't fire either (since the pod was never in the informer's cache). Let me add a pre-check: - Assistant
Let me add proper spacing between the pre-check and
podUpdated: - Assistant
Good. Now let me verify the complete diff:
- Assistant
The diff looks clean. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Let me check the Go environment setup:
- Assistant
GOMODCACHE and GOPATH are not set. Let me set them and try again.
- Assistant
Let me set HOME too:
- Assistant
All tests pass. Let me also run the pipeline runtime tests:
- Assistant
Let me also run the server-side tests related to the pipeline workflow:
- Assistant
Let me also run the pipeline step status tests:
- Assistant
Let me also run the agent tests:
- Assistant
All tests pass. Let me also do a build check:
- Assistant
Let me run
go vetto check for any issues: - Assistant
Let me also check that the
cache.DeletedFinalStateUnknowntype is available: - Assistant
Let me run a broader set of tests to make sure nothing is broken:
- Assistant
All tests pass. The
dummyfailure is a build constraint issue unrelated to our change. Let me also verify theTailStepfunction doesn't have the same issue since it also only watchesUpdateFunc: - Assistant
Good, the
TailStepfunction only needs to detect when a pod starts running to begin log streaming, so it doesn't need the same fix. Let me verify the final diff one more time: - Assistant
The diff looks clean and correct. Let me run the full kubernetes test suite one more time to be sure:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.