1

I have a Django (v2.2.23) application connected to a MySQL (v5.7) database. Every now and then, for a few minutes at a time, all my DB queries start to fail with one of the following errors: "Unknown MySQL Error" (error code 2000), "InterfaceError", or "Malformed Packet" (error code 2027). The errors resolve themselves on their own after a few minutes, but then they pop up again a some time later (on average, every few hours). These errors happen seemingly at random and I haven't found any reliable way to reproduce them till now. There is nothing wrong with the code as far as I can see because when the queries do execute, they work as expected and give the correct results.

I have already made several attempts to fix the issue to no avail, including :-

This error is occuring both on local and staging environments.

In order to debug this further, I create a custom exception handler and logged the error as well as the last 3 SQL queries executed (which I got using connection.queries[-3:]).

The result is given below:

'EXCEPTION': InterfaceError(0, ''), 'LAST_QUERIES': [{'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'}, {'sql': None, 'time': '0.000'}]

Notice that for the second query, the value of 'sql' is coming as None. Is Django trying to execute a null query? Could this be the cause of the strange, intermittent errors?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    Sending none as a query can indeed play havoc with mysql as it expects a query to come across. You can confirm the correlation between the exception and the none query by logging these exceptions over a longer period of time and see if all have none as sql query. If so, then I would try to establish what query should be in the place of the none (if any). The error may not have anything to do with mysql - it may be related to python, django, or the python mysql connector. – Shadow Aug 20 '21 at 08:59
  • @Shadow thanks for your reply. Yes, I actually logged this for multiple occurences of this strange exception and in all the cases, the common feature seems to be the `None` value. And you're right that it might be the reason for the MySQL error. What I'm trying to figure out is why a `None` query is being sent, as the Django ORM should ideally translate my Django query into the equivalent SQL. In other instances (when this random error doesn't pop up) the query runs correctly and returns the expected results. – Bikramjeet Singh Aug 20 '21 at 11:35
  • I don't think anyone can tell you why none is passed as sql in the database query based on the information provided. You need to debug your code and understand what parameters you see in the python code before the exception occurs. You can also consider checking if the sql is none before making any database queries. – Shadow Aug 20 '21 at 11:59

0 Answers0