Skip to content

Commit

Permalink
bugfix: when set config result_expires = 0, chord.get will hang. (#6373)
Browse files Browse the repository at this point in the history
* bugfix: when set config result_expires = 0, chord.get will hang.

`EXPIRE key 0` will delete a key in redis, then chord will never get the
result.

fix: #5237

* test: add testcase for expire when set config with zero.
  • Loading branch information
laixintao authored and thedrow committed May 31, 2021
1 parent b7eb0ba commit 2c39a70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion celery/backends/redis.py
Expand Up @@ -436,7 +436,7 @@ def on_chord_part_return(self, request, state, result,
if self._chord_zset
else pipe.rpush(jkey, encoded).llen(jkey)
).get(tkey)
if self.expires is not None:
if self.expires:
pipeline = pipeline \
.expire(jkey, self.expires) \
.expire(tkey, self.expires)
Expand Down
18 changes: 18 additions & 0 deletions t/unit/backends/test_redis.py
Expand Up @@ -695,6 +695,24 @@ def test_on_chord_part_return_no_expiry(self, restore):

self.b.expires = old_expires

@patch('celery.result.GroupResult.restore')
def test_on_chord_part_return_expire_set_to_zero(self, restore):
old_expires = self.b.expires
self.b.expires = 0
tasks = [self.create_task(i) for i in range(10)]

for i in range(10):
self.b.on_chord_part_return(tasks[i].request, states.SUCCESS, i)
assert self.b.client.zadd.call_count
self.b.client.zadd.reset_mock()
assert self.b.client.zrangebyscore.call_count
jkey = self.b.get_key_for_group('group_id', '.j')
tkey = self.b.get_key_for_group('group_id', '.t')
self.b.client.delete.assert_has_calls([call(jkey), call(tkey)])
self.b.client.expire.assert_not_called()

self.b.expires = old_expires

@patch('celery.result.GroupResult.restore')
def test_on_chord_part_return_no_expiry__unordered(self, restore):
self.app.conf.result_backend_transport_options = dict(
Expand Down

0 comments on commit 2c39a70

Please sign in to comment.