matrix-org / matrix-python-sdk

Matrix Client-Server SDK for Python 2 and 3
Apache License 2.0
255 stars 119 forks source link

JSONDecodeError when trying to interact with user in a non-healthy server #282

Open andrevmatos opened 6 years ago

andrevmatos commented 6 years ago

We got a small federation running for Raiden Network, and because of some disk full issue, one of the servers crashed. Following it, the healthy ones started returning some unexpected responses for interactions with users on the healthy ones. E.g. trying to invite an user in the unhealthy server to a room in the healthy one, the invite request returned:

<Response [429]>
{"errcode":"M_UNKNOWN","error":"Unknown Status"}

but from https://github.com/matrix-org/matrix-python-sdk/blob/e366d52850f2bd948e9613022b735054616cfe0b/matrix_client/api.py#L731-L735 if the error isn't a json, json.JSONDecodeError will be raised. Quick fix is to add json.JSONDecodeError to the handled errors in L734 Full stack traces and context can be found in raiden-network/raiden#2603

MyDev45 commented 5 years ago

Hi @andrevmatos !

I'm getting the same error while trying to connect to a server. What did you exactly do to solve the problem? I can't get it work.

Here is the piece of code i modified tonight: try: waittime = response.json()['retry_after_ms'] / 1000 except KeyError: try: errordata = json.loads(response.json()['error']) waittime = errordata['retry_after_ms'] / 1000 except json.JSONDecodeError: pass except KeyError: pass sleep(waittime) else: break

Style get the same "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" problem.

Thanks by advance !

andrevmatos commented 5 years ago

Hi, @MyDev45 . my solution for now was to handle the exception myself and implement a retry loop, outside matrix_client. The patch should be simple though, if you can fork or reinstantiate the classes.