-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connection pool in multi-thread is not thread safe #906
Comments
same question-) |
Are you taking into account the GIL when you are looking at the code? |
@rolette GIL cannot guarantee of list's threading safety.
i have to to use redis.BlockingConnectionPool,, it's threading safe since it uese |
Hi there. I landed here because I was experimenting some strange behaviour as well and thought of asking in this thread to understand a bit more about the client and its connection pool options. One difference though is that my environment is based on greenlets: our code base uses a certain lib that uses So we have a piece of code doing a certain number of concurrent HTTP requests using # simplified version ...
self.session = CachedSession(
backend="redis", cache_name=cache_name, allowable_codes=(200, 201), allowable_methods=("GET", "POST")
)
# ...
reqs = (grequests.post(url, session=self.session, auth=self.AUTH, json=data, timeout=self.TIMEOUT)
for url in urls)
resps = grequests.map(reqs) Everything seem to work fine (in this adventurous mix of having After we provided the redis client used by the session above with a connection pool of 1 connection like so: pool = redis.ConnectionPool(host="localhost", port=6379, db=0, max_connections=1) we started getting inexplicable (random) results (inconsistent data, nulls, etc), I would assume due to thread unsafety conditions... It was strange that I didn't get any exceptions as the doc points out:
When trying the blocking pool: pool = redis.BlockingConnectionPool(host="localhost", port=6379, db=0, max_connections=1) we went back to receiving correct results (as in the first use case when we didn't provide any pool or connection limits) whilst having 1 connection shared (correctly?) among all the coroutines. My question: how would then Any information to understand a bit more would be much appreciated :) |
when multi thread share one instance of connectionpool, the self._in_use_connections and self._available_connections and the self._created_connections are not thread safe.
i do not see lock with these variables, so the connection pool may create more connections than the max_connections variable in the ConnectionPool class
the only using of lock is the reset() method, but this method is for checking the change of pid, but not the id of thread.
thanks !!
The text was updated successfully, but these errors were encountered: