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
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 pods are destroyed, but the Woodpecker API still shows them as "running" state.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore more about the structure of the project, especially looking at the backend and kubernetes-related code.
Assistant
Let me explore the kubernetes backend code since the issue mentions it's related to the kubernetes backend.
Assistant
The user is trying to open a file. Let me use the correct syntax for the open command without the file parameter wrapper.
Assistant
Now let me look at the agent/runner code that handles the pipeline execution to understand how steps are started, waited, and destroyed:
Assistant
Now let me look at the pipeline runtime code to understand how the pipeline executes and manages services:
Assistant
I need to continue examining the codebase to understand how services are handled. Let me look at the workflow.go file to understand how the workflow manages services and regular steps.
Assistant
Now let me look at the runtime.go to understand the overall structure and how services are handled:
Assistant
Now let me check the backend types to understand how services are defined:
Assistant
Now I can see that
StepTypeServiceis defined and theDetachedfield is important. Let me look at how services are processed and how they relate to detached steps:Assistant
Now let me search for how services are started and traced:
Assistant
Let me look at how the workflow handles the execution of services and when they get stopped. The key issue is that services are not being cleaned up properly - their state remains "running" in the API. Let me examine the workflow execution flow more carefully:
Assistant
Now let me look at the key issue - when services are destroyed, they should have their state updated. Let me check how the
runDetachedStepandtraceStepfunctions work together:Assistant
Now I see the issue more clearly. The
uploadWaitWaitGroup is used to wait for detached steps to complete. Let me look more carefully at the workflow flow:Assistant
Looking at the code, I can see the flow:
Run()executes stages, each stage runs steps in parallel viarunStage()executeStep()handles both blocking and detached (service) steps- For detached steps (services),
runDetachedStep()returns immediately after starting, but the step completion happens in a goroutine destroyWorkflowFunc()is called beforer.uploadWait.Wait(), which means the workflow is destroyed before the detached steps have finished and traced their state- The bug:
destroyWorkflowFunc()destroys the pods, but thenr.uploadWait.Wait()waits for detached step goroutines to callcompleteStep()which includesDestroyStep()again. But the key issue is that the tracer trace for service completion might happen AFTERdestroyWorkflowFunc()has already destroyed the pods.
Wait, let me re-read the issue. The issue says the pods ARE destroyed properly, but the API still shows services as "running". This means the tracer trace is NOT being sent for service completion.
Let me look at the order of operations more carefully:
Assistant
Now I see the issue. Let me trace the execution flow more carefully:
- In
Run(), stages are executed and detached steps return immediately after starting (they run in background goroutines) - After all stages complete,
destroyWorkflowFunc()is called, which callsDestroyWorkflow() DestroyWorkflow()destroys the pods- Then
r.uploadWait.Wait()waits for detached step goroutines to finish - The detached step goroutine calls
completeStep(), which callsWaitStep(), thenDestroyStep() - But
WaitStep()will fail because the pod has already been destroyed byDestroyWorkflow()
Let me look at
DestroyWorkflowin the kubernetes backend more carefully:- In
Assistant
Let me look at what happens when
WaitStepis called after the pod has been destroyed:Assistant
I see now. The key problem is:
- When
DestroyWorkflowis called inworkflow.go, it callsstopPodfor all steps including services (detached steps) - After
DestroyWorkflowreturns,uploadWait.Wait()is called which waits for the detached step goroutines - The detached step goroutine is blocked on
WaitStep()- it's waiting for the pod phase to change - When
DestroyWorkflowdeletes the pod, the informer should triggerpodUpdated, BUT the pod is being deleted, not updated - so the informer'sDeleteFuncisn't registered, onlyUpdateFunc - The context (
r.ctx) might be cancelled, causingWaitStepto return with an error
Let me check the complete step flow more carefully:
- When
Assistant
Let me look at the
r.ctxcontext. Inruntime.go, it's initialized tocontext.Background()but let me check if it gets replaced:Assistant
Let me check how the context is set up in the agent runner:
Assistant
So
workflowCtxis passed toWithContext. Let me look at howworkflowCtxis created:Assistant
Now I understand the flow better. The
workflowCtxis passed asr.ctxin runtime. This context:- Has a timeout
- Can be cancelled by sigterm or remote cancel
Now let me trace what happens in the normal execution case:
- All stages run, blocking steps complete, detached steps return immediately
destroyWorkflowFunc()is called - this destroys all pods including servicesr.uploadWait.Wait()waits for detached step goroutines to complete
In the detached step goroutine:
completeStep()is calledwaitForLogs()drains the log streamWaitStep(r.ctx, ...)is called - this waits for the pod to finish
The issue is: When
DestroyWorkflowdestroys the pod, theWaitStepcall in the detached goroutine is still waiting for the pod via the informer. But since the pod is deleted, the informer'sUpdateFuncwon't fire - only aDeleteFuncwould, which isn't registered!Let me check if the context gets cancelled at some point that would help:
Assistant
I see it now. The context
workflowCtxis only cancelled in error cases (sigterm, remote cancel, init error). In normal execution, the context is NOT cancelled, so:- Detached step goroutine calls
WaitStep(r.ctx, ...)wherer.ctxisworkflowCtx workflowCtxis not cancelled in normal flow- The informer waits for an update, but the pod gets deleted (not updated)
WaitStepblocks forever (or until timeout)
The fix should be: After the main steps complete and before destroying the workflow, the context should be cancelled, OR the workflow should wait for detached steps BEFORE destroying the workflow.
Looking at the
Run()function inworkflow.go:go// Now we can shutdown the workflow destroyWorkflowFunc() // Ensure all logs/traces are uploaded before finishing logger.Debug().Msg("waiting for logs and traces upload") r.uploadWait.Wait()- Detached step goroutine calls
Assistant
Let me look at the workflow_test.go to see if there's a pattern:
Assistant
Let me look at the runtime_test.go file to find the test for unbounded services:
Assistant
Let me continue investigating the test and dummy backend to understand how this case is handled.
Assistant
Let me look at the execution flow more carefully. In the dummy backend,
DestroyWorkflowcloses a channel thatWaitSteplistens on. But in the Kubernetes backend,WaitStepdoesn't listen for workflow shutdown.Assistant
main
Assistant
Command was not sent - please check the special commands format.
Assistant
Now I can see the problem clearly. In
WaitStepat line 313, onlyUpdateFuncis registered for the pod informer, but notDeleteFunc. When the pod is deleted byDestroyWorkflow, the informer's delete handler is not triggered, soWaitStepblocks waiting for the pod to transition to Succeeded/Failed state that will never come.The fix should be to also register a
DeleteFunchandler in theWaitStepinformer event handler, so that when a pod is deleted, thefinishedchannel is closed andWaitStepcan return.Let me check the exact code to understand the fix needed. )
Assistant
I need to properly format my tool call. Let me look at the WaitStep function in the kubernetes backend.
Assistant
Now I understand the issue. In
WaitStep, onlyUpdateFuncis registered for the pod informer. When a pod is deleted byDestroyWorkflow, the informer doesn't triggerUpdateFunc- it would triggerDeleteFuncinstead, which is not registered. This causesWaitStepto block forever waiting for the pod to transition to Succeeded/Failed state.Let me implement the fix:
Assistant
Now I understand the issue. The
WaitStepfunction in the Kubernetes backend only registers anUpdateFunchandler for the pod informer. When a pod is deleted (byDestroyWorkflow), the informer triggersDeleteFunc, notUpdateFunc. SinceDeleteFuncis not registered, thefinishedchannel is never closed, andWaitStepblocks forever.Additionally, after the
finishedchannel is closed, the code tries to fetch the pod to get its termination state. If the pod was deleted, this will fail.Let me implement the fix:
Assistant
Now I understand the issue. Let me implement the fix. The
WaitStepfunction needs to:- Register a
DeleteFunchandler so that when the pod is deleted byDestroyWorkflow, the waiting finishes - Handle the case where the pod is not found after the
finishedchannel is closed (because it was deleted)
- Register a
Assistant
Now I understand the issue. Let me implement the fix. The problem is that
WaitSteponly registers anUpdateFunchandler, but whenDestroyWorkflowdeletes the pod, onlyDeleteFuncwould fire. I need to:- Add a
DeleteFunchandler to signal when the pod is deleted - Handle the case where the pod doesn't exist because it was deleted
- Add a
Assistant
I need to check if the kubernetes.go file already imports the errors package, and then implement the fix. Let me check the imports in kubernetes.go.
Assistant
The errors package is not imported in kubernetes.go. I need to add it to the imports and then modify the WaitStep function to:
- Add a DeleteFunc handler to signal when the pod is deleted
- Handle the case where the pod doesn't exist because it was deleted (using the errors package to check for NotFound)
Let me implement the fix.
Assistant
The kubernetes.go file doesn't import the
k8s.io/apimachinery/pkg/api/errorspackage. I need to:- Add the import for
k8s.io/apimachinery/pkg/api/errors(aliased askube_errorsto avoid conflict with the standarderrorspackage) - Modify the
WaitStepfunction to:- Add a
DeleteFunchandler that signals when the pod is deleted - Handle the case where the pod is not found (because it was deleted) by returning a state indicating the step was terminated
- Add a
Let me implement this fix.
- Add the import for
Assistant
The user is waiting for me to continue with my fix. Let me verify the import was added correctly and then continue with modifying the WaitStep function.
Assistant
The import was added correctly. Now I need to modify the WaitStep function to:
- Add a
DeleteFunchandler that signals when the pod is deleted - Handle the case where the pod is not found (because it was deleted) by returning a state indicating the step was terminated
Let me look at the current WaitStep function and modify it.
- Add a
Assistant
Now I need to modify the WaitStep function to:
- Add a
DeleteFunchandler that signals when the pod is deleted - Handle the case where the pod is not found (because it was deleted) by returning an exited state
- Add a
Assistant
Now I need to replace the WaitStep function. The function starts at line 281 and ends at line 360 (the closing brace before the TailStep comment).
Let me replace the entire WaitStep function with the fixed version that:
- Adds a DeleteFunc handler
- Handles the NotFound error when getting the pod
Assistant
The function is 80 lines long. Let me use the edit command to replace it.
Assistant
The WaitStep function spans from line 281 to line 360. I need to replace this entire function with the fixed version that:
- Adds a DeleteFunc handler that signals when the pod is deleted
- Handles the NotFound error when getting the pod
Let me use the edit command to replace it.
Assistant
Now I need to replace the WaitStep function in the kubernetes.go file with the fixed version.
Assistant
Let me verify the fix was applied correctly by checking the file content.
Assistant
Let me verify the fix was applied correctly and check if the file compiles.
Assistant
The fix was applied correctly. Now let me verify that the code compiles correctly.
Assistant
The Go environment is not properly configured. Let me set up the Go environment and try again.
Assistant
The Go environment needs more environment variables set. Let me set all the required environment variables and try again.
Assistant
The code compiles successfully. Now let me run the existing tests to make sure I didn't break anything.
Assistant
The tests are passing so far. Let me see the rest of the test output to make sure all tests pass.
Assistant
All tests pass. Let me also run the tests for the pipeline runtime to make sure the fix doesn't break anything there.
Assistant
The runtime tests have a build tag. Let me check what build tag they use.
Assistant
The tests require the
testbuild tag. Let me run them with the proper build tag.Assistant
All tests pass. Let me also verify that the whole project compiles correctly.
Assistant
The build is downloading dependencies. Let me wait for it to complete.
Assistant
The build warning about web assets is expected - it's not related to my change. The important thing is that the kubernetes backend compiles correctly. Let me verify by building only the relevant packages.
Assistant
The pipeline package builds successfully. Let me review the complete diff of my changes.
Assistant
The user seems to have sent an empty message or there was some issue. Let me continue with my work. I've already:
- Made the fix to the kubernetes backend's WaitStep function
- Verified the code compiles
- Run the tests and they pass
Now I should summarize the changes and verify the fix is complete before submitting.
Assistant
I've completed my analysis and fix. Let me provide a summary and submit the changes.
Summary of the Bug Fix
Problem: In the Kubernetes backend's
WaitStepfunction, onlyUpdateFuncwas registered for the pod informer. WhenDestroyWorkflowdeletes a service pod, the informer doesn't triggerUpdateFunc— it would triggerDeleteFuncinstead, which wasn't registered. This causedWaitStepto block forever waiting for the pod to transition to a terminal state.Impact: Services (detached steps) would never have their final state traced to the server, leaving them showing as "running" in the UI and API, even after the pods were destroyed.
Fix: Two changes to
WaitStepinpipeline/backend/kubernetes/kubernetes.go:- Added a
DeleteFunchandler that signals thefinishedchannel when the pod is deleted - Handle the
NotFounderror when trying to get the pod's final state after deletion, returning a terminated state with exit code 0
Testing:
- ✅ Code compiles successfully
- ✅ All existing Kubernetes backend tests pass
- Added a
Assistant
The fix has been applied 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.