From 88edeb42ad5b53279aced704f4c5a974882d575e Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Tue, 13 Sep 2016 11:29:30 +0200 Subject: [PATCH 1/2] Don't clear the user-defined function list until after the DB is actually closed. This order prevents problems in plugins that run just before closing the db, e.g., library closed plugins. It is possible that a better fix is to move the two lines of code into db.cache.close(). I looked for unwanted side effects and didn't see any, but I didnt want to make the more invasive change. --- src/calibre/gui2/ui.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 1335846de3..68e1d58a66 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -634,9 +634,6 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ olddb = self.library_view.model().db if copy_structure: default_prefs = olddb.prefs - - from calibre.utils.formatter_functions import unload_user_template_functions - unload_user_template_functions(olddb.library_id) except: olddb = None try: @@ -688,6 +685,8 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ try: if call_close: olddb.close() + from calibre.utils.formatter_functions import unload_user_template_functions + unload_user_template_functions(olddb.library_id) except: import traceback traceback.print_exc() From 1fe6c8d9a5987357da42ce23dfb751167e074a9c Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Tue, 13 Sep 2016 12:06:07 +0200 Subject: [PATCH 2/2] Move unloading formatter functions to backend.close(). --- src/calibre/db/backend.py | 7 ++++++- src/calibre/gui2/ui.py | 2 -- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index d556e8fc08..4c4ef58ea7 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -31,7 +31,8 @@ from calibre.utils.filenames import ( WindowsAtomicFolderMove, atomic_rename, remove_dir_if_empty, copytree_using_links, copyfile_using_links) from calibre.utils.img import save_cover_data_to -from calibre.utils.formatter_functions import load_user_template_functions +from calibre.utils.formatter_functions import (load_user_template_functions, + unload_user_template_functions) from calibre.db.tables import (OneToOneTable, ManyToOneTable, ManyToManyTable, SizeTable, FormatsTable, AuthorsTable, IdentifiersTable, PathTable, CompositeTable, UUIDTable, RatingTable) @@ -1032,6 +1033,10 @@ class DB(object): self.execute('UPDATE custom_columns SET mark_for_delete=1 WHERE id=?', (data['num'],)) def close(self, force=False): + try: + unload_user_template_functions(self.library_id) + except: + pass if getattr(self, '_conn', None) is not None: self._conn.close(force) del self._conn diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 68e1d58a66..0adfd2628c 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -685,8 +685,6 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ try: if call_close: olddb.close() - from calibre.utils.formatter_functions import unload_user_template_functions - unload_user_template_functions(olddb.library_id) except: import traceback traceback.print_exc()