From 0c9106717be8ba3899aa9f679476b4cf77552bcd Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Wed, 9 Sep 2020 17:00:07 +0200 Subject: [PATCH] handle case where contact is concurrently created --- email_handler.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/email_handler.py b/email_handler.py index af4d4869..2f5aaca1 100644 --- a/email_handler.py +++ b/email_handler.py @@ -241,15 +241,20 @@ def replace_header_when_forward(msg: Message, alias: Alias, header: str): reply_email = generate_reply_email() - contact = Contact.create( - user_id=alias.user_id, - alias_id=alias.id, - website_email=contact_email, - name=contact_name, - reply_email=reply_email, - is_cc=header.lower() == "cc", - ) - db.session.commit() + try: + contact = Contact.create( + user_id=alias.user_id, + alias_id=alias.id, + website_email=contact_email, + name=contact_name, + reply_email=reply_email, + is_cc=header.lower() == "cc", + ) + db.session.commit() + except IntegrityError: + LOG.warning("Contact %s %s already exist", alias, contact_email) + db.session.rollback() + contact = Contact.get_by(alias_id=alias.id, website_email=contact_email) new_addrs.append(contact.new_addr())