﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34666	"Mysql issue using afirst ""The client was disconnected by the server because of inactivity"""	Bernardo Tavares	nobody	"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""''
"	Bug	closed	Database layer (models, ORM)	4.2	Normal	needsinfo	async, afirst, mysql, inactivity		Unreviewed	0	0	0	0	0	0
