# Buggy code
def _get_connection(self, write=False):
    """Prepare the connection for action.

    Arguments:
        write (bool): are we a writer?
    """
    if self._connection is not None:
        return
    try:
        self._connection = cassandra.cluster.Cluster(
            self.servers,
            port=self.port,
            auth_provider=self.auth_provider,
            **self.cassandra_options,
        )
        self._session = self._connection.connect(self.keyspace)

        # We're forced to do concatenation below, as formatting would
        # blow up on superficial %s that'll be processed by Cassandra
        self._write_stmt = cassandra.query.SimpleStatement(
            Q_INSERT_RESULT.format(table=self.table, expires=self.cqlexpires),
        )
        self._write_stmt.consistency_level = self.write_consistency

        self._read_stmt = cassandra.query.SimpleStatement(
            Q_SELECT_RESULT.format(table=self.table),
        )
        self._read_stmt.consistency_level = self.read_consistency

        if write:
            # Only possible writers "workers" are allowed to issue
            # CREATE TABLE.  This is to prevent conflicting situations
            # where both task-creator and task-executor would issue it
            # at the same time.

            # Anyway; if you're doing anything critical, you should
            # have created this table in advance, in which case
            # this query will be a no-op (AlreadyExists)
            self._make_stmt = cassandra.query.SimpleStatement(
                Q_CREATE_RESULT_TABLE.format(table=self.table),
            )
            self._make_stmt.consistency_level = self.write_consistency

            try:
                self._session.execute(self._make_stmt)
            except cassandra.AlreadyExists:
                pass

    except cassandra.OperationTimedOut:
        # a heavily loaded or gone Cassandra cluster failed to respond.
        # leave this class in a consistent state
        if self._connection is not None:
            self._connection.shutdown()  # also shuts down _session

        self._connection = None
        self._session = None
        raise  # we did fail after all - reraise


# Traceback (AttributeError)
[2020-06-02 21:52:19,592: ERROR/MainProcess] Task add[4bd528a0-c1ba-456c-b74e-7dee054616bb] raised unexpected: AttributeError("'NoneType' object has no attribute 'execute'")
Traceback (most recent call last):
File "/home/bx/project/celery/celery/app/trace.py", line 480, in trace_task
uuid, retval, task_request, publish_result,
File "/home/bx/project/celery/celery/backends/base.py", line 158, in mark_as_done
self.store_result(task_id, result, state, request=request)
File "/home/bx/project/celery/celery/backends/base.py", line 443, in store_result
request=request, **kwargs)
File "/home/bx/project/celery/celery/backends/cassandra.py", line 201, in _store_result
self._session.execute(self._write_stmt, (
AttributeError: 'NoneType' object has no attribute 'execute'

# Fixed code
def _get_connection(self, write=False):
    """Prepare the connection for action.

    Arguments:
        write (bool): are we a writer?
    """
    if self._session is not None:
        return
    self._lock.acquire()
    try:
        if self._session is not None:
            return
        self._cluster = cassandra.cluster.Cluster(
            self.servers,
            port=self.port,
            auth_provider=self.auth_provider,
            **self.cassandra_options,
        )
        self._session = self._cluster.connect(self.keyspace)

        # We're forced to do concatenation below, as formatting would
        # blow up on superficial %s that'll be processed by Cassandra
        self._write_stmt = cassandra.query.SimpleStatement(
            Q_INSERT_RESULT.format(table=self.table, expires=self.cqlexpires),
        )
        self._write_stmt.consistency_level = self.write_consistency

        self._read_stmt = cassandra.query.SimpleStatement(
            Q_SELECT_RESULT.format(table=self.table),
        )
        self._read_stmt.consistency_level = self.read_consistency

        if write:
            # Only possible writers "workers" are allowed to issue
            # CREATE TABLE.  This is to prevent conflicting situations
            # where both task-creator and task-executor would issue it
            # at the same time.

            # Anyway; if you're doing anything critical, you should
            # have created this table in advance, in which case
            # this query will be a no-op (AlreadyExists)
            make_stmt = cassandra.query.SimpleStatement(
                Q_CREATE_RESULT_TABLE.format(table=self.table),
            )
            make_stmt.consistency_level = self.write_consistency

            try:
                self._session.execute(make_stmt)
            except cassandra.AlreadyExists:
                pass

    except cassandra.OperationTimedOut:
        # a heavily loaded or gone Cassandra cluster failed to respond.
        # leave this class in a consistent state
        if self._cluster is not None:
            self._cluster.shutdown()  # also shuts down _session

        self._cluster = None
        self._session = None
        raise  # we did fail after all - reraise
    finally:
        self._lock.release()