-
Notifications
You must be signed in to change notification settings - Fork 22
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
Refactor room and server tasks cleanup #76
Conversation
self._broadcast_task.cancel() | ||
await self._broadcast_task |
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.
Awaiting the cancelled task will raise a CancelledError
.
await self._broadcast_task |
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 does not seem to raise a CancelledError
when I run this, unless it is caught somewhere and silently passing?
How do you make sure the task finished cancelling?
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.
How do you make sure the task finished cancelling?
Then you need to catch CancelledError
:
await self._broadcast_task | |
try: | |
await self._broadcast_task | |
except asyncio.CancelledError: | |
pass |
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.
Isn't the CancelledError
the thing that gets triggered in the task code when it is being cancelled?
Sorry if it's a dumb question, I'm new to these things.
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.
Yes CancelledError
is raised in the task, so since we're awaiting that task we should catch the exception.
But thinking about it I think we should just use AnyIO, with which all these things come for free.
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 see, so we'd need a restructure if we go with AnyIO
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.
But what happens if you want to cancel a task group from outside the context manager, can you do it?
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.
Yes, I'm going to open a PR to show what it would look like.
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.
thanks.
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 opened #77.
Co-authored-by: David Brochart <david.brochart@gmail.com>
There is a ypy-websocket/ypy_websocket/websocket_provider.py Lines 38 to 44 in 8010567
In the PR of jupyter_collaboration for improving the clean-up, I miss that point from Python documentation:
So it was a mistake to wait for the task after calling cancel on them. And the For example, the while loop should become: while True:
update = await self._update_queue.get()
message = create_update_message(update)
try:
await self._websocket.send(message)
except asyncio.CancelledError:
raise
except Exception:
pass |
Thank you for your comment @fcollonval ! I have a try. |
We should close this PR in favor of #77. |
This is partly taken from jupyter_collaboration.
The user API gets broken by this PR (making some APIs async that were sync before).