question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Client.captureException(None)` results in a `UnicodeDecodeError` with python 2.7 if future library is used

See original GitHub issue

This is happening on python 2.7.9 with raven 5.23.0. Full traceback from the sending thread is below:

Traceback (most recent call last):
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/raven/transport/threaded.py", line 174, in send_sync
    super(ThreadedHTTPTransport, self).send(data, headers)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/raven/transport/http.py", line 47, in send
    ca_certs=self.ca_certs,
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/raven/utils/http.py", line 66, in urlopen
    return opener.open(url, data, timeout)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/future/backports/urllib/request.py", line 494, in open
    response = self._open(req, data)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/future/backports/urllib/request.py", line 512, in _open
    '_open', req)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/future/backports/urllib/request.py", line 466, in _call_chain
    result = func(*args)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/raven/utils/http.py", line 46, in https_open
    return self.do_open(ValidHTTPSConnection, req)
  File "/home/russ/.virtualenvs/edms/lib/python2.7/site-packages/future/backports/urllib/request.py", line 1284, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/opt/python2.7.9/lib/python2.7/httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "/opt/python2.7.9/lib/python2.7/httplib.py", line 1035, in _send_request
    self.endheaders(body)
  File "/opt/python2.7.9/lib/python2.7/httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "/opt/python2.7.9/lib/python2.7/httplib.py", line 848, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)

Note the future module in there… suspicious?

For the failed capture, here is some debug info on what is being sent to the raven Client’s transport.async_send:

>>> type(data), data[:10], headers
(<type 'str'>,
 'x\x9c\xed]\x0bw\xe2F\xb2\xfe',
 {'Content-Encoding': 'deflate',
  'Content-Type': 'application/octet-stream',
  'User-Agent': 'raven-python/5.23.0',
  'X-Sentry-Auth': 'Sentry sentry_timestamp=1468702548.44, sentry_client=raven-python/5.23.0, sentry_version=6, sentry_key=3de74049829c4e22827b100a5ece12e1, sentry_secret=<too secret to include>'})

At the httplib exception location (msg += message_body) in the thread, we end up with str and unicode, and the zlib-compressed str block:

>>> type(msg), type(message_body), message_body[:10]
(<type 'unicode'>, <type 'str'>, 'x\x9c\xed]\x0bw\xe2F\xb2\xfe')

Also potentially of note is that self._buffer that got joined prior to the exception line was a mixed bag of str and unicode:

>>> map(type, self._buffer)
[<type 'unicode'>,
 <type 'str'>,
 <type 'unicode'>,
 <type 'str'>,
 <type 'unicode'>,
 <type 'str'>,
 <type 'str'>,
 <type 'unicode'>,
 <type 'str'>]
>>> self._buffer[:3]
[u'POST /api/4/store/ HTTP/1.1',
 'Accept-Encoding: identity',
 u'Content-Length: 5476']

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
hyperaircommented, Jul 23, 2019

Seems to be solved as of https://github.com/getsentry/raven-python/commit/c69abaa1506a426802e979c7f8c6a4ab0ef9c4d7#diff-1009f02b22291a2682b3d868a2211291R190

Specifically, this commit makes it so that raven.utils.compat.urllib2 is no longer future.backports.urllib.request on Python 2.

0reactions
untitakercommented, Aug 8, 2019

closing, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix: "UnicodeDecodeError: 'ascii' codec can't decode ...
If a UnicodeDecodeError from the read() line, then you've probably used the wrong encoding value. CSV Files. The Python 2.7 CSV module does...
Read more >
read_hdf throws UnicodeDecodeError with Python 3.5 and 3.6 ...
Problem description. The HDF5 dataset was created with pandas , to_hdf in Python 2.7 and can be read in by Python 2.7. When...
Read more >
codecs — Codec registry and base classes — Python 3.11.1 ...
Source code: Lib/codecs.py This module defines base classes for standard Python ... If encoding is not None , then the underlying encoded files...
Read more >
Six: Python 2 and 3 Compatibility Library — six 1.15.0 ...
This is unicode() in Python 2 and str in Python 3. Type for representing binary data. This is str in Python 2 and...
Read more >
Supporting Python 2 and Python 3 - scikit-bio
Recent versions of the future project stopped bundling the six library, so we also directly depend on six (e.g., for StringIO compatibility).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found