From 506e397e4a65cfca95f978ac53a63524d13443ba Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Mon, 22 Sep 2014 17:48:11 +0200 Subject: [PATCH] [IMP] mail: avoid sending an email twice In some specific conditions (e.g. admin logging during cron run, concurrent update), writing on the mail object may fail, triggering a rollback of the transaction. As the write was done after the SMTP sendmail action, an email could be sent twice (state not correctly put, would be retried at next process of the email queue). This patch provoke the error before the sendmail action and thus avoids sending the email. Fixes #552 --- addons/mail/mail_mail.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/addons/mail/mail_mail.py b/addons/mail/mail_mail.py index 3d51853..f474a07 100644 --- a/addons/mail/mail_mail.py +++ b/addons/mail/mail_mail.py @@ -274,6 +274,12 @@ class mail_mail(osv.Model): except Exception: pass + # Writing on the mail object may fail (e.g. lock on user) which + # would trigger a rollback *after* actually sending the email. + # To avoid sending twice the same email, provoke the failure earlier + mail.write({'state': 'exception'}) + mail_sent = False + # build an RFC2822 email.message.Message object and send it without queuing res = None for email in email_list: @@ -299,9 +305,6 @@ class mail_mail(osv.Model): if res: mail.write({'state': 'sent', 'message_id': res}) mail_sent = True - else: - mail.write({'state': 'exception'}) - mail_sent = False # /!\ can't use mail.state here, as mail.refresh() will cause an error # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1 -- 1.7.10.4