Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions forks/woodpecker/pipeline/runtime/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"strconv"
"sync"
"time"

backend_types "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/types"
Expand Down Expand Up @@ -111,9 +110,9 @@ func (r *Runtime) setStepEnv(step *backend_types.Step) error {

// startStep starts the step container and spawns a goroutine to stream its logs.
// It returns:
// - waitForLogs: must be called before WaitStep — it blocks until the log stream
// is fully drained. Some backends (e.g. local) close the log stream when
// WaitStep is called, so draining first is required.
// - waitForLogs: must be called before WaitStep. It blocks until the log stream
// is fully drained, or until the workflow context is done. Some backends (e.g.
// local) close the log stream when WaitStep is called, so draining first is required.
// - startTime: unix timestamp recorded right after the container started, used
// later to fill waitState.Started.
//
Expand All @@ -130,16 +129,25 @@ func (r *Runtime) startStep(step *backend_types.Step) (func(), int64, error) {
return nil, 0, err
}

var wg sync.WaitGroup
wg.Go(func() {
done := make(chan struct{})
go func() {
defer close(done)
logger := r.makeLogger()
if err := r.logger(step, rc); err != nil {
logger.Error().Err(err).Str("step", step.Name).Msg("step log streaming failed")
}
// The close error cannot change what was already logged, so it is not handled.
_ = rc.Close()
})
}()

return wg.Wait, startTime, nil
// A stalled log stream must not hold the step past the workflow deadline.
waitForLogs := func() {
select {
case <-done:
case <-r.ctx.Done():
}
}
return waitForLogs, startTime, nil
}

// cancelFallout reports whether err is a consequence of the workflow being
Expand Down
150 changes: 150 additions & 0 deletions forks/woodpecker/pipeline/runtime/step_deadline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2026 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build test

package runtime

import (
"context"
"errors"
"io"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/dummy"
backend_types "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/types"
pipeline_errors "go.woodpecker-ci.org/woodpecker/v3/pipeline/errors"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/logging"
)

const workflowDeadline = 200 * time.Millisecond

// waitUntilDone models a step that never exits on its own: WaitStep returns
// only when its context is done, as the backend contract requires.
func waitUntilDone(ctx context.Context) (*backend_types.State, error) {
<-ctx.Done()
return nil, ctx.Err()
}

// stalledStreamBackend tails a log stream that stays open until released,
// even after the workflow context is done.
type stalledStreamBackend struct {
backend_types.Backend
release <-chan struct{}
}

func (b *stalledStreamBackend) TailStep(_ context.Context, _ *backend_types.Step, _ string) (io.ReadCloser, error) {
return stalledStream{release: b.release}, nil
}

func (b *stalledStreamBackend) WaitStep(ctx context.Context, _ *backend_types.Step, _ string) (*backend_types.State, error) {
return waitUntilDone(ctx)
}

type stalledStream struct {
release <-chan struct{}
}

func (s stalledStream) Read(_ []byte) (int, error) {
<-s.release
return 0, io.EOF
}

func (s stalledStream) Close() error { return nil }

// honoringWaitBackend runs the step on the dummy backend, but WaitStep only
// returns when its context is done.
type honoringWaitBackend struct {
backend_types.Backend
}

func (b *honoringWaitBackend) WaitStep(ctx context.Context, _ *backend_types.Step, _ string) (*backend_types.State, error) {
return waitUntilDone(ctx)
}

// assertFinishesAtDeadline runs a workflow whose step never exits and asserts
// that Run returns near the workflow deadline, not when a blocked call is released.
func assertFinishesAtDeadline(t *testing.T, engine backend_types.Backend, logger logging.Logger) {
t.Helper()

ctx, cancel := context.WithTimeout(t.Context(), workflowDeadline)
t.Cleanup(cancel)

r := New(
&backend_types.Config{
Stages: []*backend_types.Stage{
{Steps: []*backend_types.Step{cmdStep("hang")}},
},
},
engine,
WithTracer(newTestTracer(t)),
WithLogger(logger),
WithContext(ctx),
)

done := make(chan error, 1)
start := time.Now()
go func() { done <- r.Run(t.Context()) }()

select {
case err := <-done:
require.ErrorIs(t, err, pipeline_errors.ErrCancel,
"a workflow that hits its deadline must report the cancellation")
assert.Less(t, time.Since(start), workflowDeadline+2*time.Second,
"the workflow must finish near its deadline")
case <-time.After(workflowDeadline + 5*time.Second):
t.Fatal("workflow hung past its deadline: a blocked call was not bounded by the workflow context")
}
}

// (i) The log stream stays open past the workflow deadline. The drain must not
// hold the workflow.
func TestWorkflowDeadlineBoundsStalledLogStream(t *testing.T) {
t.Parallel()

release := make(chan struct{})
t.Cleanup(func() { close(release) })

engine := &stalledStreamBackend{Backend: dummy.New(), release: release}
drain := func(_ *backend_types.Step, rc io.ReadCloser) error {
_, err := io.Copy(io.Discard, rc)
return errors.Join(err, rc.Close())
}

assertFinishesAtDeadline(t, engine, drain)
}

// (ii) The log sender blocks after one line, as an rpc EnqueueLog does when its
// buffer is full. The step must still end at the workflow deadline.
func TestWorkflowDeadlineBoundsBlockedLogSend(t *testing.T) {
t.Parallel()

release := make(chan struct{})
t.Cleanup(func() { close(release) })

engine := &honoringWaitBackend{Backend: dummy.New()}
blockedSend := func(_ *backend_types.Step, rc io.ReadCloser) error {
if _, err := io.ReadFull(rc, make([]byte, 1)); err != nil {
return err
}
<-release
return rc.Close()
}

assertFinishesAtDeadline(t, engine, blockedSend)
}
Loading