[fix] sqlitedb: create DB Schema (DDL) during app initialization (hardening) (#6187)

The initialization of the DB schema ("base schema") has so far been done on
demand, which causes race conditions with competing threads and processes.

The DDL statements for creating the "base schema" are now executed as part of
the initialization of the app.

Further improvements were made to harden the database applications:

- Wikidata & Radio-Browser engine perform their initialization only once (so far
  the initialization was carried out in each thread/process).

- If multiple processes try to set DB's WAL mode when opening the DB at the same
  time, this usually leads to another race condition, which is now also caught.

Related:

- https://github.com/searxng/searxng/issues/6181#issuecomment-4586705

Closes: #6181

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser
2026-06-10 15:48:49 +02:00
committed by GitHub
parent f3fab143be
commit 26801e92af
7 changed files with 74 additions and 17 deletions
+14 -1
View File
@@ -1348,6 +1348,8 @@ def run():
def init():
# pylint: disable=import-outside-toplevel
if searx.sxng_debug or app.debug:
app.debug = True
searx.sxng_debug = True
@@ -1358,6 +1360,18 @@ def init():
logger.error("server.secret_key is not changed. Please use something else instead of ultrasecretkey.")
sys.exit(1)
# init database schema first / DB schema is created with the first connect
from searx.data import get_cache
from searx.enginelib import ENGINES_CACHE
conn = get_cache().connect()
conn.close()
conn = ENGINES_CACHE.connect()
conn.close()
favicons.init()
# init application
locales_initialize()
valkey_initialize()
searx.plugins.initialize(app)
@@ -1366,7 +1380,6 @@ def init():
searx.search.initialize(check_network=True, enable_metrics=metrics)
limiter.initialize(app, settings)
favicons.init()
def static_headers(headers: Headers, _path: str, _url: str) -> None: