closeio/socketshark

on_unsubscribe: asyncio.CancelledError when http_post()

inschiavon opened this issue · 0 comments

Hello, first of all, thanks for this magnificent library.

We have found an issue during the 'authorization_renewal_period'.

Summary

When the authorisation expires and, then, the authorisation is not valid, an on_unsuscribe event should be sent but our hook handler (our server) is not able to receive the request.

What we saw in the code

When the event of unsuscribing is happening and tries to send an http_post to the hook, there is an exception which is not handled: asyncio.CancelledError

What we did to fix it

We added a catch exception code block:

except asyncio.CancelledError:
    log.exception('Task was cancelled')

What was the behaviour after the fix

In the first try to run the function, it throws an exception and the try-catch block avoids finishing the execution
As it has a retry functionality, in the second try, the http post is sent to the hook and the client correctly receives the message indicating the unsubscription.

Where we saw it

How to replicate the issue:

  • Set a websocket config with and authorizer and authorization_renewal_period

E.g.

    'myservice': {
        'require_authentication': True,
        'authorizer': 'myhost/websocket/authorize/',
        'authorization_renewal_period': 10,
        'on_subscribe': 'myhost/websocket/on_subscribe/',
        'on_unsubscribe': 'myhost/websocket/on_unsubscribe/',
    }

  • Run the server
  • Run a websocket client, connect and authorise your client. The authorisation should eventually expire.
{
    "event": "auth",
    "method": "ticket",
    "ticket": "ticketxxx"
}

  • Subscribe the client to the service
{
    "event": "subscribe",
    "subscription": "myservice",
}

  • Wait until your subscription expires.

  • Then, when the authorization expires.

  • The authorization_renewal_period process should catch this expiration and start its process.

  • Socketshark tries to send:

{"pid": xx, "url": "http://myhost/websocket/on_unsubscribe/", "data": {"subscription": "myservice", ...}, "event": "http request", "level": "debug", "logger": "socketshark", "timestamp": "2020-12-18T13:09:58.012525Z"}

but as I mentioned before, myhost never receives the request due to the asyncio.CancelledError not handled.