From e322c44d3e98a8d2d1193cd2584cc53470274d7f Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 29 Mar 2021 18:23:13 +0200 Subject: [PATCH] Stop runtime error on dict changing during write/init at start (#27) * Lock datastore when writing * Racecase fix * Tweaks to locking (add delay) --- backend/store.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/store.py b/backend/store.py index 6aace3c0..8155fbea 100644 --- a/backend/store.py +++ b/backend/store.py @@ -157,7 +157,6 @@ class ChangeDetectionStore: def data(self): has_unviewed = False - for uuid, v in self.__data['watching'].items(): self.__data['watching'][uuid]['newest_history_key'] = self.get_newest_history_key(uuid) if int(v['newest_history_key']) <= int(v['last_viewed']): @@ -251,7 +250,7 @@ class ChangeDetectionStore: json.dump(self.__data, json_file, indent=4) logging.info("Re-saved index") - self.needs_write = False + self.needs_write = False # Thread runner, this helps with thread/write issues when there are many operations that want to update the JSON # by just running periodically in one thread, according to python, dict updates are threadsafe. @@ -261,8 +260,15 @@ class ChangeDetectionStore: if self.stop_thread: print("Shutting down datastore thread") return + + # The delay here serves two purposes + # - Conserve CPU + # - when this thread function starts, it will try to write while other threads are modifying the structure + # The second point is indicative of the data structure/usage not really fitting the case, should switch to + # a real DB at some point + time.sleep(1) if self.needs_write: self.sync_to_json() - time.sleep(1) + # body of the constructor