From 7ee71f63fd6fce6535f8f99f9d47199cd934bb31 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 6 Sep 2024 09:13:53 +0200 Subject: [PATCH] Don't commit when garbage-collected in a different thread (#81) --- jupyter_server_fileid/manager.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/jupyter_server_fileid/manager.py b/jupyter_server_fileid/manager.py index 30d7170..e19f204 100644 --- a/jupyter_server_fileid/manager.py +++ b/jupyter_server_fileid/manager.py @@ -386,8 +386,14 @@ def __del__(self): """Cleans up `ArbitraryFileIdManager` by committing any pending transactions and closing the connection.""" if hasattr(self, "con"): - self.con.commit() - self.con.close() + # If garbage collection happens in a different thread than the thread where + # the SQLite object was created, committing will fail anyway. We just ignore + # the exception if this is the case. + try: + self.con.commit() + self.con.close() + except sqlite3.ProgrammingError: + pass class LocalFileIdManager(BaseFileIdManager):