diff --git a/internal/web/service/inbound_client_ips.go b/internal/web/service/inbound_client_ips.go index 8114ed2df..42e5082f0 100644 --- a/internal/web/service/inbound_client_ips.go +++ b/internal/web/service/inbound_client_ips.go @@ -160,7 +160,7 @@ func mergeInboundClientIps(db *gorm.DB, reporter *model.Node, incomingIps []mode // check_client_ip_job rewrites this blob too; an unconditional Update loses // whichever writer commits second and its remote IPs with it (#6587). - err := mergeExistingClientIps(tx, current, incomingEntries, cutoff) + err := mergeExistingClientIps(tx, current, incomingEntries, cutoff, reporter != nil) // The observation belongs to the email in the report. A concurrent // delete or rename can remove it while the poll waits for the row. if reporter != nil && errors.Is(err, gorm.ErrRecordNotFound) { @@ -199,8 +199,12 @@ func CasUpdateInboundClientIps(tx *gorm.DB, id int, expectedIps, newIps string) } // Seed the first CAS from the preload; only a competing write requires a re-read. -func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incoming []model.ClientIpEntry, cutoff int64) error { - email := current.ClientEmail +func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incoming []model.ClientIpEntry, cutoff int64, polled bool) error { + if polled { + // Keep the observed email in every CAS attempt even if the ID is + // still present after a concurrent rename. + tx = tx.Where("client_email = ?", current.ClientEmail).Session(&gorm.Session{}) + } for attempt := range ClientIpCasRetries { var oldEntries []model.ClientIpEntry if current.Ips != "" { @@ -211,7 +215,7 @@ func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incomi if current.Ips == mergedStr { return nil } - ok, err := CasUpdateInboundClientIps(tx.Where("client_email = ?", email), current.Id, current.Ips, mergedStr) + ok, err := CasUpdateInboundClientIps(tx, current.Id, current.Ips, mergedStr) if err != nil { return err } @@ -220,7 +224,7 @@ func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incomi return nil } if attempt+1 < ClientIpCasRetries { - if err := tx.Where("id = ? AND client_email = ?", current.Id, email).First(current).Error; err != nil { + if err := tx.Where("id = ?", current.Id).First(current).Error; err != nil { return err } }