Skip to content

Commit

Permalink
fix(account/middleware): SyncToAsync never awaited
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Feb 9, 2024
1 parent a2a051d commit 4037177
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
0.61.1 (unreleased)
*******************

Fixes
-----

- Fixed a ``RuntimeWarning`` that could occur when running inside an async
environment (``'SyncToAsync' was never awaited``).


Security notice
---------------

Expand Down
24 changes: 14 additions & 10 deletions allauth/account/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ async def middleware(request):
with context.request_context(request):
try:
response = await get_response(request)
_remove_dangling_login(
request, response, sync_to_async(_session_check)
)
if _should_check_dangling_login(request, response):
await _acheck_dangling_login(request)
return response
except ImmediateHttpResponse as e:
return e.response
Expand All @@ -28,32 +27,37 @@ def middleware(request):
with context.request_context(request):
try:
response = get_response(request)
_remove_dangling_login(request, response, _session_check)
if _should_check_dangling_login(request, response):
_check_dangling_login(request)
return response
except ImmediateHttpResponse as e:
return e.response

return middleware


def _remove_dangling_login(request, response, session_check):
def _should_check_dangling_login(request, response):
content_type = response.headers.get("content-type")
if content_type:
content_type = content_type.partition(";")[0]
if content_type and content_type != "text/html":
return
return False
if request.path.startswith(settings.STATIC_URL) or request.path in [
"/favicon.ico",
"/robots.txt",
"/humans.txt",
]:
return
return False
if response.status_code // 100 != 2:
return
session_check(request)
return False
return True


def _session_check(request):
def _check_dangling_login(request):
if not getattr(request, "_account_login_accessed", False):
if "account_login" in request.session:
request.session.pop("account_login")


async def _acheck_dangling_login(request):
await sync_to_async(_check_dangling_login)(request)
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ stdenv.mkDerivation {
python310Packages.sphinx-rtd-theme
python310Packages.requests-oauthlib
python310Packages.tox
python310Packages.daphne
sphinx
twine
];
Expand Down

0 comments on commit 4037177

Please sign in to comment.