Skip to content

fix _run_authentication creating the coroutine result twice #1457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ninja/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ def _run_authentication(self, request: HttpRequest) -> Optional[HttpResponse]:
if is_async_callable(callback) or getattr(callback, "is_async", False):
result = callback(request)
if inspect.iscoroutine(result):
result = async_to_sync(callback)(request)

async def await_result(cor: Coroutine) -> Any:
return await cor

result = async_to_sync(await_result)(result)
else:
result = callback(request)
Comment on lines 207 to 216
Copy link

@matez0 matez0 May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the try block,

                result = callback(request)
                if inspect.iscoroutine(result):
                    async def await_result(cor: Coroutine) -> Any:
                        return await cor

                    result = async_to_sync(await_result)(result)

would not be enough since result is created the same way in all cases and we may want to await result independently of the properties of callback?

except Exception as exc:
Expand Down