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/", line 47, in inner
response = get_response(request)
File "/opt/venv/lib/python3.7/site-packages/django/core/handlers/", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/venv/lib/python3.7/site-packages/django/views/decorators/", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/django/views/generic/", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/rest_framework/", line 509, in dispatch
response = self.handle_exception(exc)
File "/opt/venv/lib/python3.7/site-packages/rest_framework/", line 469, in handle_exception
File "/opt/venv/lib/python3.7/site-packages/rest_framework/", line 480, in raise_uncaught_exception
raise exc
File "/opt/venv/lib/python3.7/site-packages/rest_framework/", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/app/ca/", line 19, in post
File "/opt/venv/lib/python3.7/site-packages/rest_framework/", line 212, in save
self.instance = self.create(validated_data)
File "/app/ca/", line 63, in create
ca.whyChooseYou, ca.wereCa, ca.isChoosen, ca.registerations])
File "/app/scripts/", 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/", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/", line 932, in execute
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/", line 222, in _retry_request
raise exception
File "/opt/venv/lib/python3.7/site-packages/googleapiclient/", line 191, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/opt/venv/lib/python3.7/site-packages/", line 225, in request
File "/opt/venv/lib/python3.7/site-packages/httplib2/", line 1721, in request
conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,
File "/opt/venv/lib/python3.7/site-packages/httplib2/", line 1440, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/opt/venv/lib/python3.7/site-packages/httplib2/", line 1392, in _conn_request
response = conn.getresponse()
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/", line 1373, in getresponse
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/", line 319, in begin
version, status, reason = self._read_status()
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/http/", 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/", line 589, in readinto
return self._sock.recv_into(b)
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/", line 1071, in recv_into
return, buffer)
File "/nix/store/ik7s754pwxhiky396mjagzmjs1kp0wzq-python3-3.7.16/lib/python3.7/", line 929, in read
return, buffer)
ssl.SSLError: [SSL: KRB5_S_TKT_NYV] unexpected eof while reading (_ssl.c:2570)

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

    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