Skip to content
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

Python+asyncio+autobahn websockets: CancelledError not bubbling up in the stack? #1000

Closed
karolyi opened this issue May 15, 2018 · 1 comment

Comments

@karolyi
Copy link

karolyi commented May 15, 2018

Hey,

I have an asyncio-based websocket client implemented, with an onClose method:

    async def onClose(self, wasClean, code, reason):
        self.app.root.server_info.text = _(
            'Disconnected, reconnecting in 2 seconds')
        Logger.info(f'ServerTalker: Disconnected: {reason}')
        # Tasks in Tasks.all_tasks() are weakref's so this will get
        # garbage collected
        # self.factory.server_talker.transport = None
        try:
            await aiosleep(delay=5)
        except CancelledError:
            print('||| onClose CANCELLED')
            raise
        await self.factory.server_talker.create_server_connection()

Whenever I cancel the aiosleep from outside (aiosleep is just asyncio.sleep), it seems that the CancelledError raised at that point will get swallowed by some other part of the websocket client I don't have control over.

I get the debug from my print, but when I await the cancelled task to have it properly exited, the await takes forever, as if the CancelledError have been suppressed somewhere in the underlying code (not by me).

Could you please look into this?

@karolyi
Copy link
Author

karolyi commented May 15, 2018

Alright, I might be wrong here. Asyncio is not well documented on how to deal with cancelled tasks.

After some googling, I found a locked issue comment that is talking about the exact same issue. That is:

  • If you cancel a task, and don't catch the CancelledError, it SHOULD NOT be awaited
  • If you cancel a task, but catch the CancelledError, it SHOULD be awaited otherwise one can get errors at shutdown (python exiting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants