sidorares / sidorares/node-mysql2

Query stops when the app is wrapped with async wrapper

Open
#2,255 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It can only be reproduced with the following usage.

I have a cli application that is using mysql2/promise

const app = new AppController(...)//...takes in all the db stuffs
(async () => {
  tempDb = createPool({
    host: process.env.TEMP_MYSQL_HOST,
    user: process.env.TEMP_MYSQL_USER,
    password: process.env.TEMP_MYSQL_PASS,
    database: process.env.TEMP_MYSQL_DB,
  });
  
  console.log(await tempDb.query("SELECT 1"); // this will stop the app from running and exited 0(zero)
  await app.run();
})();

It will only work if I use TSC to compile the app into commonjs with no async await but only promise by writing as

const app = new AppController(...)//...takes in all the db stuffs
await app.run(); //queries inside will work and returns correctly

May I know why?

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 mysql2/promise entry point and reproduce the supplied CLI example, comparing the async-wrapper and compiled CommonJS cases. Trace the query call and process lifetime around app.run; done when the query and app.run complete instead of the process exiting early, with a regression test covering the reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js, typescript
Domain
backend, cli, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.