-
Notifications
You must be signed in to change notification settings - Fork 295
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
try more complete lifecycle #2744
Open
wild-endeavor
wants to merge
5
commits into
async-loop-issue
Choose a base branch
from
async-loop-issue-lifecycle
base: async-loop-issue
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+42
−45
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import asyncio | ||
from concurrent.futures import ThreadPoolExecutor | ||
from contextlib import contextmanager | ||
from signal import SIGINT, SIGTERM | ||
|
||
from flytekit.loggers import logger | ||
|
||
|
||
def handler(loop, s: int): | ||
loop.stop() | ||
logger.debug(f"Shutting down loop at {id(loop)} via {s!s}") | ||
loop.remove_signal_handler(SIGTERM) | ||
loop.add_signal_handler(SIGINT, lambda: None) | ||
|
||
|
||
@contextmanager | ||
def use_event_loop(): | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
executor = ThreadPoolExecutor() | ||
loop.set_default_executor(executor) | ||
for sig in (SIGTERM, SIGINT): | ||
loop.add_signal_handler(sig, handler, loop, sig) | ||
try: | ||
yield loop | ||
finally: | ||
tasks = asyncio.all_tasks(loop=loop) | ||
for t in tasks: | ||
logger.debug(f"canceling {t.get_name()}") | ||
t.cancel() | ||
group = asyncio.gather(*tasks, return_exceptions=True) | ||
loop.run_until_complete(group) | ||
executor.shutdown(wait=True) | ||
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is an existing event loop running, then I do not think a library should be modifying the global event loop.
If there is another event loop running with tasks from another library, then switching out the event loop from underneath them can cause problems. For example, if another library is actively scheduling work, they will have task end up in two different loops.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a try block around this then, to look for an existing event loop, and use that if found. But I think that is not a good idea. Still learning, but I feel libraries should not be creating event loops on import. As of today, this is true of flytekit's dependencies (except for the unionai library).
The lack of an event loop is why the error came about in the first place right?
asyncio.run()
creates and then destroys/cleans up the event loop. But if there's one that already exists (which is the case in the main issue with these unionfses), it will basically ignore it, and create a new one. Afterasyncio.run()
completes, it does not restore the existing one, which is why when the data persistence layer goes to look for one, it can't find one and errors.My issue with adding a try block... and then using the event loop if we find one.
asyncio.run
doesn't do this.pyflyte-execute
, then flytekit creates and manages its own event loop, if the user runsunion-execute
(which i know doesn't exist today), then unionai creates and manages its own event loop. Changing where the event loop is managed based on what library is installed feels bad. Also if a user installs some other library that we don't know about that creates its own event loop, and that is somehow triggered before this code is (yes i know this is unlikely since we load all flytekit code before loading user code), then flytekit ends up using some random event loop.Event loops are thread singletons, there's one per thread. I feel that if your library needs an event loop on import (not on calling an executable like
pyflyte
orpyflyte-execute
), then you should run it in a different thread.Alternatively we can also check to see if there's an event loop, save it to a variable if so, and then restore it later, basically what async run does, but with one extra step, but honestly I'd rather see it fail. Seeing the failure allowed us to find an issue in the union library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unionfs does not create the event loop during import. The event loop is created when
union://
fsspec protocol is used which initializes union's fsspec implementation.In principle, I am okay with this, but async python libraries do different things:
My preferred solution is 2, if there was a good way to pass in the new event loop into a library that does 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more, yeah shouldn't we do 2 always? We can't prevent user code from arbitrarily running
asyncio.run
in their code and as we've seen with our own usage, that will destroy the then-current event loop. So basically the rule is:Anytime an event loop is needed by flytekit/union, including when it needed by a downstream library like in the case of async grpc (and we'll just have to know), always create a loop on another thread and
There will probably be bugs related to usage of
id
in artifacts and maybe need more contextvars in general but we can address those.in the case of grpc aio, doesn't just having the loop set count as passing it in?