Skip to content

[Core] Remove asyncio.ensure_future call in run_async_func_in_event_loop - #32932

Merged
jjyao merged 4 commits into
ray-project:masterfrom
jjyao:jjyao/async
Mar 8, 2023
Merged

[Core] Remove asyncio.ensure_future call in run_async_func_in_event_loop#32932
jjyao merged 4 commits into
ray-project:masterfrom
jjyao:jjyao/async

Conversation

@jjyao

@jjyao jjyao commented Mar 1, 2023

Copy link
Copy Markdown
Contributor

Why are these changes needed?

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyward argument instead of positional argument.

I think this is the cause of https://discuss.ray.io/t/typeerror-ensure-future-takes-1-positional-argument-but-2-were-given/9453

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
Comment thread python/ray/_raylet.pyx Outdated
coroutine = func(*args, **kwargs)
if threading.get_ident() == async_thread.ident:
future = asyncio.ensure_future(coroutine, eventloop)
future = asyncio.ensure_future(coroutine, loop=eventloop)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to trigger this code path given all of our tests fail to uncover the bug.

@simon-mo I saw you added this in #8691, do you know how to trigger this path and why do we need the branching here instead of always calling run_coroutine_threadsafe?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm i think we should unify the code path here, especially run_coroutine_threadsafe can perform the check for us.

I believe this is triggered when the serde code gets called while we are in async thread, and the serde functions themselves involves calling async funcs. this is probably due to the fact during await object_ref the core worker as of 3 years ago will recursively call into this function so we need to handle this case.

nevertheless, this is definitely legacy code and safe to unify

@rkooo567 rkooo567 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's surprising this wasn't caught from testing? Can you add proper unit tests?

@jjyao

jjyao commented Mar 8, 2023

Copy link
Copy Markdown
Contributor Author

It's surprising this wasn't caught from testing? Can you add proper unit tests?

Unfortunately I was not able to get a reproduce. After talking to Simon, we think the code can be safely deleted.

@jjyao jjyao changed the title [Core] Fix asyncio.ensure_future call [Core] Remove asyncio.ensure_future call in run_async_func_in_event_loop Mar 8, 2023
@jjyao
jjyao merged commit fd74302 into ray-project:master Mar 8, 2023
@jjyao
jjyao deleted the jjyao/async branch March 8, 2023 22:22
ProjectsByJackHe pushed a commit to ProjectsByJackHe/ray that referenced this pull request Mar 21, 2023
…oop(ray-project#32932)

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyword argument instead of positional argument.

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
Signed-off-by: Jack He <jackhe2345@gmail.com>
edoakes pushed a commit to edoakes/ray that referenced this pull request Mar 22, 2023
…oop(ray-project#32932)

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyword argument instead of positional argument.

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
peytondmurray pushed a commit to peytondmurray/ray that referenced this pull request Mar 22, 2023
…oop(ray-project#32932)

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyword argument instead of positional argument.

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
elliottower pushed a commit to elliottower/ray that referenced this pull request Apr 22, 2023
…oop(ray-project#32932)

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyword argument instead of positional argument.

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
Signed-off-by: elliottower <elliot@elliottower.com>
ProjectsByJackHe pushed a commit to ProjectsByJackHe/ray that referenced this pull request May 4, 2023
…oop(ray-project#32932)

asyncio.run_coroutine_threadsafe can work regardless whether the calling thread is the same as the event loop thread or not.

The original asyncio.ensure_future call is also wrong: asyncio.ensure_future has the signature asyncio.ensure_future(obj, *, loop=None) so loop has to be provided as a keyword argument instead of positional argument.

Signed-off-by: Jiajun Yao <jeromeyjj@gmail.com>
Signed-off-by: Jack He <jackhe2345@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants