From 4390796985a4c2cd52c9b54eb51388cea2c2dfa4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 16 Apr 2026 00:41:39 +0200 Subject: [PATCH] Fix race in pipeline runtime (#6451) as step tracer is also used to update workflow environment variables --- pipeline/runtime/runtime.go | 7 +++++-- pipeline/runtime/step.go | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pipeline/runtime/runtime.go b/pipeline/runtime/runtime.go index 4512edfe9..1cb262ebe 100644 --- a/pipeline/runtime/runtime.go +++ b/pipeline/runtime/runtime.go @@ -16,6 +16,7 @@ package runtime import ( "context" + "sync" "github.com/oklog/ulid/v2" "github.com/rs/zerolog" @@ -42,8 +43,9 @@ type Runtime struct { // Cleanup operations should use the runnerCtx passed to Run(). ctx context.Context - tracer tracing.Tracer - logger logging.Logger + tracer tracing.Tracer + tracerLock sync.Mutex + logger logging.Logger taskUUID string description map[string]string @@ -58,6 +60,7 @@ func New(spec *backend_types.Config, backend backend_types.Backend, opts ...Opti r.engine = backend r.ctx = context.Background() r.taskUUID = ulid.Make().String() + r.tracerLock = sync.Mutex{} for _, opt := range opts { opt(r) } diff --git a/pipeline/runtime/step.go b/pipeline/runtime/step.go index abef6ec6c..ee971dc6e 100644 --- a/pipeline/runtime/step.go +++ b/pipeline/runtime/step.go @@ -253,6 +253,10 @@ func (r *Runtime) traceStep(processState *backend_types.State, err error, step * // processState == nil && err == nil: step just started, leave s.CurrStepState zero-valued. } + // The tracer should just trace changes, but it currently also updates step env vars used in various ways: + // https://github.com/woodpecker-ci/woodpecker/blob/main/agent/tracer.go#L79-L86 . + r.tracerLock.Lock() + defer r.tracerLock.Unlock() if traceErr := r.tracer.Trace(s); traceErr != nil { return traceErr }