django-celery: OperationalError: (2006, 'MySQL server has gone away')

When ever i restart my MySql celeryd and celerycam throw OperationalError: (2006, ‘MySQL server has gone away’) .

    Traceback (most recent call last):
      File "/home/webapp/webapp/../vendor/djcelery/schedulers.py", line 204, in sync
        with commit_on_success():
      File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
        return self.gen.next()
      File "/home/webapp/webapp/../vendor/djcelery/db.py", line 56, in commit_on_success
        with transaction.atomic(using):
      File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py", line 280, in __enter__
        connection.set_autocommit(False)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 333, in set_autocommit
        self._set_autocommit(autocommit)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 460, in _set_autocommit
        self.connection.autocommit(autocommit)
    OperationalError: (2006, 'MySQL server has gone away')

Is it a bug ?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 24 (5 by maintainers)

Commits related to this issue

Most upvoted comments

👍

I can update, that this is not an issue of django-celery project. I encounter this with Django 1.8.13 and Celery 3.1.23, not using django-celery at all. To get rid of the error, i force a

django.db.connection.close()

inside my Celery job before starting a possibly-long-running process. Django then re-opens the connection on the nearest database request. Another workaround can be implementing a decorator, that would attempt to execute a function and catch the “MySQL server has gone away” exception. If caught, then close the connection and try again. I use this approach to re-establish lost sftp connections, and i think this should work for reconnecting to MySQL.

In case it helps anyone else, one workaround for this is to disable Django’s persistent connections by setting ‘CONN_MAX_AGE’ to zero in the django settings.py file.