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.

KeyError on self._in_use_connections.remove(connection)

See original GitHub issue

Version: 2.10.5 Platform: Python 2.7, Ubuntu This is a very rare occurrence on a very high traffic server but I receive a key error occasionally from here: self._in_use_connections.remove(connection) https://github.com/andymccurdy/redis-py/blob/2.10.5/redis/connection.py#L913 I haven’t been able to reproduce this in testing or root out the cause.

Theories: self._available_connections is a list so you could append in the same connection more then once. Then when you try to self._in_use_connections.add(connection) https://github.com/andymccurdy/redis-py/blob/2.10.5/redis/connection.py#L898 You get no error for using the same connection. Until you try to release _in_use_connections is a set so were the add() multiple times desen’t raise an error remove() will.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Yanght115commented, Jul 18, 2019
 def reset(self):
        self.pid = os.getpid()
        self._created_connections = 0
        self._available_connections = []
        self._in_use_connections = set()
        self._check_lock = threading.Lock()

    def _checkpid(self):
        if self.pid != os.getpid():
            with self._check_lock:
                if self.pid == os.getpid():
                    # another thread already did the work while we waited
                    # on the lock.
                    return
                self.reset()

    def get_connection(self, command_name, *keys, **options):
        "Get a connection from the pool"
        self._checkpid()
        try:
            connection = self._available_connections.pop()
        except IndexError:
            connection = self.make_connection()

I guess there’s one case, thread A and B, A get lock and then call reset(), when A set pid with self.pid = os.getpid() but not self._in_use_connections = set(), B enter the _checkpid() and fail to meet the condition if self.pid != os.getpid(), so B return and get connection from old _available_connections

0reactions
andymccurdycommented, Jan 27, 2020

I’m not a fan of simply moving the .pid assignment below the other statements. It might work more than the released version but it’s still not thread safe.

I just created a new PR #1270 that implements what I believe is a fully thread-safe pool. Can anyone please give it a spin and see if it fixes your issue? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyError on self._in_use_connections.remove(connection)
This is a very rare occurrence on a very high traffic server but I receive a key error occasionally from here: self._in_use_connections.remove(connection)
Read more >
"keyerror" when trying to access a node attribute in networkx
I can do this using the predecessor function in networokx, but when I try to access the node attribute i get a "key...
Read more >
Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >
How to Fix KeyError Exceptions in Python - Rollbar
The Python KeyError is an exception that occurs when an attempt is made to access an item in a dictionary that does not...
Read more >
KeyError Pandas – How To Fix - Data Independent
Pandas KeyError - This annoying error means that Pandas can not find your column name in your dataframe. Here's how to fix this...
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