Skip to content

Commit

Permalink
Raise an exception on unlocking expired lock
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max committed May 28, 2024
1 parent aafc369 commit 275a274
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cvat/apps/dataset_manager/tests/test_rest_api_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from functools import partial
from io import BytesIO
from tempfile import TemporaryDirectory
from time import sleep
from typing import Any, Callable, ClassVar, Optional, overload
from unittest.mock import MagicMock, patch, DEFAULT as MOCK_DEFAULT

Expand All @@ -32,6 +33,7 @@
import cvat.apps.dataset_manager as dm
from cvat.apps.dataset_manager.bindings import CvatTaskOrJobDataExtractor, TaskData
from cvat.apps.dataset_manager.task import TaskAnnotation
from cvat.apps.dataset_manager.util import get_export_cache_lock
from cvat.apps.dataset_manager.views import clear_export_cache, export, parse_export_file_path
from cvat.apps.engine.models import Task
from cvat.apps.engine.tests.utils import get_paginated_collection, ApiTestBase, ForceLogin
Expand Down Expand Up @@ -1812,6 +1814,14 @@ def test_export_can_create_file_and_cleanup_job(self):
self.assertTrue(osp.isfile(export_path))
mock_rq_scheduler.enqueue_in.assert_called_once()

def test_export_cache_lock_can_raise_on_releasing_expired_lock(self):
from pottery import ReleaseUnlockedLock

with self.assertRaises(ReleaseUnlockedLock):
lock_time = 2
with get_export_cache_lock('test_export_path', ttl=lock_time, acquire_timeout=5):
sleep(lock_time + 1)

def test_export_can_request_retry_on_locking_failure(self):
format_name = "CVAT for images 1.1"
images = self._generate_task_images(3)
Expand Down
4 changes: 2 additions & 2 deletions cvat/apps/dataset_manager/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def get_export_cache_lock(
masters={django_rq.get_connection(settings.CVAT_QUEUES.EXPORT_DATA.value)},
auto_release_time=ttl,
)
acquired = lock.acquire(blocking=block, timeout=acquire_timeout)
try:
acquired = lock.acquire(blocking=block, timeout=acquire_timeout)
if acquired:
yield lock
else:
raise LockNotAvailableError

finally:
if lock.locked():
if acquired:
lock.release()


Expand Down

0 comments on commit 275a274

Please sign in to comment.