From cf0b803d505047452d9f7b502ac64ade3d531214 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Mon, 26 Jun 2023 19:19:34 +0000 Subject: [PATCH] zproject: Prevent having exactly 17/18 middlewares, for Python 3.11 bug. Having exactly 17 or 18 middlewares, on Python 3.11.0 and above, causes python to segfault when running tests with coverage; see https://github.com/python/cpython/issues/106092 Work around this by adding one or two no-op middlewares if we would hit those unlucky numbers. We only add them in testing, since coverage is a requirement to trigger it, and there is no reason to burden production with additional wrapping. --- zerver/middleware.py | 4 ++++ zproject/computed_settings.py | 4 ++-- zproject/test_extra_settings.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/zerver/middleware.py b/zerver/middleware.py index 67b19f6103..0d0c1a866d 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -689,3 +689,7 @@ class ZulipSCIMAuthCheckMiddleware(SCIMAuthCheckMiddleware): return response return None + + +class ZulipNoopMiddleware(MiddlewareMixin): + pass diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py index 45949c06dc..9f2bcba168 100644 --- a/zproject/computed_settings.py +++ b/zproject/computed_settings.py @@ -159,7 +159,7 @@ ALLOWED_HOSTS += [EXTERNAL_HOST_WITHOUT_PORT, "." + EXTERNAL_HOST_WITHOUT_PORT] # ... and with the hosts in REALM_HOSTS. ALLOWED_HOSTS += REALM_HOSTS.values() -MIDDLEWARE = ( +MIDDLEWARE = [ "zerver.middleware.TagRequests", "zerver.middleware.SetRemoteAddrFromRealIpHeader", "zerver.middleware.RequestContext", @@ -182,7 +182,7 @@ MIDDLEWARE = ( "two_factor.middleware.threadlocals.ThreadLocals", # Required by Twilio # Needs to be after CommonMiddleware, which sets Content-Length "zerver.middleware.FinalizeOpenGraphDescription", -) +] AUTH_USER_MODEL = "zerver.UserProfile" diff --git a/zproject/test_extra_settings.py b/zproject/test_extra_settings.py index be7a3d261b..02f5939eaa 100644 --- a/zproject/test_extra_settings.py +++ b/zproject/test_extra_settings.py @@ -15,6 +15,7 @@ from .settings import ( EXTERNAL_HOST, LOCAL_DATABASE_PASSWORD, LOGGING, + MIDDLEWARE, ) FULL_STACK_ZULIP_TEST = "FULL_STACK_ZULIP_TEST" in os.environ @@ -269,3 +270,13 @@ SCIM_CONFIG: Dict[str, SCIMConfigDict] = { "name_formatted_included": True, } } + + +while len(MIDDLEWARE) < 19: + # The following middleware serves to skip having exactly 17 or 18 + # middlewares, which can segfault Python 3.11 when running with + # coverage enabled; see + # https://github.com/python/cpython/issues/106092 + MIDDLEWARE += [ + "zerver.middleware.ZulipNoopMiddleware", + ]