mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

Stack Overflow - Too much recursion in query function. #2484

Closed JacobSweeten closed 3 years ago

JacobSweeten commented 3 years ago

My Code:

async function checkUserIDExists(conn, ID)
{
    res = await conn.query("SELECT ID FROM User WHERE ID=?", [ID], (err, res, fields) => {
        if(err) { throw err; }
    });
    return res.length != 0;
}

Result:

image image

In the above debugger log, I attempted a connection to the API after issuing the debugger command "c" which causes checkUserIDExists() to be called. The "User exists" is correct and expected.

What is strange is that the function completes and so does its calling function, but some asynchronous function is still running, probably recursively, taking up memory and processing power.

Eventually, it crashes. With debugger: image

Without debugger: image

Thank you, Jacob Sweeten

dougwilson commented 3 years ago

Hi @JacobSweeten thank you for the report. I have not experience this myself using this module, and not sure what the cause would be. Can you provide a reproduction case that I can run and reproduce the issue you are experiencing? Please provide a full script and instructions for how to run it to see the issue. I can then attach a debugger to determine what is happening. I also see you are already comfortable using a debugger, so if you are not comfortable sharing the code to reproduce the issue, if you can put together a pull request for the fix, that would be appreciated as well!

JacobSweeten commented 3 years ago

My apologies. I misunderstood the problem. Turns out, I had a while loop with the wrong exit condition. I assumed the while loop wasn't even running because I forgot to add a return after a reject() call. Not sure why it resulted in recursion as while loops are the opposite of recursion, but whatever. Thank you for your assistance.

I figured it out by stepping a bunch of times through the debugger. Eventually, it came back to my while loop which I did not expect.