KeyError on self._in_use_connections.remove(connection)
See original GitHub issueVersion: 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:
- Created 5 years ago
- Reactions:5
- Comments:10 (5 by maintainers)
I guess there’s one case, thread A and B, A get lock and then call
reset()
, when A set pid withself.pid = os.getpid()
but notself._in_use_connections = set()
, B enter the_checkpid()
and fail to meet the conditionif self.pid != os.getpid()
, so B return and get connection from old_available_connections
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!