Repository navigation
Closed
Description
Envrionment
- redis: 2.10.6
- celery: 4.0.2
- gevent: 1.0.2/1.2.1
problem
I'm using celery for run massive task, with CELERY_RESULT_BACKEND was enabled. The launch command is celery -A server worker -P gevent -l info -E. It work fine, but sometime got AssertionError.
The stacktrace:
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/app/task.py", line 412, in delay
return self.apply_async(args, kwargs)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/app/task.py", line 535, in apply_async
**options
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/app/base.py", line 736, in send_task
self.backend.on_task_call(P, task_id)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/backends/redis.py", line 180, in on_task_call
self.result_consumer.consume_from(task_id)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/backends/redis.py", line 76, in consume_from
self._consume_from(task_id)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/celery/backends/redis.py", line 82, in _consume_from
self._pubsub.subscribe(key)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2482, in subscribe
ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2404, in execute_command
self._execute(connection, connection.send_command, *args)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2408, in _execute
return command(*args)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/connection.py", line 610, in send_command
self.send_packed_command(self.pack_command(*args))
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/connection.py", line 585, in send_packed_command
self.connect()
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/connection.py", line 502, in connect
callback(self)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2376, in on_connect
self.subscribe(**channels)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2482, in subscribe
ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2404, in execute_command
self._execute(connection, connection.send_command, *args)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/client.py", line 2408, in _execute
return command(*args)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/connection.py", line 610, in send_command
self.send_packed_command(self.pack_command(*args))
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/redis/connection.py", line 590, in send_packed_command
self._sock.sendall(item)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/gevent/socket.py", line 466, in sendall
data_sent += self.send(_get_memory(data, data_sent), flags, timeout=timeleft)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/gevent/socket.py", line 443, in send
self._wait(self._write_event)
File "/home/qfpay/agency_statistics/python/lib/python2.7/site-packages/gevent/socket.py", line 294, in _wait
assert watcher.callback is None, 'This socket is already used by another greenlet: %r' % (watcher.callback, )
AssertionError: This socket is already used by another greenlet: <bound method Waiter.switch of <gevent.hub.Waiter object at 0x34dda50>>
Checklist
- I have a look with the source code of celery, and found the pubsub object is shared with all task. May cause these task which run in different thread or greenlet, go into race condition.
- I have a look for homepage of
redis-py, got message what It is not safe to pass PubSub or Pipeline objects between threads..
Steps to reproduce
Oh no, it't not easy to reproduce. you may encounter one for run tens of thousands of task.
Expected behavior
No Exception.
Actual behavior
raise AssertionError.
Activity
georgepsarakis commentedon Apr 19, 2018
@ideascf can you please check if by any chance #4666 fixes this issue?
Funnyang commentedon May 3, 2018
Yes, the same thing happened to me. I got message of other tasks when I use get() and on_message. I want to implement the web log, I must write it by myself now
georgepsarakis commentedon Jun 18, 2018
@Funnyang @ideascf the latest release includes #4666 which might be fixing this issue. It would be great if you could try with the latest Celery version and share your feedback. Thanks!
monsterxx03 commentedon Jun 19, 2018
I think this problem still not fixed @georgepsarakis .
Tested with :
I tried with above packages on my prod env, came to similar problem, all happened during
subscribewhenapply_async, mainly caused by socket used by another greenletA simple script to reproduce:
This script will not report error, but if you do
redis-cli -p 6379 -n 1 monitor | grep -i subscribeon another shell when running , you will seeSUBSCRIBEcommand with multi channel at very early start:The root case is once gevent patched, redis client socket will by async, pubsub obj's
on_connectcallback will be called multi times: https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L2369I try to do a pre-ping in redis backend's init func (https://github.com/celery/celery/blob/master/celery/backends/redis.py#L204) to ensure connection is established before any task:
subscribe multi channels problem disappeared, but I'm not sure it's right way. (Also I didn't test it in my prod env).
Made redis pubsub calling thread safe
auvipy commentedon Feb 17, 2021
#6416 could fix this issue, @vinayinvicible can you verify this, please?
obaltian commentedon Sep 30, 2025
Can confirm this still happens on our prod environment on
celery==5.5.3withredis==5.2.1, but during some cleanup.Maybe its a side-effect of a fix since it fails on UNSUBSCRIBE which was added as a fix.
PS I already checked this advice to set
CELERY_RESULT_BACKEND_THREAD_SAFE = Trueand it's set.auvipy commentedon Sep 21, 2026
merged a newly proposed fix for this. please report if that do not fix the problem fully or introduce any new regression in upcoming v5.7 release