Google Sheets Python Client gives indeterministic behavior

I have created a Django project with uses Google Sheets to store some data. When I make a request to an endpoint (/ca/register), sometimes the request passes successfully with a new ca object being created in the database, and its details being added to the corresponding Google Sheet too.

Other times, with the same data, the new CA's data is added to the Django database, but the script responsible for adding the data to the Google Sheets fails, leading the request to fail as a whole. I get a weird error in the deployment logs.

PS. CA stands for Campus Ambassador.

Attaching the relevant pieces of codes and logs:

Error Log

Internal Server Error: /ca/register/
Traceback (most recent call last):
File "/opt/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/opt/venv/lib/python3.7/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.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/opt/venv/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/opt/venv/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/opt/venv/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/app/ca/views.py", line 19, in post
self.serializer.save()
File "/opt/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 212, in save
self.instance = self.create(validated_data)
File "/app/ca/serializers.py", line 63, in create
ca.whyChooseYou, ca.wereCa, ca.isChoosen, ca.registerations])
File "/app/scripts/caRegistrations.py", line 119, in add_registration_details
spreadsheetId=spreadsheet_id, range=cls.RANGE_NAME, valueInputOption=cls.value_input_option, body=body).execute()
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 932, in execute
headers=self.headers,
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 222, in _retry_request
raise exception
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 191, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/google_auth_httplib2.py", line 225, in request
**kwargs
File "/opt/venv/lib/python3.7/site-packages/httplib2/__init__.py", line 1721, in request
conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,
File "/opt/venv/lib/python3.7/site-packages/httplib2/__init__.py", line 1440, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/opt/venv/lib/python3.7/site-packages/httplib2/__init__.py", line 1392, in _conn_request
response = conn.getresponse()
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/client.py", line 1373, in getresponse
response.begin()
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/client.py", line 319, in begin
version, status, reason = self._read_status()
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/client.py", line 280, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/ssl.py", line 1071, in recv_into
return self.read(nbytes, buffer)
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/ssl.py", line 929, in read
return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: KRB5_S_TKT_NYV] unexpected eof while reading (_ssl.c:2570)

CARegistrations.py

This file implements a CASheet class with contains information about the ID of the sheet to access. The following class method is called on it in the register serializer after the CA request is validated.

class CASheet:
    # Internal information about the sheet

    @classmethod
    def add_registration_details(cls, data):
        spreadsheet_id = cls.SPREADSHEET_ID
        body = {
            'values': [data]
        }
        result = cls.service.spreadsheets().values().append(
            spreadsheetId=spreadsheet_id, range=cls.RANGE_NAME, valueInputOption=cls.value_input_option, body=body).execute()

What can be the possible cause of this error and how should I resolve this indeterministic behavior?

Back to Top