Skip to content

Commit

Permalink
zproject: Prevent having exactly 17/18 middlewares, for Python 3.11 bug.
Browse files Browse the repository at this point in the history
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 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.

(cherry picked from commit cf0b803)
  • Loading branch information
alexmv committed Jul 3, 2023
1 parent 8f98071 commit 2f91471
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions zerver/middleware.py
Expand Up @@ -705,3 +705,7 @@ def process_request(self, request: HttpRequest) -> Optional[HttpResponse]:
return response

return None


class ZulipNoopMiddleware(MiddlewareMixin):
pass
4 changes: 2 additions & 2 deletions zproject/computed_settings.py
Expand Up @@ -159,7 +159,7 @@
# ... and with the hosts in REALM_HOSTS.
ALLOWED_HOSTS += REALM_HOSTS.values()

MIDDLEWARE = (
MIDDLEWARE = [
"zerver.middleware.TagRequests",
"zerver.middleware.SetRemoteAddrFromRealIpHeader",
"zerver.middleware.RequestContext",
Expand All @@ -182,7 +182,7 @@
"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"

Expand Down
11 changes: 11 additions & 0 deletions zproject/test_extra_settings.py
Expand Up @@ -15,6 +15,7 @@
EXTERNAL_HOST,
LOCAL_DATABASE_PASSWORD,
LOGGING,
MIDDLEWARE,
)

FULL_STACK_ZULIP_TEST = "FULL_STACK_ZULIP_TEST" in os.environ
Expand Down Expand Up @@ -275,3 +276,13 @@ def set_loglevel(logger_name: str, level: str) -> None:
"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",
]

0 comments on commit 2f91471

Please sign in to comment.