Skip to content

Commit 4037177

Browse files
committed
fix(account/middleware): SyncToAsync never awaited
1 parent a2a051d commit 4037177

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

Diff for: ChangeLog.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
0.61.1 (unreleased)
22
*******************
33

4+
Fixes
5+
-----
6+
7+
- Fixed a ``RuntimeWarning`` that could occur when running inside an async
8+
environment (``'SyncToAsync' was never awaited``).
9+
10+
411
Security notice
512
---------------
613

Diff for: allauth/account/middleware.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ async def middleware(request):
1515
with context.request_context(request):
1616
try:
1717
response = await get_response(request)
18-
_remove_dangling_login(
19-
request, response, sync_to_async(_session_check)
20-
)
18+
if _should_check_dangling_login(request, response):
19+
await _acheck_dangling_login(request)
2120
return response
2221
except ImmediateHttpResponse as e:
2322
return e.response
@@ -28,32 +27,37 @@ def middleware(request):
2827
with context.request_context(request):
2928
try:
3029
response = get_response(request)
31-
_remove_dangling_login(request, response, _session_check)
30+
if _should_check_dangling_login(request, response):
31+
_check_dangling_login(request)
3232
return response
3333
except ImmediateHttpResponse as e:
3434
return e.response
3535

3636
return middleware
3737

3838

39-
def _remove_dangling_login(request, response, session_check):
39+
def _should_check_dangling_login(request, response):
4040
content_type = response.headers.get("content-type")
4141
if content_type:
4242
content_type = content_type.partition(";")[0]
4343
if content_type and content_type != "text/html":
44-
return
44+
return False
4545
if request.path.startswith(settings.STATIC_URL) or request.path in [
4646
"/favicon.ico",
4747
"/robots.txt",
4848
"/humans.txt",
4949
]:
50-
return
50+
return False
5151
if response.status_code // 100 != 2:
52-
return
53-
session_check(request)
52+
return False
53+
return True
5454

5555

56-
def _session_check(request):
56+
def _check_dangling_login(request):
5757
if not getattr(request, "_account_login_accessed", False):
5858
if "account_login" in request.session:
5959
request.session.pop("account_login")
60+
61+
62+
async def _acheck_dangling_login(request):
63+
await sync_to_async(_check_dangling_login)(request)

Diff for: shell.nix

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ stdenv.mkDerivation {
2424
python310Packages.sphinx-rtd-theme
2525
python310Packages.requests-oauthlib
2626
python310Packages.tox
27+
python310Packages.daphne
2728
sphinx
2829
twine
2930
];

0 commit comments

Comments
 (0)