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 start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at the agent side, especially the kubernetes backend and how it handles services.
Assistant
Now let me look at the kubernetes backend to see how services are handled.
Assistant
Now let me look at the rpc peer update handler on the server side.
Assistant
Let me see the full Done handler:
Assistant
Now let me look at
completeChildrenIfParentCompleted:Assistant
Now let me look at the tests:
Assistant
Now let me look at the StepType definition:
Assistant
Now let me check the model package for StepType:
Assistant
Now let me check the compiler to see how services are created and what makes them detached:
Assistant
OK, so services are detached. Let me look more carefully at the detached step flow. The issue is that services are running indefinitely and never finish, but the agent side keeps the workflow "running" because the detached step is "running".
Let me check the agent state:
Assistant
Let me look at git log and recent changes:
Assistant
This is a relevant fix! Let me look at this commit:
Assistant
Now let me check the related commits:
Assistant
Now let me check the git blame to see when
completeChildrenIfParentCompletedwas introduced:Assistant
Let me look at the related commit
7efe8bf51:Assistant
Let me also look at sanitize.go to understand the full context:
Assistant
Now let me look at the most recent fix
855eae143and trace what changes were made. I need to understand the issue better. The user reports that services remain in "running" state in the API/UI, but the agent side tears them down. Let me trace through the flow:- The agent runs the workflow.
- A detached step (service) is started.
- The workflow completes the non-service steps.
- The agent tears down the workflow (calling DestroyWorkflow).
- The agent calls Done RPC.
- On the server side,
completeChildrenIfParentCompletedis called - it iterates overcompletedWorkflow.Childrenand updates any running children (services) to be StatusKilled.
Wait, let me look more carefully at this:
Assistant
Now, let me look at the
UpdateStepToStatusSkippedfunction more closely. The comment says "for daemons that are killed" - this means if the step was started (i.e., has Started != 0), the status is forcibly set to StatusSuccess, even if you passStatusKilled.So the current logic is:
- When
completeChildrenIfParentCompletedis called for a running service (which has Started != 0), it tries to mark it as StatusKilled - But the helper
UpdateStepToStatusSkippedoverrides this with StatusSuccess if Started != 0!
This is the bug! Let me verify by examining the issue and tests:
- When
Assistant
I see! The test
AlreadyStartedconfirms my suspicion. Look at this test:step, err := UpdateStepToStatusSkipped(mockStoreStep(t), model.Step{Started: 42}, int64(100), model.StatusSkipped)- It expects StatusSuccess even though the input wanted Skipped.
Looking at the implementation:
gofunc UpdateStepToStatusSkipped(store store.Store, step model.Step, finished int64, status model.StatusValue) (*model.Step, error) { step.State = status if step.Started != 0 { step.State = model.StatusSuccess // for daemons that are killed step.Finished = finished } return &step, store.StepUpdate(&step) }Assistant
Now let me see what happens in the actual flow. Let me look at the test for completeChildrenIfParentCompleted:
Assistant
Let me look at the
Donefunction more carefully:Assistant
Let me re-look at the workflow handling to understand the issue:
Assistant
Let me check git log between the d05c59737 and HEAD to see what was committed:
Assistant
Now let me check the complete picture - what does the agent tracer.go look like NOW (with the recent fixes):
Assistant
Now let me look at the options:
Assistant
Let me look at git blame for the specific lines in the Done RPC:
Assistant
Let me check stopPod:
Assistant
OK, so
stopPodcalls Kubernetes API to delete the pod. ThedeleteOptshasGracePeriodSecondsset toStopTimeout, which is 20 seconds by default.So when
destroyWorkflowFunc()is called:- All step pods are deleted (with graceful termination)
- After 20 seconds (or less), the pod is gone
- The
WaitStepshould detect this and return
But here's the issue - the
WaitStepusesr.ctx(workflow context) for select:goselect { case <-finished: case <-ctx.Done(): return nil, ctx.Err() }The
finishedchannel is closed when the pod's status becomes PodSucceeded, PodFailed, or PodUnknown. When the pod is deleted, the pod's status should become... hmm. Actually, when a pod is deleted, it doesn't go through a phase change necessarily. But the Kubernetes informer should detect the deletion and remove it from the cache.Assistant
Let me set up a test environment to actually reproduce the issue:
Assistant
Let me check the relevant kubernetes pod code:
Assistant
I think I found the bug! Let me look at the WaitStep in detail:
Assistant
Now let me look at the kubernetes_test.go file:
Assistant
Now let me write a reproducer to confirm my hypothesis:
Assistant
Let me write a reproducer test inside the kubernetes package:
Assistant
I've confirmed the bug. Now let me fix it:
Assistant
Now let me apply the fix. First, let me look at the full structure of WaitStep and TailStep:
Assistant
Let me look at how the existing code handles errors:
Assistant
Let me also check the pod.go to see what's happening withthe pod creation/deletion:
Assistant
Let me look at the existing kubernetes_test.go for context on testing patterns:
Assistant
Now let me apply the fix. First, I need to understand the full flow better and design the fix carefully:
Assistant
Now let me apply the fix:
Assistant
Let me apply the fix to the kubernetes.go file:
Assistant
Now let me apply the fix:
Assistant
Now let me actually apply the fix using the edit tool:
Assistant
Good -
k8s.io/apimachinery/pkg/api/errorsis not imported. Let me check how the existing code uses it - actually pod.go has the import. So I need to add it to kubernetes.go too. Or I could use the same package style. Let me check pod.go:Assistant
Now let me apply the fix. I need to import the k8s errors package with an alias:
Assistant
Let me fix the import ordering:
Assistant
Now let me apply the actual fix to WaitStep:
Assistant
Now let me also apply the same fix to TailStep:
Assistant
Now let me build to make sure it compiles:
Assistant
Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All tests pass. Now let me write a test to confirm the fix:
Assistant
The fix works. Now let me also verify that the test FAILS without the fix:
Assistant
Confirmed - the test fails without my fix. Now let me restore the fix:
Assistant
Now let me run all the tests to make sure I haven't broken anything:
Assistant
All kubernetes tests pass. Now let me also run the runtime tests:
Assistant
The router test failure is unrelated (missing web/dist). Let me check the rpc tests:
Assistant
Assistant
Let me look at what
pipeline.ExitCodeKilledis:Assistant
Assistant
Let me look at the dummy backend's service handling more carefully:
Assistant
Let me look at the TestWorkflowFailingServiceDoesNotFailWorkflow test more carefully:
Assistant
Let me check the docker backend's WaitStep for comparison:
Assistant
Let me look at the runtime to understand how to track workflow destruction:
Assistant
Let me reconsider. Looking at the user's expectation, they want the workflow to be green (Success). With ExitCode=143, the workflow would be StatusFailure. Let me check what the dummy backend does and align the behavior. Let me check what happens if we just don't trace the synthetic state by examining more of the runtime flow:
Assistant
Let me update my fix to use ExitCode=0 for synthetic state. This aligns with the dummy backend's behavior and provides the green workflow status the user expects:
Assistant
Now let me verify the test passes:
Assistant
Now let me add a proper test for the WaitStep fix to ensure it doesn't regress:
Assistant
The test passes. Let me also run all the kubernetes tests:
Assistant
All kubernetes tests pass. Now let me run the runtime tests to make sure I haven't broken anything:
Assistant
All runtime tests pass. Now let me look at the broader server tests:
Assistant
The router/web tests fail because of the missing web/dist directory. This is unrelated to my change. Let me verify this is pre-existing:
Assistant
Good, the router/web failure is pre-existing (unrelated to my change). Now let me run all related tests one more time:
Assistant
All pipeline tests pass. Let me also run the agent tests:
Assistant
Now let me look at the final state of my changes:
Assistant
Let me verify the test changes are included:
Assistant
Let me also run a broader test to make sure nothing is broken:
Assistant
All tests pass. Let me also verify my fix handles the user's specific scenario by writing an integration-style test using the runtime:
Assistant
Now I need to also check the tests to see if my fix is correct - i.e. when a service is torn down, the workflow should report ExitCode=0 (not ExitCode=143). Let me also check what the dummy backend does in this case, since I want to make sure my understanding is correct:
Assistant
Let me remove the integration test I added (I should not add new tests):
Assistant
Wait, the instructions say I should not add new tests. Let me remove the test I added to kubernetes_test.go and only keep my source code fix:
Assistant
Now let me verify the test file is back to its original state:
Assistant
The test file is back to its original state. Let me check the overall diff:
Assistant
The diff looks clean. Now let me run all the tests one more time to make sure everything still works:
Assistant
All pipeline tests pass. Let me also confirm my fix using a temporary script as instructed:
Assistant
Let me look at the final state of my fix:
Assistant
Let me verify my changes are complete and correct:
Assistant
Let me do a final check of my solution. The fix is clean and addresses the issue:
Summary of the fix:
The issue was that the Kubernetes backend's
WaitStepandTailSteponly registeredUpdateFunchandlers on the pod informer. When a pod is deleted (which happens during workflow teardown for services), the informer emits aDeleteevent, not anUpdateevent. This causedWaitStepto block forever waiting for a terminal pod phase that would never come, leaving the service step stuck in "running" state on the server.The fix:
-
Added a
DeleteFunchandler in bothWaitStepandTailStepthat closes thefinished/upchannel when a pod is deleted. -
Added handling for
IsNotFounderrors in theGetcall inWaitStepto return a synthetic state withExitCode: 0when the pod was deleted. This matches the dummy backend's behavior of treating workflow teardown as a successful completion, which keeps the pipeline "green" for services. -
The fix correctly handles the tombstone case where the local cache has lost the object.
-
Assistant
The only error is the pre-existing web/dist issue (unrelated to my fix). Let me also make sure the tests pass:
Assistant
All tests pass. Let me now provide my final reasoning for the checklist:
Checklist review:
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I wrote a temporary test that created a pod, called
WaitStepin a goroutine, then deleted the pod, and verified thatWaitStepdid not return (timed out after 5 seconds). This confirmed the bug.
- Yes, I wrote a temporary test that created a pod, called
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, I confirmed that the test now passes with my fix. The test was removed before submitting, but the fix is in place.
-
I have thought about edge cases and made sure my fix handles them as well.
- Yes, I considered:
- Pod created and immediately deleted (handled by in call)
- Yes, I considered:
-
Assistant
The fix is minimal, focused, and well-documented. Let me submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.