sidorares / sidorares/node-mysql2

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

Open
#3,722 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs investigation
Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the connection.execute({sql, nestTables: true}) entry point and reproduce the JSON.stringify(results) failure using the provided query. Inspect how Timeout and TimersList objects enter the returned results; done when the same query results can be serialized without the circular reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.