diff --git a/gostream/media.go b/gostream/media.go index d5c33cc4a25..d5e34d7dc4e 100644 --- a/gostream/media.go +++ b/gostream/media.go @@ -360,6 +360,10 @@ func (pc *producerConsumer[T, U]) stop() { pc.consumerCond.L.Unlock() pc.activeBackgroundWorkers.Wait() + pc.currentMu.Lock() + defer pc.currentMu.Unlock() + pc.current = nil + // reset cancelCtx, cancel := context.WithCancel(WithMIMETypeHint(pc.rootCancelCtx, pc.mimeType)) pc.cancelCtxMu.Lock() diff --git a/gostream/media_test.go b/gostream/media_test.go index fe781543285..7b6c45fb5e9 100644 --- a/gostream/media_test.go +++ b/gostream/media_test.go @@ -191,3 +191,25 @@ func TestStreamMultipleConsumers(t *testing.T) { test.That(t, wrappedImg.released.Load(), test.ShouldBeTrue) } } + +func TestStreamWithoutNext(t *testing.T) { + colors := []*WrappedImage{createWrappedImage(t, rimage.Red)} + + imgSource := &imageSource{WrappedImages: colors} + videoSrc := NewVideoSource(imgSource, prop.Video{}) + + // Start stream + stream, err := videoSrc.Stream(context.Background()) + test.That(t, err, test.ShouldBeNil) + // Get one frame + _, release, err := stream.Next(context.Background()) + test.That(t, err, test.ShouldBeNil) + release() + // Close stream + stream.Close(context.Background()) + + // Spin up stream and close without calling Next + stream, err = videoSrc.Stream(context.Background()) + test.That(t, err, test.ShouldBeNil) + stream.Close(context.Background()) +}