diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 6505e259e..5c158c7e6 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -1543,6 +1543,10 @@ func (i *InvoiceRegistry) SubscribeNotifications( } }() + i.Lock() + i.notificationClients[client.id] = client + i.Unlock() + // Query the database to see if based on the provided addIndex and // settledIndex we need to deliver any backlog notifications. err := i.deliverBacklogEvents(client) @@ -1552,12 +1556,6 @@ func (i *InvoiceRegistry) SubscribeNotifications( log.Infof("New invoice subscription client: id=%v", client.id) - i.Lock() - // With the backlog notifications delivered (if any), we'll add this to - // our active subscriptions. - i.notificationClients[client.id] = client - i.Unlock() - return client, nil } @@ -1617,6 +1615,10 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice( } }() + i.Lock() + i.singleNotificationClients[client.id] = client + i.Unlock() + err := i.deliverSingleBacklogEvents(client) if err != nil { return nil, err @@ -1625,10 +1627,6 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice( log.Infof("New single invoice subscription client: id=%v, ref=%v", client.id, client.invoiceRef) - i.Lock() - i.singleNotificationClients[client.id] = client - i.Unlock() - return client, nil }