-
-
Notifications
You must be signed in to change notification settings - Fork 718
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
Cancel asyncio tasks on worker close #6098
Conversation
@graingert do you have time to review this? |
distributed/tests/test_worker.py
Outdated
|
||
async def f(ev): | ||
await ev.set() | ||
await asyncio.Future() # Block indefinitely |
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.
await asyncio.Future() # Block indefinitely | |
await asyncio.Event().wait() |
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.
it's the same, but sure
Thank you for the review @graingert . Aside from those two comments are you ok with this PR? (should I merge if @crusaderky resolves those issues?) |
distributed/worker.py
Outdated
for task in self._async_instructions: | ||
task.cancel() | ||
while self._async_instructions: | ||
await asyncio.sleep(0) |
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.
probably will need an asyncio.gather(*self._async_instructions, return_exceptions=True)
and something to handle close itself being cancelled
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 close itself is cancelled, it should not wait for other tasks being cancelled to return (which will happen at the next event loop cycle anyway)
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.
Tasks can handle the cancel eg:
async def example():
async with database():
await asyncio.Event().wait()
The worker will need to wait for the async context manager to close
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.
First, this isn't a plugin system. We don't have any tasks there that catch CancelledError.
Second,
while self._async_instructions:
await asyncio.sleep(0)
is doing exactly the same thing as the asyncio.gather you suggested.
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.
a user can submit an async function via c.submit(f, ev, key="f1")
which is likely to suppress cancellation. using while self._async_instructions: await asyncio.sleep(0)
burns the cpu while waiting for cancellation to be handled
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.
See latest commit
@graingert any further concerns here, or should I merge? |
Upon
Worker.close()
, clean up the nanny asyncio tasks that were spawned to runWorker.execute()
.ensure_computing
transitions to newWorkerState
event mechanism #5895Caveats