question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@login_required with Flask 2.0 Async Routes, does not return a valid response

See original GitHub issue

Describe the bug When using @login_required with Flask 2.0 Async Routes I get an error.

To Reproduce Steps to reproduce the behavior: Flask 2.0.1 Werkzeug 2.0.1 Flask-Login 0.5.0

@api.route('/v1.0/check_async2')
async def check_async2():
	await asyncio.sleep(1)
	return "Hello"

@api.route('/v1.0/check_async3')
@login_required
async def check_async3():
	await asyncio.sleep(1)
	return "Hello"

Expected behavior Both should return “Hello”. However the second (check_async3) returns the following error:

"TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine."

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
SebastianB12commented, Nov 4, 2021

Could you give us an indication, when the release will happen? Looking forward to the Async functionality 😃

4reactions
cathaypacific8747commented, Jul 13, 2021

Same, took me a while to pinpoint the exact issue. Solved it temporarily by using

from flask import current_app
from flask_login import current_user

@api.route('/v1.0/check_async3')
async def check_async3():
    if not current_user.is_authenticated:
        return current_app.login_manager.unauthorized()
    await asyncio.sleep(1)
    return "Hello"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using async and await — Flask Documentation (2.2.x)
When a request comes in to an async view, Flask will start an event loop in a thread, run the view function there,...
Read more >
Async in Flask 2.0 - TestDriven.io
Starting with Flask 2.0, you can create asynchronous route handlers ... async def async_get_data(): await asyncio.sleep(1) return 'Done!
Read more >
Flask login @login_required not working - Stack Overflow
My problem was that the @flask_login.login_required decorator was above the @app.route('<path>') decorator, so I just replaced them and it ...
Read more >
The Art of Routing in Flask - Hackers and Slackers
Providing a list of accepted methods is a good way to build constraints into the route for a REST API endpoint, which only...
Read more >
Developing Python Web Applications with Flask
Flask does not provide an integrated Model (M) layer, and lets you pick your ... Instead of plain-text, you can response with an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found