From ca612ce669544e87230569a982696acaff44ddf0 Mon Sep 17 00:00:00 2001 From: Keenan Nemetz Date: Wed, 5 Jun 2024 11:45:49 -0700 Subject: [PATCH] close subscription if publisher channel is closed --- internal/db/subscriptions.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/db/subscriptions.go b/internal/db/subscriptions.go index b1e2820830..a1b0147df4 100644 --- a/internal/db/subscriptions.go +++ b/internal/db/subscriptions.go @@ -49,10 +49,13 @@ func (db *db) handleSubscription(ctx context.Context, r *request.Request) (<-cha for { var evt events.Update select { - case val := <-pub.Event(): - evt = val case <-ctx.Done(): return // context cancelled + case val, ok := <-pub.Event(): + if !ok { + return // channel closed + } + evt = val } txn, err := db.NewTxn(ctx, false) @@ -80,11 +83,11 @@ func (db *db) handleSubscription(ctx context.Context, r *request.Request) (<-cha } select { - case resCh <- res: - txn.Discard(ctx) case <-ctx.Done(): txn.Discard(ctx) return // context cancelled + case resCh <- res: + txn.Discard(ctx) } } }()