From a27b379ce0bdcf88229db74a9eb5096fd672f424 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 15 Jul 2020 16:53:30 +0200 Subject: [PATCH] fix #1720 -- don't wait for the daemon eventhread to terminate but count on it to eventually die --- python/src/deltachat/account.py | 8 ++++++-- python/src/deltachat/direct_imap.py | 4 ++++ python/src/deltachat/events.py | 8 +++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 7b4ac457b..0c1dbd797 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -609,10 +609,14 @@ class Account(object): self.log("remove dc_context references") # the dc_context_unref triggers get_next_event to return ffi.NULL # which in turns makes the event thread finish execution + self._event_thread.mark_shutdown() self._dc_context = None - self.log("wait for event thread to finish") - self._event_thread.wait() + # the following wait is left out for now because it can fail if + # a) python code holds an extra self._dc_context reference + # b) the core does not manage to send a NULL through get_next_event() + # self.log("wait for event thread to finish") + # self._event_thread.wait() self._shutdown_event.set() diff --git a/python/src/deltachat/direct_imap.py b/python/src/deltachat/direct_imap.py index 7e3e5b38d..b53ae72ba 100644 --- a/python/src/deltachat/direct_imap.py +++ b/python/src/deltachat/direct_imap.py @@ -162,6 +162,10 @@ class DirectImap: requested = [b'BODY.PEEK[HEADER]', FLAGS] for uid, data in self.conn.fetch(messages, requested).items(): body_bytes = data[b'BODY[HEADER]'] + if not body_bytes: + log("Message", uid, "has empty body") + continue + flags = data[FLAGS] path = pathlib.Path(str(dir)).joinpath("IMAP", self.logid, imapfolder) path.mkdir(parents=True, exist_ok=True) diff --git a/python/src/deltachat/events.py b/python/src/deltachat/events.py index 05bd56c8e..0c9ae7fe4 100644 --- a/python/src/deltachat/events.py +++ b/python/src/deltachat/events.py @@ -139,6 +139,7 @@ class EventThread(threading.Thread): self.account = account super(EventThread, self).__init__(name="events") self.setDaemon(True) + self._marked_for_shutdown = False self.start() @contextmanager @@ -147,6 +148,9 @@ class EventThread(threading.Thread): yield self.account.log(message + " FINISHED") + def mark_shutdown(self): + self._marked_for_shutdown = True + def wait(self): if self == threading.current_thread(): # we are in the callback thread and thus cannot @@ -164,10 +168,12 @@ class EventThread(threading.Thread): lib.dc_get_event_emitter(self.account._dc_context), lib.dc_event_emitter_unref, ) - while 1: + while not self._marked_for_shutdown: event = lib.dc_get_next_event(event_emitter) if event == ffi.NULL: break + if self._marked_for_shutdown: + break evt = lib.dc_event_get_id(event) data1 = lib.dc_event_get_data1_int(event) # the following code relates to the deltachat/_build.py's helper