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

Decoding issue for 40010 Error (Invalid Channel Name) #569

Closed
umair-ably opened this issue Sep 25, 2024 · 11 comments · Fixed by #571
Closed

Decoding issue for 40010 Error (Invalid Channel Name) #569

umair-ably opened this issue Sep 25, 2024 · 11 comments · Fixed by #571
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@umair-ably
Copy link

umair-ably commented Sep 25, 2024

Publishing a message on an invalid channel causes the SDK to fail to decode the error response appropriately. Example error log:

####### TEXT ��error��message�resource: invalid name ":"�href� https://help.ably.io/error/40010�code��J�statusCode��
Traceback (most recent call last):
  File "/home/void/.pyenv/versions/klash/lib/python3.11/site-packages/ably/sync/util/exceptions.py", line 38, in raise_for_response
    json_response = response.json()
                    ^^^^^^^^^^^^^^^
  File "/home/void/.pyenv/versions/klash/lib/python3.11/site-packages/httpx/_models.py", line 766, in json
    return jsonlib.loads(self.content, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/void/.pyenv/versions/3.11.4/lib/python3.11/json/__init__.py", line 341, in loads
    s = s.decode(detect_encoding(s), 'surrogatepass')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte

Reproduction Steps
Publish a message to the ":" channel (or any other invalid channel)

Expected Behaviour
The SDK should correctly decode and return the 40010 error.

Additional Notes:
It's worth triggering a couple of other error codes within the SDK to see if they are decoded appropriately, just to ensure this decoding issue is not a widespread problem.

┆Issue is synchronized with this Jira Task by Unito

@umair-ably umair-ably added the bug Something isn't working. It's clear that this does need to be fixed. label Sep 25, 2024
@lmars
Copy link
Member

lmars commented Sep 25, 2024

@umair-ably I assume that error parsing code should be using the same logic as when decoding successful responses:

content_type = self.__response.headers.get('content-type')
if isinstance(content_type, str):
if content_type.startswith('application/x-msgpack'):
return msgpack.unpackb(content)
elif content_type.startswith('application/json'):
return self.__response.json()

@umair-ably
Copy link
Author

@lmars I'd assume so too with it failing to decode to json. This is the raw text printed out before it attempts to decode it:

####### TEXT ��error��message�resource: invalid name ":"�href� https://help.ably.io/error/40010�code��J�statusCode��

@lmars
Copy link
Member

lmars commented Sep 25, 2024

@umair-ably yes, so that is msgpack, evident from the error "can't decode byte 0x81 in position 0", which from my many hours of inspecting raw msgpack is the start of a msgpack encoded map containing one key-value pair.

@umair-ably
Copy link
Author

umair-ably commented Sep 25, 2024

@lmars I'm not sure I follow, are you implying the user is incorrectly assuming they will get a json response when instead it's actually a msgpack response?

@AlexanderArvidsson
Copy link

AlexanderArvidsson commented Sep 25, 2024

Hi, original reporter of the issue here:

We're using msgpack in frontend via the useBinaryProtocol, but our backend Python SDK isn't configured to use msgpack. I'm assuming that our Ably app in the Ably cloud is configured to output everything to msgpack, and the python SDK isn't handling that on errors.

I think @lmars is on the right track with the content type switch, since the error response handler just assumes Json at all times, as opposed to conditionally using msgpack or json.

@lmars
Copy link
Member

lmars commented Sep 25, 2024

@umair-ably apologies, let me explain.

The realtime system supports returning responses encoded as either JSON or msgpack, and the SDK indicates which encoding it wants by setting the HTTP Accept header or the format query param, which in turn is controlled by the useBinaryProtocol client option, true for msgpack, false for JSON.

It seems to me that within the SDK, it calls AblyException.raise_for_response in various places, for example here, but that isn't taking into account what encoding the SDK is expecting and always assuming JSON, which is incorrect.

I think this is therefore a bug in the SDK, specifically that raise_for_response should be checking whether the response is msgpack or JSON and decoding accordingly, rather than assuming JSON.

@umair-ably
Copy link
Author

That makes perfect sense - thanks @lmars! I'll get this prioritised in our next sprint planning

@lmars
Copy link
Member

lmars commented Sep 29, 2024

I've proposed a fix for this in #571, since this came up as a problem in some internal testing too.

@lmars lmars closed this as completed in #571 Oct 1, 2024
@AlexanderArvidsson
Copy link

Hi @lmars, can we get this fix released to Pip?

@lmars
Copy link
Member

lmars commented Oct 18, 2024

Hey @AlexanderArvidsson, I've raised this internally and we'll get it released asap.

@coderabbitai coderabbitai bot mentioned this issue Oct 18, 2024
@ttypic
Copy link
Contributor

ttypic commented Oct 18, 2024

Hey @AlexanderArvidsson, thanks a lot for pointing out that fix hasn't been released. We released ably-python v2.0.7 with the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

Successfully merging a pull request may close this issue.

4 participants