Skip to content

Why is creating a ClientSession outside of an event loop "dangerous"? #3072

Closed
@thanatos

Description

If you create a ClientSession outside of a running event loop, you get:

Creating a client session outside of coroutine is a very dangerous idea

Which comes from here

First and foremost, I would really like to know why this is dangerous.

Ideally, I'd like to have the actual warning inform me, so that I would not need to dig through the code to determine it. But looking at the code, I'm still not sure: why is this dangerous?

To explain my use case, hopefully: at program initialization we create a few global ClientSessions, and pass those into various instances of things that require them. I don't really see anything inherent in ClientSession that should prevent it from initializing itself synchronously, and this pattern (creating needed connection pools at a high level context in the program, and passing them, typically via constructor, to lower level contexts) is essentially how I think anyone would expect a connection-pool like object to operate. (Now, I'm writing server-side code, so these pools have a lifetime of effectively forever; we shut them down when the server shuts down. I can of course see a more client-side code wanting a very brief with ClientSession(), but that isn't my use-case.)

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions