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.

objects defining async __call__ not run/awaited when used in on_startup

See original GitHub issue

Simple reproducer:

def test_async_startup_hangs():

    class Foo:
        async def __call__(self):
            raise Exception()

    app = Starlette(on_startup=[Foo()])

    with TestClient(app) as client:
        pass

No exception is raised when it should be. This, however, raises an exception as expected:

def test_async_startup_hangs():

    async def foo():
        raise Exception()

    app = Starlette(on_startup=[foo])

    with TestClient(app) as client:
        pass

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
tomchristiecommented, May 7, 2020

TBH I’d be quite keen to moving Starlette towards always expecting an async style on endpoints, startup/shutdown functions, error handlers etc.

Users could still use sync style functions, but we’d require them to be explicitly wrapped in a threadpooling sync -> async decorator.

1reaction
cjw296commented, Apr 3, 2020

Turns out this issue also affects partial:

https://stackoverflow.com/questions/52422860/partial-asynchronous-functions-are-not-detected-as-asynchronous

@tomchristie suggested Starlette should grow its own function to make these checks, once that’s happened, we should see about pushing it back into CPython core.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async OnStartup in a WPF application - Stack Overflow
You can call async Methods from within a synchronous method like this: YourAsyncMethod().Wait();. So you can make your OnStartup-Method ...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
Use of async and await enables the use of ordinary try / catch blocks around asynchronous code. Note: The await keyword is only...
Read more >
Async/Await - Best Practices in Asynchronous Programming
When an exception is thrown out of an async Task or async Task<T> method, that exception is captured and placed on the Task...
Read more >
Built in options for running async tasks
In this post I look at the problem of running one-off tasks asynchronously on app startup in ASP.NET Core, and explore the pros...
Read more >
Is there a reason why I should not call an Async method from ...
But I've not been able to find a reference to… ... public override async void OnStart() { await ... Use async Task instead....
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