diff --git a/backends/rapidpro/contact.go b/backends/rapidpro/contact.go index af60a45ae..8adc01f46 100644 --- a/backends/rapidpro/contact.go +++ b/backends/rapidpro/contact.go @@ -104,6 +104,13 @@ func contactForURN(db *sqlx.DB, org OrgID, channelID courier.ChannelID, urn cour return nil, err } + // if the returned URN is for a different contact, then we were in a race as well, rollback and start over + if contactURN.ContactID.Int64 != contact.ID.Int64 { + tx.Rollback() + return contactForURN(db, org, channelID, urn, name) + } + + // all is well, we created the new contact, commit and move forward err = tx.Commit() if err != nil { return nil, err