Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

UnboundLocalError: local variable 'result' referenced before assignment - on using Telegram bridge #2832

Closed
MurzNN opened this issue Jan 27, 2018 · 2 comments · Fixed by #8329
Labels
z-bug (Deprecated Label) Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution z-p3 (Deprecated Label)

Comments

@MurzNN
Copy link

MurzNN commented Jan 27, 2018

I got the UnboundLocalError: local variable 'result' referenced before assignment error multiple times, it happens every time when I start Mautrix-Telegram AS bridge on homeserver, in Synapse logs they looks like this:

янв 27 20:08:26 tad python[11795]: 2018-01-27 20:08:26,131 - synapse.http.server - 145 - ERROR - POST-2331- Failed handle request synapse.http.server._async_render on <synapse.rest.ClientRestResource object at 0x7f9e3d39a550>: <SynapseRequest at 0x7f9e3419b368 method=POST uri=/_matrix/client/r0/register?user_id=%40telegram_94477076%3Ahomeserver.ru&access_token=<redacted> clientproto=HTTP/1.0 site=8008>: Traceback (most recent call last):
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks
янв 27 20:08:26 tad python[11795]:     current.result = callback(current.result, *args, **kw)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1184, in gotResult
янв 27 20:08:26 tad python[11795]:     _inlineCallbacks(r, g, deferred)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1126, in _inlineCallbacks
янв 27 20:08:26 tad python[11795]:     result = result.throwExceptionIntoGenerator(g)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/python/failure.py", line 389, in throwExceptionIntoGenerator
янв 27 20:08:26 tad python[11795]:     return g.throw(self.type, self.value, self.tb)
янв 27 20:08:26 tad python[11795]: --- <exception caught here> ---
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/synapse/http/server.py", line 117, in wrapped_request_handler
янв 27 20:08:26 tad python[11795]:     yield request_handler(self, request, request_metrics)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1126, in _inlineCallbacks
янв 27 20:08:26 tad python[11795]:     result = result.throwExceptionIntoGenerator(g)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/python/failure.py", line 389, in throwExceptionIntoGenerator
янв 27 20:08:26 tad python[11795]:     return g.throw(self.type, self.value, self.tb)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/synapse/http/server.py", line 263, in _async_render
янв 27 20:08:26 tad python[11795]:     callback_return = yield callback(request, **kwargs)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1128, in _inlineCallbacks
янв 27 20:08:26 tad python[11795]:     result = g.send(result)
янв 27 20:08:26 tad python[11795]:   File "/usr/lib/python2.7/dist-packages/synapse/rest/client/v2_alpha/register.py", line 240, in on_POST
янв 27 20:08:26 tad python[11795]:     defer.returnValue((200, result))  # we throw for non 200 responses
янв 27 20:08:26 tad python[11795]: exceptions.UnboundLocalError: local variable 'result' referenced before assignment

On Mautrix-Telegram side here is error report:

[2018-01-27 20:08:26,084] [[email protected]] POST /join/%21VnRyoGslbQhPzSgQoZ%3Aqseo.ru None
[2018-01-27 20:08:26,115] [[email protected]] POST /register {'type': {'username': 'telegram_94477076'}}
[2018-01-27 20:08:26,135] [[email protected]] Failed to register @telegram_94477076:homeserver.ru!
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/mautrix_appservice/intent_api.py", line 306, in _ensure_registered
    self.client.register({"username": self.localpart})
  File "/usr/local/lib/python3.6/dist-packages/matrix_client/api.py", line 111, in register
    return self._send("POST", "/register", content)
  File "/usr/local/lib/python3.6/dist-packages/mautrix_appservice/intent_api.py", line 59, in _send
    return super()._send(method, path, content, query_params, headers, api_path=api_path)
  File "/usr/local/lib/python3.6/dist-packages/matrix_client/api.py", line 536, in _send
    code=response.status_code, content=response.text
matrix_client.errors.MatrixRequestError: 500: {"errcode":"M_UNKNOWN","error":"Internal server error"}
@richvdh richvdh added the Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution label Jan 29, 2018
@ara4n
Copy link
Member

ara4n commented Feb 6, 2018

So the problem here is that mautrix-telegram isn't handing a valid user or username to the registration API. @MurzNN can you find out what the JSON body is in the request it's making? Did you get it to work in the end?

@ara4n ara4n added z-bug (Deprecated Label) z-p3 (Deprecated Label) cannot reproduce labels Feb 6, 2018
@tulir
Copy link
Member

tulir commented Feb 6, 2018

The request body is in the logs: POST /register {'type': {'username': 'telegram_94477076'}}

After a bit of investigation, I found out that the problem on the mautrix-telegram side was using the matrix-client package from PyPI (well, I knew that already, but now I found out why). It's really outdated (last release March 2017) and the register method has changed completely after the release. I didn't realize that because I just looked at the code on master for documentation. Separate release-specific official docs (matrix-org/matrix-python-sdk#138) would probably be a good idea.

Anyway, the problem with synapse here is the internal server error. That shouldn't happen: it's a bad request, not an internal server error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
z-bug (Deprecated Label) Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution z-p3 (Deprecated Label)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants