Skip to content

Commit

Permalink
Resolved race condition during Redis backend teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusnus committed Sep 20, 2024
1 parent cadcc9e commit 4522bdb
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

Check warning on line 11 in src/pytest_celery/vendors/redis/backend/api.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/redis/backend/api.py#L11

Added line #L11 was not covered by tests

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:

Check warning on line 31 in src/pytest_celery/vendors/redis/backend/api.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/redis/backend/api.py#L31

Added line #L31 was not covered by tests
# Remove the backend reference to prevent interaction with Redis
async_result.backend = None
except Exception:
pass

Check warning on line 35 in src/pytest_celery/vendors/redis/backend/api.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/redis/backend/api.py#L33-L35

Added lines #L33 - L35 were not covered by tests

super().teardown()

0 comments on commit 4522bdb

Please sign in to comment.