From 468752564125e79b5efdfff091d8886dc23e373a Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 9 Sep 2021 10:42:13 +0200 Subject: [PATCH] webhooks/legacy: fetch subscriptions upfront This fixes a race condition when delivering webhooks for resource deletion events, in which the subscriptions would be removed from the delete cascade before the task to fetch them executes. This requires the downstream code to call Queue before committing the delete transaction, and updates the API to include a context argument for the connection pool. --- webhooks/legacy.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/webhooks/legacy.go b/webhooks/legacy.go index 8c1a5f3fa0957190c233870a52e56e91a276e196..a037c7e8a1864821a58697ed2a5d43a396ad767f 100644 --- a/webhooks/legacy.go +++ b/webhooks/legacy.go @@ -49,7 +49,7 @@ func NewLegacyQueue() *LegacyQueue { // // Name shall be the prefix of the webhook tables, e.g. "user" for // "user_webhook_{delivery,subscription}". -func (lq *LegacyQueue) Schedule(q sq.SelectBuilder, +func (lq *LegacyQueue) Schedule(ctx context.Context, q sq.SelectBuilder, name, event string, payload []byte) { // The following tasks are done during this process: // @@ -59,16 +59,16 @@ func (lq *LegacyQueue) Schedule(q sq.SelectBuilder, // // The first two steps are done in this task, then N tasks are created for // step 3 where N = number of subscriptions. - task := work.NewTask(func(ctx context.Context) error { - subs, err := fetchSubscriptions(ctx, q, event) - if err != nil { - return err - } + subs, err := fetchSubscriptions(ctx, q, event) + if err != nil { + panic(err) + } - if len(subs) == 0 { - return nil - } + if len(subs) == 0 { + return + } + task := work.NewTask(func(ctx context.Context) error { tasks := make([]*work.Task, len(subs)) if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { var err error