From 4bb611591619819b3300aecafb3b90389fc7bc13 Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:59:10 -1000 Subject: [PATCH] Close pubsub topic when broadcaster closes Closes #227 --- pubsub_broadcaster.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pubsub_broadcaster.go b/pubsub_broadcaster.go index 02a23a6..590905d 100644 --- a/pubsub_broadcaster.go +++ b/pubsub_broadcaster.go @@ -39,6 +39,13 @@ func NewPubSubBroadcaster(ctx context.Context, psub *pubsub.PubSub, topic string go func(ctx context.Context, subs *pubsub.Subscription) { <-ctx.Done() subs.Cancel() + // subs.Next returns error when subscription closed. Subscription must + // be closed before psubTopic can be closed. + var err error + for err == nil { + _, err = subs.Next(ctx) + } + psubTopic.Close() }(ctx, subs) return &PubSubBroadcaster{