diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/pins_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/pins_test.go index b6a330b8..bee58513 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/pins_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/pins_test.go @@ -1,6 +1,7 @@ package pipeline import ( + "errors" "testing" catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" @@ -179,3 +180,44 @@ func TestPlacingAWaitingPipelineAsksNoRunningStepAgain(t *testing.T) { t.Errorf("an advance placed %d (%v) and asked %d steps again, want nothing", n, err, len(asks.asks)-before) } } + +// A-449: the pipeline-ended's widens read the pipeline's revision while it is still pinned; the pin is freed after the end is +// said, also when saying it fails. +func TestThePinIsFreedAfterTheEndAndItsWidensAreRead(t *testing.T) { + h, store, _, _, _, _ := fixture() + p := store[pkey] + p.p.Revision = 12 + store[pkey] = p + freed, readWhileFreed, bodies := false, false, 0 + h.Say = func(subject string, body any) error { + switch body.(type) { + case delivery.Pins: + freed = freed || subject == delivery.SubjectFreed + default: + bodies++ + } + return nil + } + h.Widens = func(seat.Pipeline) []seat.Widening { + readWhileFreed = freed // the revision is gone once the freed is said + return nil + } + if err := h.said(store[pkey].p, false, true); err != nil { + t.Fatal(err) + } + if readWhileFreed || !freed || bodies == 0 { + t.Errorf("widens read after the freed %v, freed %v, %d other events: want the widens read first and the pin freed after the end", readWhileFreed, freed, bodies) + } + // An end that cannot be said still frees the pin. + freed = false + h.Say = func(subject string, body any) error { + if _, ok := body.(delivery.Pins); ok { + freed = freed || subject == delivery.SubjectFreed + return nil + } + return errors.New("bus away") + } + if err := h.said(store[pkey].p, false, true); err == nil || !freed { + t.Errorf("an end not said: err %v, freed %v, want the error and the pin freed", err, freed) + } +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index 43d3781d..a1c7a3c2 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -225,7 +225,9 @@ func (h *Hearer) said(p seat.Pipeline, asked, ended bool) error { } } if ended { - h.free(p) // the revision it ran on is no longer kept for it (A-449) + // The end is said first, with its widens, which read the revision the pipeline pinned; only then is the pin freed (A-449), + // and also when saying the end fails. + defer h.free(p) if err := h.say(seat.EventPipelineEnded, p, nil); err != nil { return fmt.Errorf("pipeline %s ended but %s is not said: %w", p.Pipeline, seat.EventPipelineEnded, err) }