Skip to content

Commit

Permalink
Resolved race condition during Redis backend teardown (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusnus authored Sep 20, 2024
1 parent cadcc9e commit a1b73b5
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/pytest_celery/vendors/redis/backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@

import gc

from celery.result import AsyncResult

from pytest_celery.api.backend import CeleryTestBackend


class RedisTestBackend(CeleryTestBackend):
def teardown(self) -> None:
# When a test that has a AsyncResult object is finished
# there's a race condition between the AsyncResult object
# and the Redis container. The AsyncResult object tries
# to release the connection but the Redis container has already
# exited. This causes a warning to be logged. To avoid this
# warning to our best effort we force a garbage collection here.
gc.collect(1)
"""When a test that has a AsyncResult object is finished there's a race
condition between the AsyncResult object and the Redis container.
The AsyncResult object tries to release the connection but the
Redis container has already exited.
"""
# First, force a garbage collection to clean up unreachable objects
gc.collect()

# Next, find all live AsyncResult objects and clean them up
async_results = [obj for obj in gc.get_objects() if isinstance(obj, AsyncResult)]

for async_result in async_results:
try:
# Remove the backend reference to prevent interaction with Redis
async_result.backend = None
except Exception:
pass

super().teardown()

0 comments on commit a1b73b5

Please sign in to comment.