Commit bb1ba3b2 authored by Colomban Wendling's avatar Colomban Wendling

Mail thread: only report a task done when we actually handled a task

Don't account for a task in the `finally` handler as we actually got a
task only if we didn't receive an exception.  This cleans up the code
and possibly fixes it if we ever get any other exception than a
timeout.
parent 8cf30efb
...@@ -46,12 +46,10 @@ class ThreadedSMTP(object): ...@@ -46,12 +46,10 @@ class ThreadedSMTP(object):
server = None server = None
while self._loop or not self._queue.empty(): while self._loop or not self._queue.empty():
task_eaten = True
try: try:
args, kwargs = self._queue.get(timeout=timeout) args, kwargs = self._queue.get(timeout=timeout)
except queue.Empty: except queue.Empty:
server = self.__server_quit(server) server = self.__server_quit(server)
task_eaten = False # we didn't eat a task, just timeout
else: else:
if len(args) or len(kwargs): # ignore empty items if len(args) or len(kwargs): # ignore empty items
try: try:
...@@ -60,9 +58,7 @@ class ThreadedSMTP(object): ...@@ -60,9 +58,7 @@ class ThreadedSMTP(object):
server.sendmail(*args, **kwargs) server.sendmail(*args, **kwargs)
except Exception as e: except Exception as e:
logging.warning("Couldn't send email: %s" % str(e)) logging.warning("Couldn't send email: %s" % str(e))
finally: self._queue.task_done()
if task_eaten:
self._queue.task_done()
self.__server_quit(server) self.__server_quit(server)
def sendmail(self, *args, **kwargs): def sendmail(self, *args, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment