From 8bd57a9f0f04d91118e9cee7db86efa3d28bcf32 Mon Sep 17 00:00:00 2001 From: "hongjin.zhou" Date: Fri, 30 Jun 2023 11:57:57 +0200 Subject: [PATCH] Fix endless file reading even with a set stop event Problem: when a dlt file always has incoming dlt messages, the file reading fails to stop even when users set the stop_reading_event. Because we dont check the status of stop_reading_event while reading dlt file returns OK, so the set stop_reading_event wont be perceived Solution: while reading dlt file returns OK, we have another check of the status of stop_reading_event, so that the reading could be stopped accordingly --- dlt/dlt.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/dlt/dlt.py b/dlt/dlt.py index 29fd693..bacb60b 100644 --- a/dlt/dlt.py +++ b/dlt/dlt.py @@ -767,7 +767,7 @@ def __getitem__(self, index): def _open_file(self): """Open the configured file for processing""" file_opened = False - while not self.stop_reading.is_set() and not self.stop_reading_proc.is_set(): + while not self._is_stop_reading_set(): if dltlib.dlt_file_open(ctypes.byref(self), self.filename, self.verbose) >= DLT_RETURN_OK: file_opened = True break @@ -791,6 +791,9 @@ def _log_message_progress(self): self.msg.ctid, ) + def _is_stop_reading_set(self): + return self.stop_reading.is_set() or self.stop_reading_proc.is_set() + def __iter__(self): # pylint: disable=too-many-branches """Iterate over messages in the file""" logger.debug("Starting File Read") @@ -804,9 +807,7 @@ def __iter__(self): # pylint: disable=too-many-branches self._open_file() found_data = False - while ( - not self.stop_reading.is_set() and not self.stop_reading_proc.is_set() - ) or corruption_check_try: # pylint: disable=too-many-nested-blocks + while not self._is_stop_reading_set() or corruption_check_try: # pylint: disable=too-many-nested-blocks os_stat = os.stat(self.filename) mtime = os_stat.st_mtime @@ -814,7 +815,9 @@ def __iter__(self): # pylint: disable=too-many-branches cached_mtime = mtime corruption_check_try = False - while dltlib.dlt_file_read(ctypes.byref(self), self.verbose) >= DLT_RETURN_OK: + while not self._is_stop_reading_set() and ( + dltlib.dlt_file_read(ctypes.byref(self), self.verbose) >= DLT_RETURN_OK + ): found_data = True if ( self.filter