You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
get_client within a task creates a new (unused) event loop for every thread #5309
We really should just be using asyncio.get_running_loop for this case where we just want to check if there's already an event loop for the current thread, without creating one. But because we need a Tornado loop, not an asyncio loop, we can't just compare that to loop (though we could peek into IOLoop._ioloop_for_asyncio).
But more broadly, I'm wondering why we even have AnyThreadEventLoopPolicy. Worker threads spinning up their own event loops seems like a very advanced use-case. Where do we actually need this behavior? Rather than adding this policy across the board, with potential for unexpected event loop creation like this, in those places where we do need it, could we explicitly create a new loop when necessary?
The AnyThreadEventLoopPolicy was introduced way back in #1518. There was discussion of the issues with this on tornadoweb/tornado#2183. Funnily enough, there was a comment back then predicting this issue:
This won't lead to waste unless you have a lot of threads that sometimes do something asynchronous but spend most of their time without an event loop, but that would be a very strange pattern.
This is exactly the pattern we've now created with get_client.
deftest_get_client_no_new_loops_sync(client):
deff(_):
importasynciotry:
asyncio.get_running_loop()
exceptRuntimeError:
passelse:
raiseRuntimeError("Worker thread already has running loop")
get_client()
try:
asyncio.get_running_loop()
exceptRuntimeError:
passelse:
raiseRuntimeError("Running loop created for worker thread after get_client")
client.gather(client.map(f, range(4)))
and was surprised it passed.
get_client is indeed creating a new event loop instance for every thread, but the loop is never started, so it's not running.
Just creating an extraneous event loop object per thread is still a little sloppy, but if they're not actually running, it seems pretty innocuous.
I'm wondering why we even have AnyThreadEventLoopPolicy
FWIW, running under uvloop tramples this (uvloop.install() is called after importing distributed.utils, which is where the AnyThreadEventLoopPolicy is set) and things seem to more or less work fine, which makes me think we'd be okay just removing the AnyThreadEventLoopPolicy and fixing anything that fails in tests. get_client inside a task was the first thing I've found that breaks on a real cluster, hence this issue, and there's a very easy fix:
Activity
gjoseph92 commentedon Sep 10, 2021
Update: I wrote a little test for this
and was surprised it passed.
get_clientis indeed creating a new event loop instance for every thread, but the loop is never started, so it's not running.Just creating an extraneous event loop object per thread is still a little sloppy, but if they're not actually running, it seems pretty innocuous.
FWIW, running under uvloop tramples this (
uvloop.install()is called after importingdistributed.utils, which is where theAnyThreadEventLoopPolicyis set) and things seem to more or less work fine, which makes me think we'd be okay just removing theAnyThreadEventLoopPolicyand fixing anything that fails in tests.get_clientinside a task was the first thing I've found that breaks on a real cluster, hence this issue, and there's a very easy fix:We can probably close this, but I'd also be happy to fix the 5 places in code we use the
self.loop is IOLoop.current()pattern to be more proper.Don't create unnecessary IOLoop instances
mrocklin commentedon Sep 10, 2021
I haven't done any actual research here, but we might also look into
self.thread_id == threading.get_ident(). This might not cover all cases though.Remove `AnyThreadEventLoopPolicy`