[CONJS-193] Wrong error returned "Cannot read properties of undefined… … (reading 'charset')" when error during handshake Created: 2022-04-20  Updated: 2022-04-26  Resolved: 2022-04-26

Status: Closed
Project: MariaDB Connector/node.js
Component/s: other
Affects Version/s: 3.0.0
Fix Version/s: 3.0.1

Type: Bug Priority: Major
Reporter: markus makela Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None
Environment:

MariaDB 10.2.7



 Description   

The following code throws an exception with MariaDB 10.2 whereas with 10.6 it just works.

[
  { val: 1n },
  meta: [
    ColumnDef {
      _stringParser: [StringParser],
      collation: [Collation],
      columnLength: 1,
      columnType: 8,
      flags: 129,
      scale: 0,
      type: 'BIGINT'
    }
  ]
]
undefined
/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/io/packet-output-stream.js:48
    this.encoding = collation.charset;
                              ^
 
TypeError: Cannot read properties of undefined (reading 'charset')
    at PacketOutputStream.changeEncoding (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/io/packet-output-stream.js:48:31)
    at EventEmitter.emit (node:events:532:35)
    at Connection.<anonymous> (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/connection.js:595:21)
    at Object.onceWrapper (node:events:640:26)
    at Handshake.emit (node:events:520:28)
    at Handshake.throwError (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/cmd/command.js:90:10)
    at Handshake.parseHandshakeInit (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/cmd/handshake/handshake.js:54:19)
    at PacketInputStream.receivePacketBasic (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/io/packet-input-stream.js:76:9)
    at PacketInputStream.onData (/home/markusjm/sqltest/nodejs/node_modules/mariadb/lib/io/packet-input-stream.js:139:20)
    at Socket.emit (node:events:520:28)

const mariadb = require("mariadb")
const pool = mariadb.createPool({
    host: '127.0.0.1',
    port: 3000,
    user:'maxuser',
    password: 'maxpwd',
    connectionLimit: 100
});
async function asyncFunction() {
    let conn;
    try {
        conn = await pool.getConnection();
        const rows = await conn.query("SELECT 1 as val");
        console.log(rows); //[ {val: 1}, meta: ... ]                                                                                                                                                                                                                                      
        const res = await conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
        console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }                                                                                                                                                                                                           
    } catch (err) {
        throw err;
    } finally {
        if (conn) return conn.end();
    }
}
asyncFunction().then(console.log, console.log)



 Comments   
Comment by Diego Dupin [ 2022-04-25 ]

It doesn't seems to be related with server version 10.2 at all.
Connector is throwing an exception because of wrong handshake, but return a wrong error "Cannot read properties of undefined (reading 'charset')" in place of initial error, if receiving no handshake / or an handshake packet with a 0x00 charset byte.

This part will be corrected with https://github.com/mariadb-corporation/mariadb-connector-nodejs/commit/5121d6cf1b32d25167ef9f385a0c78cf6554a4d3
Could you confirm markus makela that server was not up or something like that ?

Comment by markus makela [ 2022-04-26 ]

Testing it again with the 10.2.7 image does show that it now works when connecting directly as well as through MaxScale. The server was most definitely up when I tested it as it returned a resultset before throwing that exception.

Comment by Diego Dupin [ 2022-04-26 ]

thanks markus, i will then close the task changing to label to wrong error returned "Cannot read properties of undefined… … (reading 'charset')" when error during handshake

Generated at Thu Feb 08 03:23:26 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.