From ca3924763cca64fc1ee146cb2dabf670a7d32e45 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Wed, 27 Jan 2021 12:38:52 +0300 Subject: [PATCH 1/3] synapse.storage.engines: use psycopg2cffi compat hook Plainly importing psycopg2cffi as psycopg2 is inadequate, because psycopg2 or its components (psycopg2.extras) are being also imported elsewhere and patching them all is prone to breakage. Fixes #5054. Signed-off-by: Ivan Shapovalov --- changelog.d/9270.misc | 1 + synapse/storage/engines/__init__.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changelog.d/9270.misc diff --git a/changelog.d/9270.misc b/changelog.d/9270.misc new file mode 100644 index 000000000000..314f5d4484f6 --- /dev/null +++ b/changelog.d/9270.misc @@ -0,0 +1 @@ +Restore PyPy compatibility by using psycopg2cffi consistently. diff --git a/synapse/storage/engines/__init__.py b/synapse/storage/engines/__init__.py index 035f9ea6e98b..f0c6ad356480 100644 --- a/synapse/storage/engines/__init__.py +++ b/synapse/storage/engines/__init__.py @@ -30,9 +30,11 @@ def create_engine(database_config) -> BaseDatabaseEngine: if name == "psycopg2": # pypy requires psycopg2cffi rather than psycopg2 if platform.python_implementation() == "PyPy": - import psycopg2cffi as psycopg2 # type: ignore - else: - import psycopg2 # type: ignore + from psycopg2cffi import compat # type: ignore + + compat.register() + + import psycopg2 # type: ignore return PostgresEngine(psycopg2, database_config) From 2884a7f876f1bb05197787c50790290c39ab94db Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Wed, 27 Jan 2021 12:50:34 +0300 Subject: [PATCH 2/3] synapse.app.base: only call gc.freeze() on CPython gc.freeze() is an implementation detail of CPython garbage collector, and notably does not exist on PyPy. Rather than playing whack-a-mole and skipping the call when under PyPy, simply restrict it to CPython because the whole gc module is implementation-defined. Signed-off-by: Ivan Shapovalov --- changelog.d/9270.misc | 2 +- synapse/app/_base.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog.d/9270.misc b/changelog.d/9270.misc index 314f5d4484f6..b571cd6f1042 100644 --- a/changelog.d/9270.misc +++ b/changelog.d/9270.misc @@ -1 +1 @@ -Restore PyPy compatibility by using psycopg2cffi consistently. +Restore PyPy compatibility by using psycopg2cffi consistently and not calling CPython GC methods. diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 395e202b89c7..9840a9d55b1b 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -16,6 +16,7 @@ import gc import logging import os +import platform import signal import socket import sys @@ -339,7 +340,7 @@ def run_sighup(*args, **kwargs): # rest of time. Doing so means less work each GC (hopefully). # # This only works on Python 3.7 - if sys.version_info >= (3, 7): + if platform.python_implementation() == "CPython" and sys.version_info >= (3, 7): gc.collect() gc.freeze() From eb092ecd73388d77c3f4813b4add8c326fef1c43 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Sat, 30 Jan 2021 18:39:47 +0300 Subject: [PATCH 3/3] Revert "synapse.storage.engines: use psycopg2cffi compat hook" This reverts commit ca3924763cca64fc1ee146cb2dabf670a7d32e45. --- changelog.d/9270.misc | 2 +- synapse/storage/engines/__init__.py | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/changelog.d/9270.misc b/changelog.d/9270.misc index b571cd6f1042..908e5ee78b6c 100644 --- a/changelog.d/9270.misc +++ b/changelog.d/9270.misc @@ -1 +1 @@ -Restore PyPy compatibility by using psycopg2cffi consistently and not calling CPython GC methods. +Restore PyPy compatibility by not calling CPython-specific GC methods when under PyPy. diff --git a/synapse/storage/engines/__init__.py b/synapse/storage/engines/__init__.py index f0c6ad356480..035f9ea6e98b 100644 --- a/synapse/storage/engines/__init__.py +++ b/synapse/storage/engines/__init__.py @@ -30,11 +30,9 @@ def create_engine(database_config) -> BaseDatabaseEngine: if name == "psycopg2": # pypy requires psycopg2cffi rather than psycopg2 if platform.python_implementation() == "PyPy": - from psycopg2cffi import compat # type: ignore - - compat.register() - - import psycopg2 # type: ignore + import psycopg2cffi as psycopg2 # type: ignore + else: + import psycopg2 # type: ignore return PostgresEngine(psycopg2, database_config)