2023-04-16

MySQL query returning undefined if there are too many rows

I have a MySQL connection on a website that has been working fine for a long time, but recently started having a problem. I discovered that this only happens when I surpass about 150 rows which is really small. There is no large or abnormal data and only about 20 columns. I have an async function like this:

var mysql = require('mysql');
var connection = mysql.createPool({...});
return await new Promise((resolve, reject) => pool.query(sql, params, (err, results) => {
        if (err) {
            reject(err);
        } else {
            resolve(results);
        }
}));

I always await the results. When the SQL returns about 150 rows, it works fine and I can log all the results to the console. As soon as I raise the limit a little further, the results are undefined. It's not throwing any errors.

I made sure the queries work in the workbench. I have tried different browsers and incognito mode. I also tried without the pool:

var connection = mysql.createConnection({...});
const { promisify } = require('util');
const query = promisify(connection.query).bind(connection);
var results = await query(sql, params);
connection.end();
return results;

This approach has the same symptoms. After searching quite a bit, I also tried this attempt to keep the promise alive in case that was the problem:

process.exitCode = 1;
const i = setInterval(() => { /* do nothing but prevent node process from exiting */  }, 1000);
try{...}
finally{
    clearInterval(i);
}
process.exitCode = 0;

The query also runs quickly, less than a second. Any ideas on what could be causing this?

Update: I'm still trying to track down the root cause. I tried logging the results of the query before the promise is resolved. With 150 rows, it is logged and returned as expected.

With a few more rows, the row count is still logged. Meaning, the query completed and the promise is about to be resolved. Here's where it gets weird. Sometimes it returns the results, sometimes it doesn't. When it returns undefined, it doesn't even run any code in the function outside the try catch block. It's like the code just gives up waiting for a response from this function and moves on without it.



No comments:

Post a Comment