[Django] #34666: Mysql issue using afirst "The client was disconnected by the server because of inactivity"

53 views
Skip to first unread message

Django

unread,
Jun 19, 2023, 10:19:20 AM6/19/23
to django-...@googlegroups.com
#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.

Django

unread,
Jun 19, 2023, 10:30:42 AM6/19/23
to django-...@googlegroups.com
#34666: Mysql issue using afirst "The client was disconnected by the server because
of inactivity"
-------------------------------------+-------------------------------------
Reporter: Bernardo Tavares | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: async, afirst, | Triage Stage:
mysql, inactivity | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Natalia Bidart):

I wonder if this is related to #33497...

Bernardo, do you think you could provide a minimal (but complete) example
to reproduce this issue? Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/34666#comment:1>

Django

unread,
Jun 19, 2023, 11:31:16 PM6/19/23
to django-...@googlegroups.com
#34666: Mysql issue using afirst "The client was disconnected by the server because
of inactivity"
-------------------------------------+-------------------------------------
Reporter: Bernardo Tavares | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: needsinfo

Keywords: async, afirst, | Triage Stage:
mysql, inactivity | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

> In my understanding, Django always opens a connection, executes a query
and then closes a connection.

Yes, if you're talking about request-response cycle and connections are
not persistent.

> 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.

As far as I'm aware, this has nothing to do with `afirst()`.

> Any help? Thank you in advance!

I don't think you've explained the issue in enough detail to confirm a bug
in Django and Trac is not a support channel. Please reopen the ticket if
you can debug your issue and provide details about why and where Django is
at fault. A small project that reproduces the issue will also help. If
you're having trouble debugging, see
TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/34666#comment:2>

Django

unread,
Jun 20, 2023, 5:36:24 AM6/20/23
to django-...@googlegroups.com
#34666: Mysql issue using afirst "The client was disconnected by the server because
of inactivity"
-------------------------------------+-------------------------------------
Reporter: Bernardo Tavares | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: async, afirst, | Triage Stage:
mysql, inactivity | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bernardo Tavares):

Replying to [comment:1 Natalia Bidart]:


> I wonder if this is related to #33497...
>
> Bernardo, do you think you could provide a minimal (but complete)
example to reproduce this issue? Thanks!

I'm using unicorn with wsgi. Sure!

--
Ticket URL: <https://code.djangoproject.com/ticket/34666#comment:3>

Django

unread,
Jun 20, 2023, 6:05:40 AM6/20/23
to django-...@googlegroups.com
#34666: Mysql issue using afirst "The client was disconnected by the server because
of inactivity"
-------------------------------------+-------------------------------------
Reporter: Bernardo Tavares | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: async, afirst, | Triage Stage:
mysql, inactivity | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bernardo Tavares):

Replying to [comment:2 Mariusz Felisiak]:


> > In my understanding, Django always opens a connection, executes a
query and then closes a connection.
>
> Yes, if you're talking about request-response cycle and connections are
not persistent.
>
> > 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.
>
> As far as I'm aware, this has nothing to do with `afirst()`.
>
> > Any help? Thank you in advance!
>
> I don't think you've explained the issue in enough detail to confirm a
bug in Django and Trac is not a support channel. Please reopen the ticket
if you can debug your issue and provide details about why and where Django
is at fault. A small project that reproduces the issue will also help. If
you're having trouble debugging, see
TicketClosingReasons/UseSupportChannels for ways to get help.

Understood. I will try to go deeper on this.
I opened a [forum discussion here](https://forum.djangoproject.com/t
/debugging-the-client-was-disconnected-by-the-server-because-of-
inactivity/21730) , if you can help me debug a bit further and explain why
this is not related with afirst() I would be grateful.

--
Ticket URL: <https://code.djangoproject.com/ticket/34666#comment:4>

Reply all
Reply to author
Forward
0 new messages