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.

Differences in parsing with decode_responses=True, with and without hiredis

See original GitHub issue

I ran into a similar problem as #809: I have a redis connection using decode_repsonses=True, but I am also using a 3rd-party package (RQ) that pickles some data and deep in that library I get UnicodeDecodeErrors. I’m kind of stuck because of that library’s use of pickled data.

However, I thought to install hiredis-py to see if it handled parsing differently and it does.

So, with this:

import redis
import pickle
rdb = redis.StrictRedis(decode_responses=True)
data = pickle.dumps(dict(hello='world'))
rdb.set('foo', data)
rdb.get('foo')

With hiredis installed I see:

b'\x80\x03}q\x00X\x05\x00\x00\x00helloq\x01X\x05\x00\x00\x00worldq\x02s.'

Without hiredis installed, I get:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

Looking at hiredis-py code, I see the equivalent of this:

if encoding:
   try:
      data = data.decode(encoding)
   except ValueError:
      pass

I’m wishing redis-py did the same. I’d love to see the following change to connection.py:

def decode(self, value, force=False):
   "Return a unicode string from the byte representation"
   if (self.decode_responses or force) and isinstance(value, bytes):
      try:
         value = value.decode(self.encoding, self.encoding_errors)
      except ValueError:
         # Ignore encoding and return bytes
         pass
   return value

Considering using hiredis comes highly recommended, I’d love to see this base difference in parsing resolved.

Thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
piercefreemancommented, Feb 26, 2020

@andymccurdy Stumbled upon this thread when investigating my own bug with decoding responses with binary data. Is the addition of a r.responses_as_bytes context manager still on the roadmap or have you switched approaches?

3reactions
andymccurdycommented, Dec 30, 2018

The proposed change (redis/hiredis-py#82) to hiredis-py has been accepted and merged. Future versions of hiredis-py will behave sanely and no longer silently discard decoding errors by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differences in parsing with decode_responses=True ... - GitHub
I ran into a similar problem as #809: I have a redis connection using decode_repsonses=True, but I am also using a 3rd-party package...
Read more >
Hiredis | Redis
The following code creates a connection to Redis using hiredis' synchronous API: #include "hiredis.h" redisContext *c = redisConnect("hostname", port); if ...
Read more >
NodeJS using redis - installing with hiredis vs without?
For production I would seriously consider using hiredis parser because it performs better. For testing you do not need it.
Read more >
Use hiredis to increase performance of reading large sets
hiredis -rb uses the hiredis C client library to optimize read/writes to Redis. ... members = JSON.parse(redis.get(string_key)) } x.compare! end ...
Read more >
hiredis - PyPI
Python extension that wraps protocol parsing code in hiredis. ... When the buffer does not contain a full reply, gets returns False ....
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