From 535163b6758ebf2efc3768340c121db62d6084cf Mon Sep 17 00:00:00 2001 From: croneter Date: Thu, 30 Aug 2018 12:27:16 +0200 Subject: [PATCH] Fix Kodi crash by committing to DB frequently - Hopefully fixes #531 --- .../lib/library_sync/process_metadata.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/resources/lib/library_sync/process_metadata.py b/resources/lib/library_sync/process_metadata.py index c0258730..2aa683e0 100644 --- a/resources/lib/library_sync/process_metadata.py +++ b/resources/lib/library_sync/process_metadata.py @@ -12,6 +12,11 @@ from . import sync_info ############################################################################### LOG = getLogger("PLEX." + __name__) +# Commit to DB happens only after leaving respective itemtypes context manager +# with item_fct() as item_class: +# After how many items shall we additionally commit? Otherwise, Kodi +# crashes if we sync thousands of items without a single commit. +COMMIT_AFTER_N_ITEMS = 1000 ############################################################################### @@ -57,14 +62,12 @@ class ThreadedProcessMetadata(Thread): LOG.debug('Processing thread started') # Constructs the method name, e.g. itemtypes.Movies item_fct = getattr(itemtypes, self.item_class) - # cache local variables because it's faster - queue = self.queue - stopped = self.stopped + n = 0 with item_fct() as item_class: - while stopped() is False: + while self.stopped() is False: # grabs item from queue try: - item = queue.get(block=False) + item = self.queue.get(block=False) except Empty: sleep(20) continue @@ -83,6 +86,11 @@ class ThreadedProcessMetadata(Thread): with sync_info.LOCK: sync_info.PROCESS_METADATA_COUNT += 1 sync_info.PROCESSING_VIEW_NAME = item['title'] - queue.task_done() + n += 1 + if n == COMMIT_AFTER_N_ITEMS: + n = 0 + item_class.plexconn.commit() + item_class.kodiconn.commit() + self.queue.task_done() self.terminate_now() LOG.debug('Processing thread terminated')