From f673b752b3525a57c00c178008d37ea96d80cd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Gastinel?= <264859180+gastinelt@users.noreply.github.com> Date: Fri, 6 Nov 2026 22:40:39 +0100 Subject: [PATCH] =?UTF-8?q?forum=20:=20verrou=20limit=C3=A9=20aux=20appels?= =?UTF-8?q?=20SQLite=20(connexion=20verrouill=C3=A9e),=20plus=20de=20verro?= =?UTF-8?q?u=20pendant=20la=20lecture=20r=C3=A9seau=20;=20d=C3=A9lai=20de?= =?UTF-8?q?=2030=20s=20sur=20les=20sockets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- forum/db.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++- forum/panneau.py | 12 +---------- forum/server.py | 11 +--------- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/forum/db.py b/forum/db.py index feede84..a2ed5e9 100644 --- a/forum/db.py +++ b/forum/db.py @@ -1,5 +1,6 @@ """Schéma SQLite et accès aux données (version 4 : + sessions Gastinel, demandes d'adhésion, photos de la fiche station).""" import sqlite3 +import threading SCHEMA = """ CREATE TABLE IF NOT EXISTS users ( @@ -129,6 +130,58 @@ SYSTEME = "systeme" # compte technique, auteur du sujet fixe BULLETIN = "Bulletin de la semaine du Vercors" +class _Resultat: + """Résultat déjà lu sous le verrou (les lignes ne dépendent plus de la connexion partagée).""" + def __init__(self, lignes, rowcount, lastrowid): + self._l, self.rowcount, self.lastrowid = lignes, rowcount, lastrowid + def fetchone(self): + return self._l.pop(0) if self._l else None + def fetchall(self): + l, self._l = self._l, [] + return l + def __iter__(self): + return iter(self.fetchall()) + + +class ConnexionVerrouillee: + """Connexion SQLite partagée entre les fils du serveur : chaque appel passe sous un verrou et lit + ses lignes tout de suite. Le verrou ne couvre que la base, jamais la lecture du réseau.""" + def __init__(self, con): + self._con, self.verrou = con, threading.RLock() + def execute(self, sql, params=()): + with self.verrou: + c = self._con.execute(sql, params) + return _Resultat(c.fetchall(), c.rowcount, c.lastrowid) + def executemany(self, sql, seq): + with self.verrou: + c = self._con.executemany(sql, seq) + return _Resultat([], c.rowcount, c.lastrowid) + def executescript(self, sql): + with self.verrou: + return self._con.executescript(sql) + def commit(self): + with self.verrou: + self._con.commit() + def rollback(self): + with self.verrou: + self._con.rollback() + def __enter__(self): # « with con: » = une transaction entière sous le verrou + self.verrou.acquire() + try: + self._con.__enter__() + except BaseException: + self.verrou.release() + raise + return self + def __exit__(self, *exc): + try: + return self._con.__exit__(*exc) + finally: + self.verrou.release() + def __getattr__(self, nom): + return getattr(self._con, nom) + + def connecter(chemin=":memory:"): con = sqlite3.connect(chemin, check_same_thread=False) con.row_factory = sqlite3.Row @@ -192,4 +245,4 @@ def connecter(chemin=":memory:"): con.execute("INSERT INTO topics(title, author_id, fixed, pinned, pin_pos, replies_open)" " VALUES (?, ?, 1, 1, 0, 0)", (BULLETIN, sid)) con.commit() - return con + return ConnexionVerrouillee(con) diff --git a/forum/panneau.py b/forum/panneau.py index 624ad84..19d5eab 100644 --- a/forum/panneau.py +++ b/forum/panneau.py @@ -11,7 +11,6 @@ Second serveur, sur la même base que le forum (python forum/panneau.py --port 8 Variables d'environnement : celles du forum (FORUM_HUB_URL, FORUM_CLIENT_ID, FORUM_OAUTH_SECRET[_FILE], FORUM_PROPRIETAIRE_ID) + FORUM_PANNEAU_REDIRECT_URI (défaut : http://localhost:/connexion/retour). """ -import threading import argparse import hashlib import hmac @@ -317,16 +316,7 @@ class Handler(BaseHTTPRequestHandler): def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None): h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())}) - # Une seule connexion SQLite partagée entre les fils : les requêtes passent une par une - # (sinon « bad parameter or other API misuse » et connexion coupée quand plusieurs arrivent ensemble). - VERROU_BASE = threading.RLock() - for nom in ("do_GET", "do_POST", "do_HEAD"): - orig = getattr(h, nom, None) - if orig is not None: - def enveloppe(self, _orig=orig): - with VERROU_BASE: - return _orig(self) - setattr(h, nom, enveloppe) + h.timeout = 30 # socket muette plus de 30 s : connexion fermée return ThreadingHTTPServer((hote, port), h) diff --git a/forum/server.py b/forum/server.py index fc7a642..ef35c9f 100644 --- a/forum/server.py +++ b/forum/server.py @@ -2049,16 +2049,7 @@ def form_supprimer(action, champ, ident, classe="suppr", resume="Supprimer", aid def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None): h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())}) - # Une seule connexion SQLite partagée entre les fils : les requêtes passent une par une - # (sinon « bad parameter or other API misuse » et connexion coupée quand plusieurs arrivent ensemble). - VERROU_BASE = threading.RLock() - for nom in ("do_GET", "do_POST", "do_HEAD"): - orig = getattr(h, nom, None) - if orig is not None: - def enveloppe(self, _orig=orig): - with VERROU_BASE: - return _orig(self) - setattr(h, nom, enveloppe) + h.timeout = 30 # socket muette plus de 30 s : connexion fermée return ThreadingHTTPServer((hote, port), h)