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 the issue. The bug is about Woodpecker CI services not being cleaned up properly after a pipeline completes when using the Kubernetes backend. The services pods are destroyed, but the Woodpecker API still shows them as "running" state. They're only cleaned up after a timeout.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore more of the repository structure to understand how it's organized.
Assistant
The issue is about services not being cleaned up properly after pipeline steps complete. The Kubernetes backend destroys the pods, but the Woodpecker API still shows the service steps as "running". Let me look at the pipeline and server code to understand how services are managed.
Assistant
Let me look at the agent runner code, which is responsible for executing pipelines:
Assistant
Now let me look at the pipeline runtime code:
Assistant
Now let me look at the step execution code:
Assistant
Let me search for how services are handled:
Assistant
Now let me look at the runtime.go and understand how the workflow is structured:
Assistant
Now let me look at the convert.go to understand how services are configured:
Assistant
I can see that services are set as
Detached = true. Now I need to understand what happens when the workflow completes. The issue is that services are not being properly stopped when the pipeline completes. Let me look at the workflow tests and the backend interface:Assistant
Now let me understand the flow better. The issue is about services that remain in "running" state in the API/UI even after they've been destroyed by the backend. Let me search for how step states are reported to the server:
Assistant
Let me look at how the tracer is created in the agent runner:
Assistant
Now I understand the tracing mechanism. The tracer reports step states to the server. When a step is destroyed, it sends an update with
Exited: trueandFinishedtimestamp. Let me look at the server-side code that handles these updates:Assistant
Let me look at the
checkWorkflowAllowsStepUpdatefunction and the pipeline update functions:Assistant
Now let me look at the server pipeline package to understand how step status is updated:
Assistant
Now let me look at the Done function in rpc.go more carefully, especially how it handles the cleanup of steps:
Assistant
Let me continue reading the Done function:
Assistant
Now let me look at the
completeChildrenIfParentCompletedfunction more closely. This is the key function that handles the cleanup of service steps when the parent workflow completes:Assistant
Now I see the problem! Let me check
UpdateStepToStatusSkipped:Assistant
Let me look at the function more closely:
Assistant
Assistant
The previous command didn't execute. Let me try again.
Assistant
Let me look at the WaitStep implementation in the Kubernetes backend to understand what happens when the pod is destroyed.
Assistant
I see the issue! The
WaitStepfunction in the Kubernetes backend uses an informer that only handlesUpdateFunc, notDeleteFunc. WhenDestroyWorkflowdeletes the pod, the informer won't receive a delete event, soWaitStepwill block until the context is cancelled (which only happens on timeout).Let me verify this by looking at the informer setup:
Assistant
I found the bug! The
WaitStepfunction in the Kubernetes backend only registers anUpdateFunchandler for the pod informer, but does NOT register aDeleteFunchandler. WhenDestroyWorkflowdeletes the service pods, the informer never notifiesWaitStepthat the pod was deleted, soWaitStepkeeps waiting until the context times out.Let me verify this by looking at the DestroyWorkflow function:
Assistant
I need to continue investigating the bug. Let me look at the DestroyWorkflow function in the Kubernetes backend.
Assistant
Now I understand the bug. The
DestroyWorkflowfunction deletes all pods (including service pods), but theWaitStepfunction only listens forUpdateFuncevents in the pod informer. When a pod is deleted (rather than updated to a terminal state), the informer never notifiesWaitStep, so it blocks until the context times out.Let me check the
stopPodfunction to confirm:Assistant
The user's system showed that the previous command failed with a non-zero exit code. Let me search for the stopPod function again using a different approach.
Assistant
The user is asking me to continue investigating the bug. Let me look for the stopPod function definition. The previous grep only showed usage of stopPod, not its definition. Let me search for where it's defined.
Assistant
Let me look at the stopPod function to understand what it does when a pod is destroyed.
Assistant
I've identified the bug. When
DestroyWorkflowcallsstopPodto delete the service pods, theWaitStepfunction in the Kubernetes backend never receives notification because it only listens forUpdateFuncevents in the informer, notDeleteFuncevents.Let me look at the full
WaitStepfunction to create the fix:Assistant
Now I fully understand the bug. The
WaitStepfunction in the Kubernetes backend only registers anUpdateFunchandler for the pod informer. WhenDestroyWorkflowdeletes service pods viastopPod, the informer never notifiesWaitStepbecause there's noDeleteFunchandler. This causesWaitStepto block indefinitely until the context times out.Additionally, after the
finishedchannel closes, the code tries toGetthe pod to read its status - but if the pod was deleted, this will fail.Let me check what error handling exists and then create the fix:
Assistant
Now I have a clear understanding of the bug. The fix needs to:
- Add a
DeleteFunchandler to the informer inWaitStepso that when a pod is deleted,WaitStepknows the step has finished - Handle the case where the pod is not found after waiting (because it was deleted)
Let me open the file and create the fix:
- Add a
Assistant
The user seems to have encountered an error with the tool call. Let me try a different approach to fix the issue. I need to understand the bug and create a fix.
The bug is:
- The
WaitStepfunction in the Kubernetes backend only registers anUpdateFunchandler for the pod informer - When
DestroyWorkflowdeletes service pods viastopPod, the informer never notifiesWaitStepbecause there's noDeleteFunchandler - This causes
WaitStepto block indefinitely until the context times out
The fix needs to:
- Add a
DeleteFunchandler to the informer inWaitStepso that when a pod is deleted,WaitStepknows the step has finished - Handle the case where the pod is not found after waiting (because it was deleted)
Let me use the bash terminal to make the edit.
- The
Assistant
Now I understand the bug. The
WaitStepfunction only listens forUpdateFuncevents, notDeleteFunc. WhenDestroyWorkflowdeletes the service pods, the informer never notifiesWaitStep, causing it to block until context timeout.Let me fix this by adding a
DeleteFunchandler and handling the case where the pod is not found after waiting:Assistant
Now I can see the exact code. The bug is:
- Line 313-316: The informer only registers
UpdateFunc, notDeleteFunc- so when pods are deleted,WaitStepnever knows - Line 331-334: After
finishedchannel closes, it tries toGetthe pod - but if deleted, this fails
Let me fix this by adding a
DeleteFunchandler and handling the "pod not found" case:- Line 313-316: The informer only registers
Assistant
Now I understand the fix needed. I need to:
- Add a
DeleteFunchandler to the informer so that when pods are deleted (like service pods),WaitStepis notified - Handle the case where the pod is not found after waiting (because it was deleted)
Let me first check what imports are already available:
- Add a
Assistant
Now I'll apply the fix. The changes needed are:
- Add a
DeleteFunchandler to the informer - Track whether the pod was deleted
- Handle the "pod not found" case after waiting
- Add a
Assistant
The patch approach failed due to formatting issues. Let me manually edit the file using the edit command.
Assistant
The open command failed. Let me try using edit directly with the --file option to modify the file.
Assistant
It seems the edit command syntax is not working properly. Let me try using sed to make the changes.
Assistant
Good, the first change was applied. Now I need to add the
podDeletedHandlerfunction after thepodUpdatedfunction and update the informer event handler to includeDeleteFunc.Assistant
Let me continue fixing the bug. I need to:
- Add the
podDeletedHandlerfunction after line 311 - Modify the informer event handler to include
DeleteFunc: podDeletedHandler - Modify the select statement to handle the
podDeletedchannel - Handle the "pod not found" case when getting the pod after waiting
Let me use sed to make these changes step by step.
- Add the
Assistant
The user seems to be waiting for me to continue with the fix. Let me continue making the changes to the file. I need to:
- Modify the informer event handler to include
DeleteFunc: podDeletedHandler - Modify the select statement to handle the
podDeletedchannel - Handle the "pod not found" case when getting the pod after waiting
Let me check the current state of the file and continue with the modifications.
- Modify the informer event handler to include
Assistant
Good progress! Now I need to:
- Add
DeleteFunc: podDeletedHandlerto the informer event handler (line 326-327) - Add a case for
podDeletedchannel in the select statement (line 336-340) - Handle the "pod not found" case after getting the pod (line 343-344)
Let me continue with the edits.
- Add
Assistant
Now I need to continue with the remaining changes to the WaitStep function. Let me add the case for the
podDeletedchannel in the select statement and handle the "pod not found" case.Assistant
I see the indentation is wrong for the
DeleteFuncline. Let me fix that and continue with the remaining changes.Assistant
The sed command didn't change anything because the pattern was the same. Let me look at the actual content to see what the indentation looks like.
Assistant
I see the issue - the
DeleteFuncline has one extra tab (4 tabs instead of 3). Let me fix that:Assistant
The user is seeing my response being generated. Let me continue with the fix. I need to:
- Add the case for the
podDeletedchannel in the select statement - Handle the "pod not found" case after getting the pod
- Add the case for the
Assistant
The indentation is now correct. Now I need to:
- Add a case for the
podDeletedchannel in the select statement - Handle the "pod not found" case after getting the pod
- Add a case for the
Assistant
Now I need to handle the "pod not found" case when getting the pod after waiting. Let me check the current state and make that change.
Assistant
Now I need to handle the "pod not found" case when getting the pod. This is important for race conditions where the pod might be deleted between the informer event and the Get call.
Assistant
The user hasn't sent any new message. The system is reminding me that I haven't been tool call enabled. Let me continue with the fix by verifying the changes I made and checking if they are correct.
Assistant
All the changes have been applied. Let me verify the complete
WaitStepfunction to ensure everything looks correct:Assistant
The fix looks good. Now let me verify that the code compiles and run the existing tests to make sure nothing is broken:
Assistant
Let me set up the Go environment and try building again:
Assistant
The user is not providing new information - they're waiting for me to continue. I need to set up the Go environment properly to compile and test the code. Let me set all the necessary environment variables.
Assistant
I see several issues. Let me check what went wrong and fix them:
Assistant
I see the issue. My sed commands accidentally modified the
TailStepfunction as well, because the patterncase <-ctx.Done():appears in multiple places. I also need to use the correctIsNotFoundfunction from the kubernetes API. Let me revert and fix this properly.Assistant
Let me revert and apply the fix more carefully:
Assistant
Let me start fresh with a more careful approach. I'll examine the exact lines I need to modify and make precise changes.
Assistant
Let me use a Python script to make precise changes to the file:
Assistant
The user didn't provide a new message. The system is reminding me that I haven't been tool call enabled. Let me continue with my fix. I need to check if there's a kubernetes errors package imported and use the correct IsNotFound function.
Assistant
Now I see that other files in the same package use
k8s.io/apimachinery/pkg/api/errors. But inkubernetes.go, there's already"errors"from the standard library. I need to check if I can add an aliased import for the kubernetes errors package.Assistant
Now I understand the import situation. The standard library
"errors"is already imported, so I need to use an alias for the kubernetes errors package. Let me create a Python script to make all the changes correctly:Assistant
The user didn't provide a new message, so I need to continue with my work. Let me verify that the changes were applied correctly by checking the file.
Assistant
The WaitStep function changes look correct. Let me verify the import was added correctly:
Assistant
The import looks correct. Now let me verify the code compiles:
Assistant
Let me check what happened at line 414 in the TailStep function:
Assistant
The Python script also modified the TailStep function because it matched the same pattern. I need to revert and make more targeted changes:
Assistant
Let me create a more targeted fix that only modifies the
WaitStepfunction:Assistant
The user hasn't provided any new input. Let me continue with verifying the fix by checking the modified file and attempting to compile it.
Assistant
I see the
IsNotFoundcheck wasn't added. Let me fix that:Assistant
The IsNotFound check wasn't added. The python script's pattern matching didn't work correctly. Let me fix this manually with sed.
Assistant
The user's message is just a system reminder. Let me continue verifying the fix and building the code to make sure it compiles correctly.
Assistant
The IsNotFound check is now in place. Let me verify the import was added and try to build:
Assistant
Now let me try to build the code to verify it compiles correctly:
Assistant
The code compiles successfully. Now let me run the existing tests for the kubernetes backend to make sure the changes don't break anything:
Assistant
The WaitStep tests pass. Let me run all the kubernetes backend tests to ensure nothing is broken:
Assistant
All kubernetes backend tests pass. Let me check the full diff of my changes and run the broader pipeline tests:
Assistant
The diff looks correct. Let me run the broader pipeline runtime tests to make sure everything works together:
Assistant
Let me check the test files for the pipeline runtime package. The user wants me to continue testing my changes. Let me look for the test files and run them.
Assistant
I see there was a FAIL at the end. Let me check which tests failed:
Assistant
The dummy backend has a build failure - let me check if that's related to my changes:
Assistant
The user hasn't provided any new input. Let me continue investigating the dummy backend test failure to make sure it's not related to my changes.
Assistant
The dummy backend failure is not related to my changes - it's a build constraint issue that excludes all Go files. This is a pre-existing issue in the repository, not something I caused. Let me verify this by checking if the dummy package had this issue before my changes.
Assistant
Good - the dummy backend failure is pre-existing and not related to my changes. Let me restore my changes and continue verifying.
Assistant
Let me verify the final state of my changes and run the kubernetes backend tests one more time:
Assistant
Let me run the kubernetes backend tests one more time to make sure everything is working correctly.
Assistant
All tests pass. Let me verify my understanding of the fix by reviewing the complete diff:
Assistant
I've successfully fixed the bug. Let me summarize what was done and submit the changes.
Summary
Root Cause: The
WaitStepfunction in the Kubernetes backend only registered anUpdateFunchandler for the pod informer. WhenDestroyWorkflowdeleted service pods viastopPod, the informer never notifiedWaitStepbecause there was noDeleteFunchandler. This causedWaitStepto block indefinitely until the workflow context timed out.Fix: Added three changes to
WaitStepinpipeline/backend/kubernetes/kubernetes.go:- Added
DeleteFunchandler - ApodDeletedHandlerfunction that signals when the watched pod is deleted, and registered it with the informer - Handle
podDeletedchannel - Added a case in the select statement to return immediately when the pod is deleted - Handle
IsNotFounderror - Added a check forerrors.IsNotFoundwhen getting the pod after the informer signals completion, since the pod may have been deleted in a race condition
- Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.