Skip to content

Commit 024cfae

Browse files
committed
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 python/cpython#106092 Work around this by adding a no-op middleware if we would hit that unlucky number. We only add it in testing, since coverage is a requirement to trigger it, and there is no reason to burden production with an additional wrapping.
1 parent 6d969fb commit 024cfae

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

zerver/middleware.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,7 @@ def process_request(self, request: HttpRequest) -> Optional[HttpResponse]:
689689
return response
690690

691691
return None
692+
693+
694+
class ZulipNoopMiddleware(MiddlewareMixin):
695+
pass

zproject/computed_settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
# ... and with the hosts in REALM_HOSTS.
160160
ALLOWED_HOSTS += REALM_HOSTS.values()
161161

162-
MIDDLEWARE = (
162+
MIDDLEWARE = [
163163
"zerver.middleware.TagRequests",
164164
"zerver.middleware.SetRemoteAddrFromRealIpHeader",
165165
"zerver.middleware.RequestContext",
@@ -182,7 +182,7 @@
182182
"two_factor.middleware.threadlocals.ThreadLocals", # Required by Twilio
183183
# Needs to be after CommonMiddleware, which sets Content-Length
184184
"zerver.middleware.FinalizeOpenGraphDescription",
185-
)
185+
]
186186

187187
AUTH_USER_MODEL = "zerver.UserProfile"
188188

zproject/test_extra_settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
EXTERNAL_HOST,
1616
LOCAL_DATABASE_PASSWORD,
1717
LOGGING,
18+
MIDDLEWARE,
1819
)
1920

2021
FULL_STACK_ZULIP_TEST = "FULL_STACK_ZULIP_TEST" in os.environ
@@ -269,3 +270,13 @@ def set_loglevel(logger_name: str, level: str) -> None:
269270
"name_formatted_included": True,
270271
}
271272
}
273+
274+
275+
while len(MIDDLEWARE) < 19:
276+
# The following middleware serves to skip having exactly 17 or 18
277+
# middlewares, which can segfault Python 3.11 when running with
278+
# coverage enabled; see
279+
# https://github.com/python/cpython/issues/106092
280+
MIDDLEWARE += [
281+
"zerver.middleware.ZulipNoopMiddleware",
282+
]

0 commit comments

Comments
 (0)