From b993112558764e324693e5fc48ba1aab6e8980f2 Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Fri, 25 Jul 2025 15:22:35 +0200 Subject: [PATCH] semaphore-for-db-write-lock: sqlite only See #117 --- bugsink/transaction.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bugsink/transaction.py b/bugsink/transaction.py index 0be1a88..5129e58 100644 --- a/bugsink/transaction.py +++ b/bugsink/transaction.py @@ -7,6 +7,7 @@ import threading from django.db import transaction as django_db_transaction from django.db import DEFAULT_DB_ALIAS +from django.conf import settings from snappea.settings import get_settings as get_snappea_settings @@ -218,6 +219,17 @@ def immediate_atomic(using=None, savepoint=True, durable=True): # case. with immediate_atomic: yield + + elif "sqlite" not in settings.DATABASES[using]["ENGINE"]: + # The SemaphoreContext was added specifically to address the WAL growth issue in sqlite; better not to use it + # for other database backends; in particular, if such databases have longer default timeouts, then the error + # message may be confusing (semaphore timeout after 10s throws an error... while the thread that hogs the DB + # is _not_ (yet) timed out) + # + # in-string matching matches both our 'timed' backend and the django default. + with immediate_atomic: + yield + else: # https://stackoverflow.com/a/45681273/339144 provides some context on nesting context managers; and how to # proceed if you want to do this with an arbitrary number of context managers.