cvat-ai / cvat

Annotate better with CVAT, the industry-leading data engine for machine learning. Used and trusted by teams at any scale, for data of any scale.
https://cvat.ai
MIT License
12.48k stars 2.99k forks source link

Internal Server Error. When trying to delete a task that was assigned to a deactivated and than removed user. #4088

Closed dvkruchinin closed 2 years ago

dvkruchinin commented 2 years ago

My actions before raising this issue

Internal Server Error. When trying to delete a task that was assigned to a user that was deactivated and then deleted. A similar situation occurs when trying to delete a task that was created by a user who was deactivated and then deleted. An error when opening a job that was assigned to a user who was deactivated and then deleted.

Expected Behaviour

It is possible that the task should be deleted.

Current Behaviour

ERROR django.request: Internal Server Error: /api/v1/tasks/182
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 173, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'owner'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner    
    response = get_response(request)
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
  response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception  
    self.raise_uncaught_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper        
    return bound_method(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 90, in destroy
    instance = self.get_object()

2021-12-27 12:49:42,347 DEBG 'runserver' stderr output:
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/generics.py", line 99, in get_object      
    self.check_object_permissions(self.request, obj)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 345, in check_object_permissions
    if not permission.has_object_permission(request, self, obj):
  File "/home/django/cvat/apps/iam/permissions.py", line 968, in has_object_permission
    return self.check_permission(request, view, obj)
  File "/home/django/cvat/apps/iam/permissions.py", line 957, in check_permission
    permissions.extend(perm.create(request, view, obj))
  File "/home/django/cvat/apps/iam/permissions.py", line 511, in create
    self = cls(scope, request, view, obj)
  File "/home/django/cvat/apps/iam/permissions.py", line 554, in __init__
    self.payload['input']['resource'] = self.resource
  File "/home/django/cvat/apps/iam/permissions.py", line 562, in resource
    "owner": { "id": getattr(self.obj.owner, 'id', None) },
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 187, in __get__
    rel_obj = self.get_object(instance)
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 154, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/opt/venv/lib/python3.8/site-packages/cacheops/query.py", line 352, in get
    return qs._no_monkey.get(qs, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.

log - Internal Server Error: /api/v1/tasks/182
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 173, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'owner'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner    
    response = get_response(request)
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception  
    self.raise_uncaught_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper        
    return bound_method(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 90, in destroy
    instance = self.get_object()
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/generics.py", line 99, in get_object      
   self.check_object_permissions(self.request, obj)
 File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 345, in check_object_permissions
   if not permission.has_object_permission(request, self, obj):
 File "/home/django/cvat/apps/iam/permissions.py", line 968, in has_object_permission
   return self.check_permission(request, view, obj)
 File "/home/django/cvat/apps/iam/permissions.py", line 957, in check_permission
   permissions.extend(perm.create(request, view, obj))
 File "/home/django/cvat/apps/iam/permissions.py", line 511, in create
   self = cls(scope, request, view, obj)
 File "/home/django/cvat/apps/iam/permissions.py", line 554, in __init__
   self.payload['input']['resource'] = self.resource
 File "/home/django/cvat/apps/iam/permissions.py", line 562, in resource
   "owner": { "id": getattr(self.obj.owner, 'id', None) },
 File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 187, in __get__
   rel_obj = self.get_object(instance)
 File "/opt/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 154, in get_object
   return qs.get(self.field.get_reverse_related_filter(instance))
 File "/opt/venv/lib/python3.8/site-packages/cacheops/query.py", line 352, in get
   return qs._no_monkey.get(qs, *args, **kwargs)
 File "/opt/venv/lib/python3.8/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.

Possible Solution

Fix the bug.

Steps to Reproduce (for bugs)

  1. Create a user and assign a task to him. Deactivate and delete this user. Try to delete the task.
  2. Create a user. Create a task on his behalf. Deactivate and delete the user. Try to delete the task.
  3. Create a user and assign a job to him. Deactivate and delete this user. Try to open the job.

Context

Your Environment

Next steps

You may join our Gitter channel for community support.

g-kartik commented 2 years ago

@nmanovic If you haven't started working on it yet, may I take up this issue?

nmanovic commented 2 years ago

@g-kartik , it will be great! Thanks for your contribution.

g-kartik commented 2 years ago

@g-kartik , it will be great! Thanks for your contribution.

Thank You!

dvkruchinin commented 2 years ago

A similar error occurs if the user was in the organization and then was removed from the database (deactivation of the user is not necessary).

nmanovic commented 2 years ago

It looks like it is a problem with using cacheops: https://github.com/Suor/django-cacheops/issues/348

Basically we have code which cache Task.objects.get() and Job.objects.get(): https://github.com/openvinotoolkit/cvat/blob/c9aa9a12e1fd9bacb7bb017c0cf3671616d57e79/cvat/settings/base.py#L326

Solution:

  1. I'm going to cache users instead
  2. I'm not going to cache tasks and jobs for now
  3. I will create an issue about rethinking the caching strategy. Need to understand what to cache and how. It should be in scope of performance optimization task for CVAT server.