From 3e8687e46467fe276e4c3fa52f742f472492139b Mon Sep 17 00:00:00 2001 From: adbenitez Date: Fri, 10 Feb 2023 14:43:32 -0500 Subject: [PATCH 1/2] capture unexpected exceptions in EventThread --- python/src/deltachat/events.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/python/src/deltachat/events.py b/python/src/deltachat/events.py index 6db3de61e..975d4e451 100644 --- a/python/src/deltachat/events.py +++ b/python/src/deltachat/events.py @@ -247,17 +247,18 @@ class EventThread(threading.Thread): def run(self) -> None: """get and run events until shutdown.""" with self.log_execution("EVENT THREAD"): - self._inner_run() + event_emitter = ffi.gc( + lib.dc_get_event_emitter(self.account._dc_context), + lib.dc_event_emitter_unref, + ) + while not self._marked_for_shutdown: + with self.swallow_and_log_exception("Unexpected error in event thread"): + event = lib.dc_get_next_event(event_emitter) + if event == ffi.NULL or self._marked_for_shutdown: + break + self._process_event(event) - def _inner_run(self): - event_emitter = ffi.gc( - lib.dc_get_event_emitter(self.account._dc_context), - lib.dc_event_emitter_unref, - ) - while not self._marked_for_shutdown: - event = lib.dc_get_next_event(event_emitter) - if event == ffi.NULL or self._marked_for_shutdown: - break + def _process_event(self, event) -> None: 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 From 6a30c0a997ec90e46a2be28521e7b4b02635dca8 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 11 Feb 2023 09:21:25 +0000 Subject: [PATCH 2/2] Fix code style with `black` --- python/src/deltachat/events.py | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/python/src/deltachat/events.py b/python/src/deltachat/events.py index 975d4e451..9f26af618 100644 --- a/python/src/deltachat/events.py +++ b/python/src/deltachat/events.py @@ -259,25 +259,25 @@ class EventThread(threading.Thread): self._process_event(event) def _process_event(self, event) -> None: - 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 - # function which provides us signature info of an event call - evt_name = get_dc_event_name(evt) - if lib.dc_event_has_string_data(evt): - data2 = from_optional_dc_charpointer(lib.dc_event_get_data2_str(event)) - else: - data2 = lib.dc_event_get_data2_int(event) + 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 + # function which provides us signature info of an event call + evt_name = get_dc_event_name(evt) + if lib.dc_event_has_string_data(evt): + data2 = from_optional_dc_charpointer(lib.dc_event_get_data2_str(event)) + else: + data2 = lib.dc_event_get_data2_int(event) - lib.dc_event_unref(event) - ffi_event = FFIEvent(name=evt_name, data1=data1, data2=data2) - with self.swallow_and_log_exception(f"ac_process_ffi_event {ffi_event}"): - self.account._pm.hook.ac_process_ffi_event(account=self, ffi_event=ffi_event) - for name, kwargs in self._map_ffi_event(ffi_event): - hook = getattr(self.account._pm.hook, name) - info = f"call {name} kwargs={kwargs} failed" - with self.swallow_and_log_exception(info): - hook(**kwargs) + lib.dc_event_unref(event) + ffi_event = FFIEvent(name=evt_name, data1=data1, data2=data2) + with self.swallow_and_log_exception(f"ac_process_ffi_event {ffi_event}"): + self.account._pm.hook.ac_process_ffi_event(account=self, ffi_event=ffi_event) + for name, kwargs in self._map_ffi_event(ffi_event): + hook = getattr(self.account._pm.hook, name) + info = f"call {name} kwargs={kwargs} failed" + with self.swallow_and_log_exception(info): + hook(**kwargs) @contextmanager def swallow_and_log_exception(self, info):