From: Nicolas Noirbent Date: Wed, 3 Dec 2025 15:58:25 +0000 (+0100) Subject: fix(adapters): avoid race condition when replacing class name with itself X-Git-Tag: 3.3.2~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08bfe006ae0d9dea00b69f6a6df8497a0c0e4965;p=thirdparty%2Fpsycopg.git fix(adapters): avoid race condition when replacing class name with itself 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 --- diff --git a/docs/news.rst b/docs/news.rst index 539fb3d6a..4ac9cc863 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -10,6 +10,12 @@ Current release --------------- +Psycopg 3.3.2 +^^^^^^^^^^^^^ + +Fix race condition in adapters at startup (:ticket:`#1230`). + + Psycopg 3.3.1 ^^^^^^^^^^^^^ diff --git a/psycopg/psycopg/_adapters_map.py b/psycopg/psycopg/_adapters_map.py index da19cbacc..a986d77e4 100644 --- a/psycopg/psycopg/_adapters_map.py +++ b/psycopg/psycopg/_adapters_map.py @@ -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)