Skip to content

fix: stop middleware re-wrapping, and turn a branch panic into an error - #6

Merged
veggiemonk merged 1 commit into
mainfrom
fix/v1-defects
Sep 21, 2026
Merged

veggiemonk merged 1 commit into
mainfrom
fix/v1-defects

Conversation

@veggiemonk

Copy link
Copy Markdown
Owner

Patches v1 in place. No exported signature changed, so this is a patch release, not a breaking one.

Each defect was reproduced against the current release before the fix, and each now has a regression test.

1. Middleware collected a new layer on every run — fixed

Pipeline.Run and series.Run wrote the wrapped step back into their own slice while they ran (workflow.go:46, workflow.go:281):

run 1 middleware invocations: 1
run 2 middleware invocations: 2
run 3 middleware invocations: 3

A Retry(3) became 9 attempts on the second run. Two concurrent runs also raced on the slice.

Middleware is now applied to a local copy, for the length of the call only, through one small unexported helper shared by Pipeline, series, selector and parallel:

func apply[T any](s Step[T], mid []Middleware[T]) Step[T] {
	for _, m := range slices.Backward(mid) {
		s = m(s)
	}
	return s
}

Tests: TestPipelineDoesNotReapplyMiddleware, TestSequentialDoesNotReapplyMiddleware, TestRunDoesNotMutateThePipeline, TestConcurrentRunsAreSafe (under -race).

One visible behaviour change

String() now prints the pipeline as you declared it. It used to show the middleware wrappers that a previous Run had left behind:

 Pipeline[Result]
-├── Logger(StepFunc[workflow_test.Result])
-├── Logger(series[Result]
+├── StepFunc[workflow_test.Result]
+├── series[Result]

Two existing expectations asserted the wrapped form. They had encoded the bug, so I updated them. The middleware itself still runs exactly as before — the log output in Example is unchanged.

2. Merge drops a branch — documented, not repaired

Merge keeps the first branch; it does not add the branches together. mergo writes into a field only while that field is still empty, and every branch of a Parallel starts from a copy of the same request. So the first branch fills the field and every later branch is dropped in silence:

wf.Parallel(nil, wf.Merge[T], inc, inc) // Counter == 1, not 2

I could not fix this, and I do not think a patch can. It follows from the shape of Step[T]: a step reads a T and returns a T, so every branch returns the whole struct and nothing says which field a branch owns. Any "fix" would just pick a different branch to lose.

What this PR does instead:

  • Documents it on Merge, MergeTransform and Parallel, with the counter example spelled out.
  • Adds a Merging parallel results section to the README, right after the usage example — which is itself an instance of the bug, since both of its parallel tasks run r.State.Counter++.
  • Adds the same warning to docs/llms.md.
  • Pins the real behaviour with TestMergeDoesNotCombineValues, so nobody discovers it in production. The same test shows a correct MergeRequest of your own producing 2.

Parallel already takes a user-supplied MergeRequest, so the escape hatch exists. The defect was that the default silently loses data and the README taught it.

3. A branch panic crashed the caller — fixed

CapturePanic logged the panic and swallowed it. The goroutine returned nil, errgroup saw success, the branch result stayed nil, and mergo then panicked on the nil pointer:

panic: reflect: call of reflect.Value.Type on zero Value

One panic became two, and the second was not recovered.

Parallel now returns a PanicError:

type PanicError struct {
	Step  string
	Value any
	Stack []byte
}

Merge and MergeTransform also skip a nil response rather than dereference it, which closes the second half of the crash.

CapturePanic is kept for compatibility and marked deprecated: it leaves the caller with a zero result and no error, which is almost never wanted.

Tests: TestParallelPanicBecomesAnError, TestMergeSkipsANilResponse.

Verification

go test -race -count=1 -shuffle=on ./...   ok
go vet ./...                               clean
modernize -fix -diff ./...                 prints nothing
go fix -diff ./...                         prints nothing
gofmt -l .                                 prints nothing
golangci-lint run                          0 issues

Merge order

The new README section links to ./v2, which arrives in #5. Merge #5 first, or the link is dead until it lands.

🤖 Generated with Claude Code

Three defects, each reproduced against the current release before the fix
and each now covered by a regression test.

1. Pipeline.Run and series.Run wrote the wrapped step back into their own
   step slice while they ran (workflow.go:46, workflow.go:281). Every run
   added another layer, so one middleware fired 1, then 2, then 3 times, and
   a Retry(3) became 9 attempts on the second run. Two concurrent runs also
   raced on the slice. Middleware is now applied to a local copy through a
   new unexported apply helper, for the length of the call only.

   This also means String() prints the pipeline as it was declared. Two test
   expectations that showed the middleware left behind by a previous Run
   were updated; they had encoded the bug.

2. Merge keeps the first branch; it does not add the branches together.
   mergo writes into a field only while that field is still empty, and every
   branch of a Parallel starts from a copy of the same request, so the first
   branch wins and the rest are dropped without a word.

   This one is NOT repaired. It follows from Step[T] reading and returning
   the same type: every branch returns the whole struct and nothing says
   which field a branch owns. It is now documented on Merge, MergeTransform
   and Parallel, in the README and in docs/llms.md, and pinned by
   TestMergeDoesNotCombineValues. Callers should pass their own MergeRequest.
   workflow/v2 removes the problem at the type level.

3. A panic in a parallel branch was logged by CapturePanic and swallowed.
   The goroutine returned nil, errgroup saw success, the branch result
   stayed nil, and mergo then panicked on the nil pointer, stopping the
   program. Parallel now returns a PanicError. Merge and MergeTransform also
   skip a nil response rather than dereference it.

CapturePanic is deprecated: it leaves the caller with a zero result and no
error.

No exported signature changed. Clean under -race, -shuffle=on, go vet,
modernize, go fix and golangci-lint.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@veggiemonk
veggiemonk merged commit 93578f9 into main Sep 21, 2026
1 check passed
@veggiemonk
veggiemonk deleted the fix/v1-defects branch September 21, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant