sidorares / sidorares/node-mysql2
JSON.stringify() on query results causes "Converting circular structure to JSON" error due to internal Timeout objects
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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