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.
Assistant
Let me explore the server and pipeline directories, focusing on how steps and services are managed.
Assistant
Let me explore the repository structure first.
Assistant
Let me look at the pipeline directory and understand how steps are executed.
Assistant
Let me look at the agent directory where step execution and lifecycle is managed.
Assistant
Now let me look at the pipeline runtime to understand how steps and services are executed and reported.
Assistant
Now let me look at the step.go file to understand how steps are executed and reported, especially services.
Assistant
Let me look at the runtime.go and the kubernetes backend.
Assistant
Now let me understand how the tracer works and how the server receives step state updates. Let me look at the tracer in the agent.
Assistant
I see that
stepState.Finishedis only set whenExitedis true. Let me understand the full flow. Let me look at how detached steps complete and whether theExitedfield is properly set.Let me look at the kubernetes backend's WaitStep and DestroyStep to understand how detached steps work.
Assistant
Now I see the key issue. For detached (service) steps, the
runDetachedStepstarts a background goroutine that callscompleteStep. IncompleteStep,WaitStepis called which for the kubernetes backend waits for the pod to reach a terminal state (Succeeded, Failed, Unknown). But a service like postgres runs indefinitely and never reaches a terminal state on its own.The key issue is: when does the detached step's background goroutine complete and trace the step as finished? It waits on
WaitStepwhich blocks until the pod terminates. The pod only terminates whenDestroyWorkfloworDestroyStepis called (which deletes the pod). ButDestroyStepis called insidecompleteStepAFTERWaitStepreturns. So there's a deadlock:WaitStepwaits for the pod to finish, but the pod is only destroyed afterWaitStepreturns.Let me check the git history to understand what changed.
Assistant
Let me look at the previous version of step.go to understand the detached step logic before the refactor.
Assistant
Let me look at how detached steps were handled before the big refactor. Let me find the original runtime code.
Assistant
There's a commit "Fix workflow hang on services (#6507)". Let me look at that.
Assistant
Let me check the docker backend's WaitStep to compare how it handles detached steps.
Assistant
Let me look at the helper functions in the kubernetes backend.
Assistant
Let me check the default StopTimeout and the flags for the kubernetes backend.
Assistant
Let me check the git history of the kubernetes backend's WaitStep.
Assistant
Let me look at how the server processes step state updates from the agent.
Assistant
Let me look at how the runtime handled this before the big refactor.
Assistant
Let me look at the commit that added the uploadWait logic.
Assistant
Let me look at the dummy backend to understand the expected contract.
Assistant
Let me look at the runtime tests to understand the expected behavior for services.
Assistant
Now I understand the issue. Let me look at the current
WaitStepmore carefully and the imports.Assistant
Good,
cacheanderrorsare already imported. Let me also check ifkube_api_errorsor similar is imported forIsNotFound. Let me check what's available for checking "not found" errors.Actually, I see
errors.IsNotFound(err)is used instopPod:err = engine.client.CoreV1().Pods(...).Delete(ctx, podName, deleteOpts) if errors.IsNotFound(err) {Wait, but that
errorsis from"errors"standard library? No,errors.IsNotFoundis fromk8s.io/apimachinery/pkg/api/errors. Let me check.Assistant
I see that in
pod.go,errorsis imported as"k8s.io/apimachinery/pkg/api/errors". But inkubernetes.go,errorsis imported as the standard library"errors". Let me verify by looking at the imports ofpod.go.Assistant
Now let me open the kubernetes.go file to look at the WaitStep function.
Assistant
Now I have a clear understanding of the issue. Let me implement the fix. First, let me add the kubernetes api errors import.
Assistant
Let me fix the import order to be alphabetical.
Assistant
That didn't work right. Let me just fix it directly.
Assistant
Let me use the edit command instead for precise changes.
Assistant
Now let me modify the WaitStep function. Let me view the current WaitStep.
Assistant
Now let me implement the fix by replacing the WaitStep function.
Assistant
Let me view the rest of the WaitStep function to make sure it's correct.
Assistant
I notice the closing brace
}of the WaitStep function is missing. Let me check.Assistant
The closing brace is missing. Let me fix it.
Assistant
Now let me build the kubernetes backend to check for compilation errors.
Assistant
The build succeeds. Now let me also make the environment variables persistent for future commands. Let me run the existing kubernetes tests and the runtime tests.
Assistant
All existing tests pass. Let me run a broader set of tests to make sure nothing is broken.
Assistant
The only error is in
web/web.gowhich is about missing web frontend build artifacts (unrelated to my changes). Let me verify the pipeline packages build fine.Assistant
The
web/web.goerror is coming from theserverpackage which imports the web package. Let me check if this is a pre-existing issue by checking if the dist directory exists.Assistant
The
web/distdirectory doesn't exist (needs to be built from the web frontend), but thepipeline/...andagent/...packages build fine. This is a pre-existing issue. Let me verify:Assistant
Now let me also verify the full diff of my changes.
Assistant
Let me verify the
errorsimport is still used (I changed some error returns).Assistant
Good,
errorsis still used at line 524. Let me also checkfmtis still used since I removed somefmt.Errorfcalls.Assistant
Let me check if there are kubernetes tests that test WaitStep behavior specifically.
Assistant
These tests use the fake kubernetes client. Let me run them specifically.
Assistant
All tests pass. Now let me write a temporary test to verify that my fix works — that
WaitStepreturns when the pod is deleted (simulating whatDestroyWorkflowdoes for service steps).Assistant
Both tests pass. Let me verify that these tests would fail without my fix (to confirm the fix is necessary). Let me temporarily stash the source change.
Assistant
The tests fail without my fix, confirming the issue. Now let me restore my fix.
Assistant
My fix works. Now let me clean up the temporary test file and run the full kubernetes and runtime test suites one more time.
Assistant
Let me also verify there are no leftover temp files and check the final git status.
Assistant
Let me run the broader backend test suite to make sure nothing else is broken.
Assistant
The dummy backend requires the
-tags testbuild flag. Let me run with that.Assistant
All tests pass. Let me do a final review of the complete diff.
Assistant
Let me do a final compilation check and verify all imports are still used.
Assistant
Let me run the full kubernetes test suite one final time to confirm everything passes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.