Opened 7 weeks ago
Last modified 7 weeks ago
#35812 closed Bug
2006 'MySQL server has gone away' error after idle period in Django app polling Kafka — at Initial Version
Reported by: | Adithya Kaade Arvind | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Normal | Keywords: | 2006 'MySQL server has gone away' |
Cc: | Adithya Kaade Arvind | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Issue Summary:
In a custom Django app, a 2006: 'MySQL server has gone away' error occurs when querying the MySQL database after an idle period. The app polls messages from a Kafka topic with low traffic, so after a long idle period, the first query to the MySQL database fails with this error. However, immediately after the failure, subsequent queries succeed without issue.
Steps to Reproduce:
Create a custom Django app using python manage.py startapp.
Inside the app, implement an infinite loop to poll messages from a Kafka topic.
Messages are rarely inserted into the Kafka topic.
After a long idle period, when a new message is received and the app tries to query the MySQL database, the first query fails with the error (2006, 'Server has gone away').
Immediately after this failure, subsequent queries succeed without any errors.
Expected Behavior:
When CONN_HEALTH_CHECKS = True is set, the connection to the database should be checked and re-established before the query is executed, preventing the error.
Actual Behavior:
The first query fails with a 2006: 'MySQL server has gone away' error after an idle period.
Setting CONN_HEALTH_CHECKS = True did not resolve the issue.
Setting CONN_MAX_AGE = 30 also did not prevent the error.
All subsequent queries after the first failure succeed immediately without issues.
Environment:
Django Version: 4.2.13
Python Version: 3.10.14
MySQL Version: 8.0
Additional Information:
The issue only occurs for the first query after an idle period; subsequent queries work as expected.
Tested with both CONN_HEALTH_CHECKS and CONN_MAX_AGE settings, but the issue persists.