Skip to content

Stale read after creating a model

Sentry Issue: ARKINDEX-BACKEND-19Q

TypeError: '>=' not supported between instances of 'NoneType' and 'int'
(11 additional frame(s) were not displayed)
...
  File "rest_framework/serializers.py", line 548, in data
    ret = super().data
  File "rest_framework/serializers.py", line 246, in data
    self._data = self.to_representation(self.instance)
  File "rest_framework/serializers.py", line 515, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "rest_framework/fields.py", line 1882, in to_representation
    return method(value)
  File "arkindex/training/serializers.py", line 47, in get_rights

This TypeError occurred during a POST to /api/v1/models/, which creates a new model. DRF tried to return the newly created model, but the ModelSerializer calls get_max_level to retrieve the access rights of the new model. Since the user who created the model is not a Django admin, the method tried to retrieve the membership for this user, which just got created alongside the new model at the Admin level. This membership was not yet sync'd up with the replica, so it was not found, which means the user has no rights at all on this model. get_max_level returned None, causing the TypeError here.

This could in theory be fixed by just adding .using('default') within get_max_level, but all uses of this method should be checked beforehand because this could potentially affect the performance of important endpoints. A simple alternative is to force the serializer to always return ['read', 'write', 'admin'] when we are in a POST request.