question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django Error: (2013, 'Lost connection to MySQL server during query') after running for "mysql wait_timeout" second

See original GitHub issue

In order to recreate this bug,i set mysql wait_timeout=10 and interactive_timeou=10 .

run server :

uvicorn mweb.asgi:application --host 0.0.0.0 --port 8000 --ws wsproto  --debug   

after 10s connect websocket,

ERROR: Exception in ASGI application
Traceback (most recent call last):
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/uvicorn/protocols/websockets/wsproto_impl.py", line 190, in run_asgi
    asgi = self.app(self.scope)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/uvicorn/middleware/debug.py", line 66, in __call__
    return self.app(scope)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/routing.py", line 58, in __call__
    return self.application_mapping[scope["type"]](scope)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/sessions.py", line 43, in __call__
    return self.inner(dict(scope, cookies=cookies))
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/sessions.py", line 141, in __call__
    return SessionMiddlewareInstance(scope, self)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/sessions.py", line 165, in __init__
    self.inner = self.middleware.inner(self.scope)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/middleware.py", line 31, in __call__
    inner_instance = self.inner(scope)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/routing.py", line 144, in __call__
    "kwargs": {**outer.get("kwargs", {}), **kwargs},
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/channels/routing.py", line 144, in __call__
    "kwargs": {**outer.get("kwargs", {}), **kwargs},
  File "./sales_ai_coach/base/consumer_base.py", line 100, in __init__
    u=User.objects.get(id=153)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 393, in get
    num = len(clone)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __len__
    self._fetch_all()
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/Users/xx/project/py3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')

but http work fine.

Django==2.1.3
channels==2.1.5
channels_redis==2.3.1
uvicorn==0.4.1
wsproto==0.12.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abersheerancommented, Dec 8, 2021

@MahmoudHigazy Any file that will be imported. I added it in settings.py.

1reaction
MahmoudHigazycommented, Dec 7, 2021

May be you can try https://aber.sh/articles/Django-automatic-reconnect/

import importlib

from django.conf import settings
from django.db.backends.utils import CursorWrapper


for name, config in settings.DATABASES.items():
    module = importlib.import_module(config["ENGINE"] + ".base")

    def ensure_connection(self):
        if self.connection is not None:
            try:
                with CursorWrapper(self.create_cursor(), self) as cursor:
                    cursor.execute("SELECT 1")
                return
            except Exception:
                pass

        with self.wrap_database_errors:
            self.connect()

    module.DatabaseWrapper.ensure_connection = ensure_connection

where should I add this code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Lost connection to MySQL server during query
Partially solved the problem by applying the following techniques: 1) Optimised a number of queries sent to the database by adding ...
Read more >
"MySQL server has gone away" error - Solution(s)
Error Code: 2013. Lost connection to MySQL server during query ... MySQL wait_timeout is the number of seconds the server waits for activity ......
Read more >
How to fix Error Code 2013 Lost connection to MySQL server
This error appears when the connection between your MySQL client and database server times out. Essentially, it took too long for the query...
Read more >
Lost connection to MySQL server during query - Google Groups
Greetings to everybody, using django 1.2.3 with celery 2.0.3 and django-celery 2.0.3 and mysql 5.1.41 on kubuntu 10.04, I receive the following error:...
Read more >
ERROR 1205 (HY000): Lock wait timeout exceeded
If a row is currently locked by query q1 running in transaction t1 ... ERROR 2013 (HY000): Lost connection to MySQL server during...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found