Skip to content
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

synchronous startup function blocking on a future hangs when TestClient is used as a context manager #885

Closed
cjw296 opened this issue Mar 31, 2020 · 4 comments
Labels
testclient TestClient-related

Comments

@cjw296
Copy link
Contributor

cjw296 commented Mar 31, 2020

Here's a brief reproducer:

def test_async_startup_hangs():

    async def foo():
        pass

    def blocking_foo():
        coro = foo()
        loop = asyncio.get_event_loop()
        future = asyncio.run_coroutine_threadsafe(coro, loop)
        return future.result()

    app = Starlette(on_startup=[blocking_foo])

    with TestClient(app) as client:
        pass

The test hangs on entering the context inside the blocking stuff inside future.result.

If I just do:

app = Starlette(on_startup=[foo])

...then foo isn't run at all by the with TestClient block.

@cjw296 cjw296 changed the title async startup functions hang when TestClient is used as a context manager synchronous startup function blocking on a future hangs when TestClient is used as a context manager Mar 31, 2020
@cjw296
Copy link
Contributor Author

cjw296 commented Mar 31, 2020

Right, so it seems like the intention is to support async startup callables, so this issue becomes much more of an edge case. I've filed the more important problem under #886

@florimondmanca
Copy link
Member

I think this can be closed, since as you mentioned async startup callables should be called directly, without any thread wrapping.

@cjw296
Copy link
Contributor Author

cjw296 commented Mar 31, 2020

Not so sure, it's definitely not important, but there's still an edge case here where blocking on a future doesn't appear to work in a startup function.

@JayH5 JayH5 added the testclient TestClient-related label Sep 11, 2020
@Kludex
Copy link
Member

Kludex commented Apr 30, 2022

At the time this issue was created, Starlette was still didn't have anyio as dependency. Today, you can run blocking code with anyio.to_thread.

@Kludex Kludex closed this as completed Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testclient TestClient-related
Projects
None yet
Development

No branches or pull requests

4 participants