When a celery task fails, during a softTimeLimitException, the application tries to roll back the session and log the failed task. The issue is that a _finalize_fairy exception Exception during reset or similar occurs. This could be a result of Python attempting to roll back a session that SQLAlchemy has already closed. Therefore, the tasks fail without any log. What is your suggestion in this case? Is it possible to check if the transaction exists in the first try block and then roll back it? How can I modify the second try and convert it to an independent block? I mean if there is a problem with the existing transaction, I make a new one and then commit it?
try: # roll back transaction db.transaction.rollback() except Exception: log(message) return try: # log failed task task = FailedModel(id=req_id, reason=reason) task.save() db.transaction.commit() except Exception: log(message)