Skip to content
Open
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
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type builder struct {
resultChan chan blockResult
workerGroup *errgroup.Group
workerCtx context.Context
workerCancel context.CancelFunc // Cancels workerCtx to unblock stuck workers
workerCancel context.CancelCauseFunc // Cancels workerCtx to unblock stuck workers
writerDone chan error
writerErr error
workersShutDown atomic.Bool // True once the worker pipeline has been torn down
Expand Down
21 changes: 16 additions & 5 deletions builder_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (b *builder) startPipeline(worker func() error) {
}

// Wrap in explicit cancel so shutdownWorkers can unblock workers stuck on resultChan.
ctx, cancel := context.WithCancel(b.ctx)
ctx, cancel := context.WithCancelCause(b.ctx)
b.workerCancel = cancel
b.workerGroup, b.workerCtx = errgroup.WithContext(ctx)
for range b.workers {
Expand Down Expand Up @@ -151,7 +151,7 @@ func (b *builder) dispatchBlockWork(blockID uint32, entries []routedEntry, poole
case b.workChan <- work:
return nil
case <-b.workerCtx.Done():
return b.workerCtx.Err()
return b.pipelineErr()
case err := <-b.writerDone:
// A nil read here means writerDone was closed (pipeline torn down); do
// not drop this block — consumePipelineError surfaces the real error.
Expand All @@ -165,6 +165,15 @@ func (b *builder) dispatchEmptyBlock(blockID uint32) error {
return b.dispatchBlockWork(blockID, nil, false)
}

// pipelineErr returns why workerCtx is done: the build context's error if the
// build was canceled, otherwise the error of the worker or writer that failed.
func (b *builder) pipelineErr() error {
if err := b.ctx.Err(); err != nil {
return err
}
return context.Cause(b.workerCtx)
}

// blockSolver is a worker's block builder, with the payload buffer it reuses
// across blocks.
type blockSolver struct {
Expand Down Expand Up @@ -293,10 +302,12 @@ func (b *builder) runWorker() error {
// drainParallelPipeline's workerGroup.Wait — would hang forever. Combined with
// runWorker returning buildErr (which cancels via the errgroup), this guarantees
// the invariant: ANY internal failure cancels b.workerCtx exactly once.
// workerCancel is idempotent (also called by shutdownWorkers).
// workerCancel is idempotent (also called by shutdownWorkers). err is its
// cause, so the context carries the failure even when this cancel lands
// before a failed worker's own.
func (b *builder) failWriter(err error) {
b.writerDone <- err
b.workerCancel()
b.workerCancel(err)
}

func (b *builder) runWriter() {
Expand Down Expand Up @@ -425,7 +436,7 @@ func (b *builder) shutdownWorkers() {
}
b.workersShutDown.Store(true)
if b.workerCancel != nil {
b.workerCancel()
b.workerCancel(nil)
}
close(b.workChan)
_ = b.workerGroup.Wait()
Expand Down
2 changes: 1 addition & 1 deletion builder_unsorted_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (ub *UnsortedBuilder) finishUnsortedParallel() error {
case b.workChan <- work:
case <-b.workerCtx.Done():
slotFences[slotIdx].Done()
return fail(b.workerCtx.Err())
return fail(b.pipelineErr())
case err := <-b.writerDone:
b.writerErr = err
slotFences[slotIdx].Done()
Expand Down
11 changes: 2 additions & 9 deletions parallel_pipeline_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type pipeScenario uint8

const (
scenValid pipeScenario = iota // well-formed input: build must succeed + index is a bijection
scenDuplicate // a duplicate key: build must fail, not hang
scenDuplicate // a duplicate key: build must fail with ErrDuplicateKey, not hang
scenCancel // ctx cancelled mid-build: build must return, not hang
scenCount
)
Expand Down Expand Up @@ -209,14 +209,7 @@ func runPipelineScenario(t *testing.T, cfg pipeConfig) {
assertBijection(t, indexPath, keys)

case scenDuplicate:
if buildErr == nil {
t.Fatalf("%s: expected a duplicate-key error, got nil", cfg)
}
// The precise cause is ErrDuplicateKey; in some interleavings the
// pipeline surfaces a context cancellation from its own teardown first.
// Both mean the build failed cleanly; only nil or an unrelated error is
// a bug.
if !errors.Is(buildErr, sherr.ErrDuplicateKey) && !errors.Is(buildErr, context.Canceled) {
if !errors.Is(buildErr, sherr.ErrDuplicateKey) {
t.Fatalf("%s: expected ErrDuplicateKey, got: %v", cfg, buildErr)
}

Expand Down
Loading