thorn icon indicating copy to clipboard operation
thorn copied to clipboard

Local dispatcher error handlers don't work

Open jonashaag opened this issue 6 years ago • 0 comments

Using the local dispatcher (THORN_DISPATCHER = 'default'), not the Celery-based one, if the webhook raises an error (e.g. due to a timeout), the on_timeout and on_error handlers passed to send() aren't called. Instead the following internal error is raised:

  File "thorn/events.py", line 124, in send
    timeout=timeout, on_timeout=on_timeout,
  File "thorn/events.py", line 146, in _send
    allow_keepalive=self.allow_keepalive,
  File "thorn/dispatch/base.py", line 73, in send
    **kwargs
  File "vine/synchronization.py", line 57, in __init__
    [self.add_noincr(p) for p in promises or []]
  File "vine/synchronization.py", line 57, in <listcomp>
    [self.add_noincr(p) for p in promises or []]
  File "vine/synchronization.py", line 82, in add_noincr
    p.then(self)
AttributeError: 'NoneType' object has no attribute 'then'

I fixed (?) this with the following patch, although I am very unsure if it's correct. https://github.com/jonashaag/thorn/commit/15453b24521933f74106838f02923caf7450958f

Test case:

import django; django.setup()

import thorn
import thorn.django.models

thorn.django.models.Subscriber.objects.create(event='foo.*', url='http://8.8.8.8')

e = thorn.Event('foo.bar')
e.send({})

With my fix (?):

Traceback (most recent call last):
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/util/connection.py", line 83, in create_connection
    raise err
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connection.py", line 166, in connect
    conn = self._new_conn()
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connection.py", line 146, in _new_conn
    (self.host, self.timeout))
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPConnection object at 0x10faddda0>, 'Connection to 8.8.8.8 timed out. (connect timeout=1)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "thorn/env3/lib/python3.6/site-packages/requests-2.18.4-py3.6.egg/requests/adapters.py", line 440, in send
    timeout=timeout
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "thorn/env3/lib/python3.6/site-packages/urllib3-1.22-py3.6.egg/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='8.8.8.8', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10faddda0>, 'Connection to 8.8.8.8 timed out. (connect timeout=1)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    e.send({})
  File "thorn/thorn/events.py", line 124, in send
    timeout=timeout, on_timeout=on_timeout,
  File "thorn/thorn/events.py", line 146, in _send
    allow_keepalive=self.allow_keepalive,
  File "thorn/thorn/dispatch/base.py", line 73, in send
    **kwargs
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/synchronization.py", line 57, in __init__
    [self.add_noincr(p) for p in promises or []]
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/synchronization.py", line 57, in <listcomp>
    [self.add_noincr(p) for p in promises or []]
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/synchronization.py", line 82, in add_noincr
    p.then(self)
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/abstract.py", line 53, in then
    return self._p.then(on_success, on_error)
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/promises.py", line 173, in then
    callback.throw(self.reason)
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/synchronization.py", line 94, in throw
    self.p.throw(*args, **kwargs)
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/promises.py", line 217, in throw
    reraise(type(exc), exc, tb)
  File "thorn/env3/lib/python3.6/site-packages/vine-1.1.4-py3.6.egg/vine/five.py", line 178, in reraise
    raise value.with_traceback(tb)
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='8.8.8.8', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10faddda0>, 'Connection to 8.8.8.8 timed out. (connect timeout=1)'))

jonashaag avatar Oct 13 '17 08:10 jonashaag