From 62089fdb228f4aeb70cf6558fde939237d3a1ab3 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Sun, 28 Feb 2021 22:53:34 -0500 Subject: [PATCH] close sqlite3 connections if unused --- archivebox/config.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/archivebox/config.py b/archivebox/config.py index 85714a1b..73c9de51 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -1102,7 +1102,15 @@ def setup_django(out_dir: Path=None, check_db=False, config: ConfigDict=CONFIG, cache.get('test', None) except django.db.utils.OperationalError: call_command("createcachetable", verbosity=0) - + + + # if archivebox gets imported multiple times, we have to close + # the sqlite3 whenever we init from scratch to avoid multiple threads + # sharing the same connection by accident + from django.db import connections + for conn in connections.all(): + conn.close_if_unusable_or_obsolete() + sql_index_path = Path(output_dir) / SQL_INDEX_FILENAME assert sql_index_path.exists(), ( f'No database file {SQL_INDEX_FILENAME} found in: {config["OUTPUT_DIR"]} (Are you in an ArchiveBox collection directory?)')