]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(adapters): avoid race condition when replacing class name with itself 1231/head
authorNicolas Noirbent <nicolas.noirbent@lumapps.com>
Wed, 3 Dec 2025 15:58:25 +0000 (16:58 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 5 Dec 2025 13:20:27 +0000 (14:20 +0100)
Another thread may be switched to between the dmap.pop(fqn) instruction
and the dmap[scls] one, typically at program startup when multiple
threads are making their "first" queries.

Close #1230

docs/news.rst
psycopg/psycopg/_adapters_map.py

index 539fb3d6ac18335bfe0de024829238b4887dd46a..4ac9cc8635073ccfeea49f79906bc529024e0d0c 100644 (file)
 Current release
 ---------------
 
+Psycopg 3.3.2
+^^^^^^^^^^^^^
+
+Fix race condition in adapters at startup (:ticket:`#1230`).
+
+
 Psycopg 3.3.1
 ^^^^^^^^^^^^^
 
index da19cbacc897044def442ca23dc41e6235b8cdb8..a986d77e44891ead4296904660efec3cad4c921e 100644 (file)
@@ -217,9 +217,10 @@ class AdaptersMap:
 
             # If the adapter is not found, look for its name as a string
             fqn = scls.__module__ + "." + scls.__qualname__
-            if fqn in dmap:
+            if (d := dmap.get(fqn)) is not None:
                 # Replace the class name with the class itself
-                d = dmap[scls] = dmap.pop(fqn)
+                dmap[scls] = d
+                dmap.pop(fqn, None)
                 return d
 
         format = PyFormat(format)