Skip to content

JSON.stringify() on query results causes "Converting circular structure to JSON" error due to internal Timeout objects #3722

@xquanluu

Description

@xquanluu

Environment
mysql2 version: 3.11.0
Node.js version: v20.19.0
Operating System: Debian 12
MySQL version: AWS RDS Aurora Server

Description
When calling JSON.stringify() on query results returned by connection.execute() with nestTables: true, a circular reference error occurs due to internal Timeout and TimersList objects being included in the result set.

It really rare happen, that make me really hard to debug and know what could be the problem.

My code is:

const mysql = require('mysql2');

const pool = mysql.createPool({
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'test',
  connectionLimit: 10
});

const sql = `SELECT * FROM applications app 
  LEFT JOIN webhooks AS ch ON app.call_hook_sid = ch.webhook_sid 
  WHERE app.application_sid = ?`;

async function reproduce() {
  const pp = pool.promise();
  try {
    const [results] = await pp.execute({sql, nestTables: true}, ['some-sid']);
    
    // This line causes the circular reference error
    console.log(JSON.stringify(results));
  } catch (err) {
    console.error(err.message);
  }
}

reproduce();

Error trace:

"message": "Converting circular structure to JSON\n --> starting at object with constructor 'Timeout'\n | property '_idlePrev' -> object with constructor 'TimersList'\n --- property '_idleNext' closes the circle",
"stack": "TypeError: Converting circular structure to JSON\n --> starting at object with constructor 'Timeout'\n | property '_idlePrev' -> object with constructor 'TimersList'\n --- property '_idleNext' closes the circle\n at JSON.stringify ()\n at lookupAppBySid (/home/admin/apps/jambonz-feature-server/node_modules/@jambonz/db-helpers/lib/lookup-app-by-sid.js:20:26)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async Object.retrieveApplication [as handle] (/home/admin/apps/jambonz-feature-server/lib/middleware.js:256:15)"

Please let me know if you need more info on this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions