sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.05k stars 612 forks source link

Warning: got packets out of order. Expected 10 but received 0 #1334

Open chriscousinsdev opened 3 years ago

chriscousinsdev commented 3 years ago

Error has been occuring for a while, happens after a while where the bot will error. I recently upgraded from mysql to mysql2 to see if it would fix the problem, still nothing. When it errors nothing can be read from the database until the bot is restarrted

Flydexo commented 3 years ago

I have the same issue but only a long time (10 hours) after launching the server. `const connection = mysql.createPool({ host : 'db', user : 'test', password : '', database : '' });

module.exports.connection = connection;`. I set the host to db because I use docker-compose. I export this connection/pool and I use it in my code with connection.query. But I have the same error. Does someone know why ?

bttger commented 3 years ago

Possible duplicates https://github.com/sidorares/node-mysql2/issues/528 https://github.com/sidorares/node-mysql2/issues/653 https://github.com/sidorares/node-mysql2/issues/778

ElijahShadbolt commented 2 years ago

I have the same warning message. I'm testing a possible cause, but in case I forget to post a solution: I suspect it might be because I'm keeping one global variable for the connection, so maybe something is going wrong with my node http server calling execute and query asynchronously, while another request is using the same connection.

gramakri commented 2 years ago

I am able to reproduce this when using close() instead of end() .

Here's a simple example using promises:

'use strict';
const mysql2p = require('mysql2/promise');

async function bugp() {
    let db = await mysql2p.createConnection({
        host: '172.18.0.2',
        user: 'root',
        password: 'helloworld'
    });

    await db.close(); // await db.end() makes it work
}
(async () => await bugp())();

Running the above with MySQL 8.0.27 (ubuntu 20.04), prints "Warning: got packets out of order. Expected 3 but received 0". If you replace db.close() with db.end() it without warning. Admittedly, I have no clue where I got db.close from since docs only refer to db.end()

If it's interesting, the equivalent callback code causes an error. Maybe that's why the above code is erroring differently:

'use strict';
const mysql2 = require('mysql2');
function bug(cb) {
    let db = mysql2.createConnection({
        host: '172.18.0.2',
        user: 'root',
        password: 'helloworld'
    });

    db.close(cb);
}

bug(() => {});

The above prints:

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:371:5)
    at _write (node:internal/streams/writable:319:11)
    at Socket.Writable.write (node:internal/streams/writable:334:10)
    at Connection.write (/home/girish/tmp/node_modules/mysql2/lib/connection.js:245:32)
    at Connection.writePacket (/home/girish/tmp/node_modules/mysql2/lib/connection.js:294:12)
    at ClientHandshake.sendCredentials (/home/girish/tmp/node_modules/mysql2/lib/commands/client_handshake.js:69:16)
    at ClientHandshake.handshakeInit (/home/girish/tmp/node_modules/mysql2/lib/commands/client_handshake.js:142:12)
    at ClientHandshake.execute (/home/girish/tmp/node_modules/mysql2/lib/commands/command.js:45:22)
    at Connection.handlePacket (/home/girish/tmp/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/home/girish/tmp/node_modules/mysql2/lib/connection.js:85:12)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (/home/girish/tmp/node_modules/mysql2/lib/connection.js:236:12)
    at Connection._handleFatalError (/home/girish/tmp/node_modules/mysql2/lib/connection.js:167:10)
    at Connection._handleNetworkError (/home/girish/tmp/node_modules/mysql2/lib/connection.js:180:10)
    at /home/girish/tmp/node_modules/mysql2/lib/connection.js:247:14
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END',
  fatal: true
}
snowballrandom commented 2 years ago

For me I got a similar message after switching from mysql to mysql2. The issue for me was changing the following:

var db = mysql.createPool(dbConfig);
db.getConnection(function(err, connection) {
         Changed this line:
          mysql -> connection.destroy();
          To this line:
          mysql2 -> db.releaseConnection(connection); 
})
OnZeng commented 2 years ago

[](url)

jchapelle commented 2 years ago

+1

misa198 commented 2 years ago

I tried increasing max_allowed_packet to 500MB = 500 1024 1024 and it seems the problem is solved.

huycn commented 1 year ago

This happens for us when using connection pool and server sends ER_CLIENT_INTERACTION_TIMEOUT packet (new from MySQL 8.0.24).

To reproduce, set wait_timeout and interactive_timeout on server to something very short, e.g. 5s. After a query is made, wait for 5s, the warning message will pop up.

The good news is it's only an annoying but harmless warning, the connection is correctly released after.

sanchexas commented 1 year ago

This problem disappeared after I started closing the connection after getting the result from the database. Got result? - resolve(result); connection.end(); Got an error? - reject(error); connection.end()

sureshvv commented 6 months ago

For me I got a similar message after switching from mysql to mysql2. The issue for me was changing the following:

var db = mysql.createPool(dbConfig);
db.getConnection(function(err, connection) {
         Changed this line:
          mysql -> connection.destroy();
          To this line:
          mysql2 -> db.releaseConnection(connection); 
})

connection.release() instead of connection.destroy()