Skip to content

get_client within a task creates a new (unused) event loop for every thread #5309

Closed

Description

@gjoseph92

get_client tries to check if the current event loop is the same as the worker's event loop, calling tornado IOLoop.current() here:

asynchronous = self.loop is IOLoop.current()

IOLoop.current() calls asyncio.get_event_loop(). Normally I believe this would be an error when not on the main thread, but because of our AnyThreadEventLoopPolicy, we spin up a new, separate event loop for the calling thread.

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.

cc @jcrist

Activity

  1. gjoseph92 commented on Sep 10, 2021

    @gjoseph92
    CollaboratorAuthor

    Update: I wrote a little test for this

    def test_get_client_no_new_loops_sync(client):
        def f(_):
            import asyncio
    
            try:
                asyncio.get_running_loop()
            except RuntimeError:
                pass
            else:
                raise RuntimeError("Worker thread already has running loop")
    
            get_client()
    
            try:
                asyncio.get_running_loop()
            except RuntimeError:
                pass
            else:
                raise RuntimeError("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:

    try:
        asynchronous = self.loop is IOLoop.current(instance=False)
    except RuntimeError:
        asynchronous = False

    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.

  2. mrocklin commented on Sep 10, 2021

    @mrocklin
    Member

    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.

  3. added a commit that references this issue on Sep 10, 2021
    1591afb
  4. self-assigned this
    on Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions