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
I'll start by exploring the repository structure to understand the codebase and identify where the issue might be.
Assistant
Let me explore the server directory since the issue mentions it's related to the server component:
Assistant
The issue is about pipeline services not being cleaned up properly. Since it mentions the Kubernetes backend, let me search for pipeline execution and service cleanup related code. Let me first look at the pipeline directory:
<tool_call> <function=bash_terminal> <parameter=command> ls -la server/pipeline/ </tool_call>
Assistant
I need to explore the pipeline directory structure to understand how pipeline services are managed and cleaned up.
Assistant
Let me search for files related to service cleanup and pipeline completion. The issue mentions that the pods are destroyed but the API still shows the service as running. Let me search for service-related code:
<tool_call> <function=bash_terminal> <parameter=command> find . -type f -name "*.go" | xargs grep -l "service.*state|service.*running" | head -20 </tool_call>
Assistant
I need to search for files related to service cleanup. Let me use a proper bash command.
Assistant
Let me search for files related to service cleanup in the server/pipeline directory:
Assistant
The issue mentions that the issue appeared in version 3.14.x. Let me search for recent changes related to service cleanup or pipeline completion. Let me look at the cancel.go file which likely handles service cleanup when pipelines are canceled:
Assistant
Let me look at the step_status.go file to understand how step status is updated, particularly for service steps:
<tool_call> <function=bash_terminal> <parameter=command> cat server/pipeline/step_status.go </tool_call>
Assistant
I need to view the step_status.go file to understand how step status is updated, particularly for service steps.
Assistant
I need to understand how pipeline workflows are finalized when all steps complete. Let me search for workflow status or finalization code.
<tool_call> <function=bash_terminal> <parameter=command> cat server/pipeline/workflow_status.go </tool_call>
Assistant
I need to view the workflow_status.go file to understand how workflows are finalized.
Assistant
The issue is about services not being properly stopped after pipeline completion. I need to look for where services are actually stopped/destroyed in the Kubernetes backend and where the step status should be updated.
Let me search for where services are stopped:
<tool_call> <function=bash_terminal> <parameter=command> find . -type f -name "*.go" | xargs grep -l "StopService|destroy.*service" | head -20 </tool_call>
Assistant
Let me search for service-related code more specifically to find where services are stopped.
Assistant
Let me try a different approach to search for service-related code.
<tool_call> <function=bash_terminal> <parameter=command> grep -r "service" --include="*.go" ./pipeline | grep -i stop </tool_call>
Assistant
Let me search in the pipeline directory for service-related code:
Assistant
The command failed. Let me look at the agents directory since services might be stopped there:
<tool_call> <function=bash_terminal> <parameter=command> ls -la agent/ </tool_call>
Assistant
Let me list the agent directory to understand its structure.
Assistant
Let me look at the runner.go file in the agent directory to see how services are handled:
Assistant
Let me look at the pipeline runtime to see how services are handled:
<tool_call> <function=bash_terminal> <parameter=command> ls -la pipeline/ </tool_call>
Assistant
Let me look at the pipeline directory structure to understand how services are handled.
Assistant
I need to explore the pipeline/runtime directory to see how services are handled.
<tool_call> <function=bash_terminal> <parameter=command> ls -la pipeline/runtime/ </tool_call>
Assistant
Let me look at the pipeline/runtime directory structure.
Assistant
Let me look at the runtime.go file to understand how the pipeline runtime handles services.
<tool_call> <function=bash_terminal> <parameter=command> cat pipeline/runtime/runtime.go </tool_call>
Assistant
I need to use the proper command syntax to run bash commands. Let me try again with the correct format.
Assistant
Let me now look at the workflow.go file to see how workflows are executed and services are handled.
Assistant
Let me look at the step.go file to see how steps are executed and when service teardown happens:
Assistant
Now I can see that in the
completeStepfunction, the container is destroyed withr.engine.DestroyStep(runnerCtx, step, r.taskUUID), but the state is traced after that. Let me look at the state.go file to understand the step state structure:<tool_call> <function=bash_terminal> <parameter=command> cat pipeline/state/state.go 2>/dev/null || cat pipeline/state/*.go </tool_call>
Assistant
Let me look at the pipeline/state directory to understand the state structure.
Assistant
I need to examine the state.go file to understand the step state structure and how it relates to the issue.
<tool_call> <function=bash_terminal> <parameter=command> cat pipeline/state/state.go </tool_call>
Assistant
I need to use the proper syntax with the function call wrapper. Let me read the state.go file.
Assistant
Now I need to look at the backend types to understand how StepState is defined. Let me search for where the backend types are defined and where StepType is defined:
<tool_call> <function=bash_terminal> <parameter=command> grep -r "StepType" --include="*.go" ./pipeline/backend | head -30 </tool_call>
Assistant
Let me continue looking for StepType and service-related constants in the backend package.
Assistant
Now let me look at the StepType definition and the backend types:
Assistant
Now I understand the issue better. When a step completes, the backend's
DestroyStepis called, and then the state is traced via the tracer. Let me look at how the state is traced and how it reaches the server. Let me check the agent RPC:<tool_call> <function=bash_terminal> <parameter=command> ls -la agent/rpc/ </tool_call>
Assistant
Let me explore the agent/rpc directory to understand how the agent reports state back to the server.
Assistant
Let me look at the client_grpc.go file to see how state is reported back to the server:
Assistant
Now let me look at the server-side RPC handler to see how it processes the step state updates:
<tool_call> <function=bash_terminal> <parameter=command> cat server/rpc/rpc.go | head -300 </tool_call>
Assistant
Let me continue to look at the server-side RPC handler to understand how it processes step state updates.
Assistant
Now I can see the issue. Looking at the
Updatefunction in the RPC handler, I can see that it callspipeline.UpdateStepStatusfor each step. Let me look at how the state is calculated and updated for service steps. Let me check thecheckWorkflowAllowsStepUpdatefunction and theCalcStepStatusfunction more carefully:Assistant
Let me search for the
checkWorkflowAllowsStepUpdatefunction definition:Assistant
Let me search for the checkWorkflowAllowsStepUpdate function definition in all files:
Assistant
Let me look at the sanitize.go file to understand the validation logic:
<tool_call> <function=bash_terminal> <parameter=command> cat server/rpc/sanitize.go </tool_call>
Assistant
I need to use the proper command syntax to read files. Let me read the sanitize.go file to understand the step update validation logic.
Assistant
Now let me trace through the code flow. The issue is that service steps remain "running" after the pipeline completes. Looking at the
CalcStepStatusfunction instep_status.go, I need to understand when a service step should transition to a terminal state. Let me search for how services are specifically handled:Assistant
The issue is that service steps don't report back their completion state properly. Let me search for how service steps differ from regular steps in the code flow.
Let me check if services are detached steps and how that affects the state reporting.
<tool_call> <function=bash_terminal> <parameter=command> grep -rn "Detached|detached" pipeline/ </tool_call>
Assistant
I need to search for how detached steps are handled, particularly service steps. Let me use bash to search for Detached/detached in the pipeline directory.
Assistant
Looking at this, I can see that service steps are set as
Detached = truein the compiler. Let me check the convert.go file to understand how services are compiled:<tool_call> <function=bash_terminal> <parameter=command> cat pipeline/frontend/yaml/compiler/convert.go | head -200 </tool_call>
Assistant
Let me view the convert.go file to understand how services are compiled and marked as detached.
Assistant
Now I can see that service steps are marked as detached. The issue is that when a service is detached, it runs in the background and the
runDetachedStepfunction returns immediately without waiting for the service to complete. The service is eventually destroyed when the workflow is destroyed, but the state is never reported back to the server for service steps.Let me look at the
runDetachedStepfunction again and understand the flow:<tool_call> <function=bash_terminal> <parameter=command> cat pipeline/runtime/step.go | grep -A 50 "func (r *Runtime) runDetachedStep" </tool_call>
Assistant
Let me view the runDetachedStep function to understand how detached steps (including services) are handled.
Assistant
Now I understand the issue! When a service step completes (or is destroyed), the
completeStepfunction inrunDetachedStepcallsr.traceStep(processState, err, step)to report the final state. However, looking at the backend implementations (especially Kubernetes), the issue is that whenDestroyStepis called, the backend might not return a properStatewithExited: true.Let me check the Kubernetes backend to see how it handles service steps during destruction:
Assistant
Let me look at the DestroyStep and DestroyWorkflow functions in the Kubernetes backend:
Assistant
Now I need to check how the
WaitStepis called in the workflow lifecycle. Looking at thecompleteStepfunction in step.go, it callsr.engine.WaitStep(r.ctx, step, r.taskUUID)to wait for the step to complete. However, for service steps that are detached, they never complete normally - they run until destroyed. Let me check how this interacts with theDestroyStepcall:Assistant
The issue is now clearer. The
completeStepfunction:- Calls
WaitStepto wait for the step to complete - Then calls
DestroyStepto clean up
For service steps,
WaitStepshould block until the service completes. However, looking at the problem description, the service pods are destroyed but the API still shows them as "running". This means thatWaitStepis not returning a proper state for service steps.Let me check how the Kubernetes backend implements
WaitStepfor service steps:<tool_call> <function=bash_terminal> <parameter=command> grep -n "func.*WaitStep" pipeline/backend/kubernetes/kubernetes.go </tool_call>
- Calls
Assistant
Let me search for the WaitStep implementation in the Kubernetes backend:
Assistant
Let me look at the WaitStep implementation in the Kubernetes backend:
Assistant
Let me continue viewing the WaitStep function to see how it returns the state:
Assistant
Now I need to understand the flow better. Let me check how service pods are identified and handled differently from regular steps. Let me look at the stepToPodName function:
Assistant
Let me view the stepToPodName function and see how service steps are handled:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '83,150p' pipeline/backend/kubernetes/pod.go </tool_call>
Assistant
I need to view the stepToPodName function to understand how service steps are handled in the Kubernetes backend.
Assistant
Let me continue investigating the isService function to understand how service steps are identified.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "isService" pipeline/backend/kubernetes/*.go </tool_call>
Assistant
I need to search for the isService function to understand how service steps are identified in the Kubernetes backend.
Assistant
Let me view the isService function to understand how service steps are identified.
Assistant
Now I understand the issue better. Service steps are identified by their type. Let me check if there's any special handling for services in the WaitStep function. Let me search for how WaitStep handles service pods differently:
<tool_call> <function=bash_terminal> <parameter=command> grep -B5 -A5 "StepTypeService" pipeline/backend/kubernetes/kubernetes.go | head -30 </tool_call>
Assistant
Let me check the git log to see what changes were made recently related to service steps or pipeline completion:
Assistant
The issue mentions that this regression appeared in version 3.14.x. I see commit
60df1c618 Fix workflow hang on services (#6507)which sounds very relevant. Let me look at what this commit changed:Assistant
This commit seems to be related to the issue! Let me see the full diff:
Assistant
Now I see the issue! Looking at the commit, it changed the flow to explicitly call
destroyWorkflowFunc()after all stages execute. However, the issue is that whenDestroyStepis called for service steps, it doesn't return a proper state withExited: truebefore callingDestroyStep. This means that service steps never get their final state reported back to the server.Let me check what the current state of the code is:
<tool_call> <function=bash_terminal> <parameter=command> git log --oneline -1 </tool_call>
Assistant
Let me check the current commit and then look at the runtime.go workflow to understand the flow better:
Assistant
Now let me check if commit 60df1c618 is in the current HEAD:
<tool_call> <function=bash_terminal> <parameter=command> git branch -a --contains 60df1c618 | head -5 </tool_call>
Assistant
Let me check if commit 60df1c618 is an ancestor of the current HEAD by checking if it's in the history.
Assistant
Good! So commit 60df1c618 is already in the history. This means the fix for the workflow hang on services has already been applied. However, the issue reported is that services still show as "running" even after the pods are destroyed. This suggests that while the workflow doesn't hang anymore, the service step state is not being properly updated to a terminal state.
Let me understand the issue better. Looking at the commit message and the changes:
- The commit fixed the workflow hanging issue by explicitly tearing down services after all stages complete
- However, when
DestroyWorkflowis called, it destroys all pods (including service pods) - But the
completeStepfunction inrunDetachedStepcallsWaitStepwhich waits for the step to complete - For service steps,
WaitStepin the Kubernetes backend waits until the pod is in a terminal state (Succeeded/Failed/Unknown)
Assistant
Let me examine the Kubernetes backend's WaitStep implementation more carefully to understand how it handles service steps and what happens when pods are deleted.
Assistant
Now I see the issue! The
WaitStepfunction waits for the pod to reach a terminal state (Succeeded/Failed/Unknown) before returning. However, for service steps that run until destroyed, the pod never naturally reaches a terminal state - it's deleted byDestroySteporDestroyWorkflow.When the context is cancelled (which happens when the workflow is completing),
WaitStepreturnsctx.Err()without getting a proper state back. This means the state reported to the server never includesExited: true, so the step remains in a "running" state.The fix should be:
- When
WaitStepis interrupted by context cancellation (for service steps), it should still return a proper state withExited: trueand the appropriate exit code - Alternatively, the
completeStepfunction should handle the case whereDestroyStepis called beforeWaitStepreturns
Let me check the current behavior in the
completeStepfunction to see how it handles context cancellation:<tool_call> <function=bash_terminal> <parameter=command> sed -n '140,200p' pipeline/runtime/step.go </tool_call>
- When
Assistant
Let me examine the
completeStepfunction to see how it handles context cancellation:Assistant
Now I understand the issue! Looking at the
completeStepfunction inpipeline/runtime/step.go:- When
WaitStepreturns due to context cancellation (line 156), the code setswaitState.Error = pipeline_errors.ErrCancel - However, it does not set
waitState.Exited = true - This means the step never transitions to a terminal state, and remains "running"
Let me create a test to reproduce this issue and then fix it:
- When
Assistant
Let me look at the existing tests for detached service steps to understand the expected behavior:
Assistant
Now let me look at how the traces are validated for service steps to see what state they should have:
<tool_call> <function=bash_terminal> <parameter=command> grep -B 5 -A 20 "TestWorkflowWithServiceStep" pipeline/runtime/runtime_test.go | head -60 </tool_call>
Assistant
Let me look at the service step test to see what the expected trace states are:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '200,250p' pipeline/runtime/runtime_test.go </tool_call>
Assistant
I need to run a command to view the test file. Let me use the proper syntax.
Assistant
Now I can see the issue clearly! The test
TestWorkflowWithServiceStepexpects that each step (including service steps) should have an "exited" trace withExited=true. This is what's missing when service steps are destroyed at workflow completion.Let me run the existing tests to see if they pass:
Assistant
Let me set up the Go module cache first:
Assistant
Let me set the necessary environment variables and try again:
<tool_call> <function=bash_terminal> <parameter=command> export GOMODCACHE=/tmp/go-mod-cache && export GOPATH=/tmp/go && export GOCACHE=/tmp/go-cache && go test -v -run TestWorkflowUnboundedServiceDoesNotHang ./pipeline/runtime/ 2>&1 | head -100 </tool_call>
Assistant
Let me try running the test again with the proper environment setup.
Assistant
The test files are not being recognized. Let me check if the test files exist and have the right package:
<tool_call> <function=bash_terminal> <parameter=command> ls -la pipeline/runtime/*_test.go | head -5 </tool_call>
Assistant
Let me check the test files in the pipeline/runtime directory.
Assistant
The test files exist. Let me try a different approach to run the tests:
Assistant
Let me check what's wrong with the test files by looking at their package declaration:
Assistant
Ah! The test files have a build tag
//go:build test. This means I need to run tests with thetestbuild tag:Assistant
Good! The test passes. This confirms that the workflow doesn't hang anymore (as fixed by commit 60df1c618). Now let me run the test for service steps to see if they properly report their state:
Assistant
The test passes! This means the current implementation correctly reports the service step state. But wait, the issue description says that services are shown as "running" even after the pipeline completes. Let me look at the actual issue more carefully.
Looking at the output, I notice that for the service step "db", the workflow is:
- "start step db"
- "wait for step db" (this blocks because it's an unbounded service)
- "stop step db" (when DestroyWorkflow is called)
- "complete"
So the service step does get a "complete" trace. But looking at the issue description again, it mentions that the pods are destroyed properly but the API still shows the service as "running". This suggests the issue might be in the state reporting, not the workflow execution.
Let me check if the issue is that when the service step completes after being stopped, the
Exitedfield is not set totrue:Assistant
Let me create a test to check if the service step's final state has Exited=true:
Assistant
Let me examine the test to see if it checks for Exited=true on the service step:
Assistant
The test does check that the service step (db) has
Exited=true. So the test passes, which means the service step should be properly marked as exited. Let me check if there's an issue with how thecompleteStepfunction handles context cancellation for service steps.Looking back at the code in
pipeline/runtime/step.go:goif err != nil { if errors.Is(err, context.Canceled) { if waitState == nil { waitState = &backend_types.State{} } waitState.Error = pipeline_errors.ErrCancel } else { return nil, err } }When
WaitStepreturns due to context cancellation, it setswaitState.Errorbut does NOT setwaitState.Exited = true. This is the bug!Let me verify this by looking at what happens in the dummy backend when a service is destroyed:
Assistant
Let me continue reading the dummy backend's WaitStep for service steps:
Assistant
Let me check what
canceledState()returns:Assistant
Now I understand! The
canceledState()function returnsExited: true, which is correct. And for service steps in the dummy backend, when the context is closed, it goes to line 191 (sleepWithContext(ctx, wc, testServiceTimeout)) which will returncanceled: false(not true), so it won't returncanceledState(). Instead, it continues to line 203 where it setsstepStateDoneand returns a state withExited: true.So the dummy backend works correctly. The issue must be with the Kubernetes backend, which doesn't return a state when the context is canceled. Let me check the Kubernetes backend's WaitStep again:
Assistant
Now I found the issue! When the context is done (
case <-ctx.Done()), the Kubernetes backend returnsnil, ctx.Err()without returning a proper state. This means theWaitStepreturnsnilfor the state, and thecompleteStepfunction in the runtime handles this by creating an empty state with just the error set, but without settingExited: true.Let me look at the completeStep function again to see how it handles this:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '150,165p' pipeline/runtime/step.go </tool_call>
Assistant
Let me view the completeStep function to see how it handles context cancellation.
Assistant
Now I can see the issue clearly! When
WaitStepreturns due to context cancellation:waitStateis set to an empty statewaitState.Erroris set toErrCancel- But
waitState.Exitedis never set totrue
This is the bug. The
completeStepfunction should setwaitState.Exited = truewhen the context is canceled, regardless of whether the backend returned a state or not.However, looking at the code again, I also see that the issue might be different. When
WaitStepreturnsnil, ctx.Err(), the code creates an empty state but doesn't setExited: true. But the service pod is then destroyed byDestroyStep, which means the service has effectively stopped.Assistant
Let me use the edit command to make the fix:
Assistant
Now I'll use the edit command to add
waitState.Exited = trueafter the first occurrence:Assistant
I need to open the file first:
Assistant
Now let me edit the file to add
waitState.Exited = true:<tool_call> <function=bash_terminal> <parameter=command> edit 161:161 << 'EOF' waitState.Error = pipeline_errors.ErrCancel waitState.Exited = true EOF </tool_call>
Assistant
I need to use the correct syntax for the edit command. Let me try again with the proper format.
Assistant
Good! The edit was successful. Now let me verify the change and also check if there's a similar issue in the second place where
waitState.Erroris set:Assistant
This is a second place where context cancellation is handled, but it's after
DestroyStephas been called, and the state has already been set. However, this code path only runs ifWaitStepsucceeded and returned a state. In this case, the state should already haveExitedset from the backend. Let me think about this...Actually, looking at the flow more carefully:
- First check (line 156-166):
WaitStepreturns an error - this is when the backend didn't get to return a state (e.g., context canceled before pod reached terminal state) - Second check (line 177-179):
WaitStepsucceeded but the workflow context was already canceled - in this case, already has the state from
- First check (line 156-166):
Assistant
The test passes. Now let me run the test for unbounded services to make sure that still works:
Assistant
Now let me run all the runtime tests to ensure the fix doesn't break anything:
Assistant
Assistant
Let me verify the fix by checking the current state of the file and then run more comprehensive tests to ensure the fix works correctly:
Assistant
The fix is in place. Now let me run the tests in the server directory to ensure this doesn't break anything:
<tool_call> <function=bash_terminal> <parameter=command> cd /woodpecker && go test -v -tags test ./server/pipeline/ 2>&1 | tail -50 </tool_call>
Assistant
Let me run the server pipeline tests to ensure my fix doesn't break anything:
Assistant
The fix is complete. Let me provide a summary and submit the solution.
Summary
The issue was that Woodpecker service steps remained in a "running" state even after the pods were destroyed. This happened because:
- Service steps are marked as
Detached: trueand run in the background until the workflow completes - When the workflow completes,
DestroyWorkflowis called, which destroys all service pods - The
WaitStepfunction in the Kubernetes backend waits for the pod to reach a terminal state (Succeeded/Failed/Unknown), but if the context is cancelled before this happens, it returnsnil, ctx.Err()without a proper state - The
completeStepfunction in the runtime handled this by creating an empty state withErrorset butExitednot set totrue, leaving the step in a "running" state
Fix
- Service steps are marked as
Assistant
Let me verify the final state of the changes before submitting:
Assistant
I'll submit the fix now.
<tool_call> <function=bash_terminal> <parameter=command> submit </tool_call>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.