#34666: Mysql issue using afirst "The client was disconnected by the server 
because
of inactivity"
-------------------------------------+-------------------------------------
               Reporter:  Bernardo   |          Owner:  nobody
  Tavares                            |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  layer (models, ORM)                |       Keywords:  async, afirst,
               Severity:  Normal     |  mysql, inactivity
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I'm using Django 4.2 connected to a MySQL 8 database.
 I'm running asyncio for a function I really need to run asynchronously:

 {{{
 from django.db import connection
 import asyncio

 example_object = asyncio.run(example(request))
 connection.close()
 }}}


 {{{
 async def example(request):
     object = await ModelExample.objects.filter(example=example).afirst()
     return object
 }}}


 I'm having the following error:
 ''"The client was disconnected by the server because of inactivity. See
 wait_timeout and interactive_timeout for configuring this behavior"''

 Initially the work codes fine, the error only happens after a few hours
 (maybe 8h, like the default wait_timeout variable) after I restart the
 (nginx) server.
 It's a low traffic website so it's very possible the function is executed
 only once a day.

 In my understanding, Django always opens a connection, executes a query
 and then closes a connection.
 Is it not closing the connection when using ''"afirst()"''? This feels
 seems like a bug to me, or is something in the documentation I'm not
 getting.

 Any help? Thank you in advance!


 ----


 **Other things that I tried:**
 - Before doing the connection.close(), I would have an error like:
 ''"MySQL server has gone away"''
 - Adding close_old_connections() before asyncio.run. Don't understand why
 there is an inactive connection left open at all.
 - Increasing the wait_timeout value and interactive_timeout variables in
 my MySQL config file. I find it very strange that this had no impact at
 all but the ''"SHOW VARIABLES"'' command shows me they are indeed
 currently set to 31536000.
 - Then I thought that maybe the connection from Django is somehow
 independent of that and tried setting ''CONN_HEALTH_CHECKS'' option to
 True, in the hopes that "if the health check fails, the connection will be
 re-established without failing the request"
 - Changing the ''CONN_MAX_AGE'' from the default 0 to "None" in the Django
 settings file, which according to Django docs, means an unlimited
 persistent database connection, but then I would have a ''"Lost connection
 to MySQL server during query"''

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34666>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070188d406352b-0a033ab0-021d-45e1-9a9b-fa664649f534-000000%40eu-central-1.amazonses.com.

Reply via email to