Connector.close() + pool.end() leaks TLS sockets for in-flight queries
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- mysql, node.js, typescript
- 領域
- backend, databases, networking
調査の方向性
まず dist/mjs/socket.js:107 と dist/mjs/connector.js:148 周辺のコネクタのライフサイクルを読み、次に mysql2/lib/base/connection.js:64 および stack trace に示されているプール接続の経路と比較します。並行する SELECT SLEEP(2) クエリでシャットダウンシーケンスを再現し、完了したシャットダウンによって実行中のクエリに対するコネクタの TLSWRAP ハンドルが残らないことを確認します。
索引モデルが issue の本文から書いたものです。
説明
Bug Description
Connector.close() followed by pool.end() leaves TLS sockets alive for any mysql2 pool connection that was mid-query when shutdown began. The leak is 1:1 with the number of in-flight queries: with 5 concurrent SELECT SLEEP(2) queries running, whyIsNodeRunning() 500 ms after shutdown reports 5 TLSWRAP handles still pinning the event loop. Their stacks root at:
@google-cloud/cloud-sql-connector/dist/mjs/socket.js:107 — tls.connect(socketOpts)
@google-cloud/cloud-sql-connector/dist/mjs/connector.js:148 — getSocket(...)
mysql2/lib/base/connection.js:64 — this.stream = opts.config.stream(opts)
Once the socket count grows past a few (e.g. a serving process handling concurrent traffic at the moment SIGTERM arrives), the process can fail to exit on graceful shutdown, relying on a forced kill.
Reproduces on both @google-cloud/[email protected] and @google-cloud/[email protected] (latest as of filing).
Suspected cause: CloudSQLInstance.close() destroys sockets in this.sockets, but pool connections that are currently checked out (mid-query) don't appear to be tracked there. Additionally, Connector.close() is synchronous but internally fire-and-forget (instance.promise.then(inst => inst.close())), so caller code that immediately follows close() with pool.end() races the microtask that actually destroys instance sockets.
Example code (or command)
import mysql from 'mysql2/promise';
import { AuthTypes, Connector } from '@google-cloud/cloud-sql-connector';
import whyIsNodeRunning from 'why-is-node-running';
const INSTANCE_CONNECTION_NAME = 'TODO: your-project:region:instance';
const DATABASE = 'TODO: your-database';
const USER = 'TODO: your-iam-user';
async function main() {
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName: INSTANCE_CONNECTION_NAME,
authType: AuthTypes.IAM,
});
const pool = mysql.createPool({
...clientOpts,
user: USER,
database: DATABASE,
});
// Fire concurrent in-flight queries without awaiting — simulates request
// handlers still running when shutdown begins.
const queries = Array.from({ length: 5 }, (_, i) =>
pool.query('SELECT SLEEP(2), ?', [i]).catch(() => {})
);
// Let the queries actually open TLS sockets.
await new Promise((r) => setTimeout(r, 100));
await pool.end();
connector.close();
// Let libuv settle (collect deactivated timers, flush pending writes).
await new Promise((r) => setTimeout(r, 500));
whyIsNodeRunning();
await Promise.allSettled(queries);
}
main();
Stacktrace
Output of whyIsNodeRunning() 500 ms after pool.end() + connector.close():
% node temp.direct.mjs
...
# TLSWRAP
node_modules/.pnpm/@[email protected]/node_modules/@google-cloud/cloud-sql-connector/dist/mjs/socket.js:107 - const tlsSocket = tls.connect(socketOpts);
node_modules/.pnpm/@[email protected]/node_modules/@google-cloud/cloud-sql-connector/dist/mjs/connector.js:148 - const tlsSocket = getSocket({
node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/base/connection.js:64 - this.stream = opts.config.stream(opts);
node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/base/pool_connection.js:7 - super(options);
node_modules/.pnpm/[email protected]/node_modules/mysql2/lib/pool_connection.js:5 - class PoolConnection extends BasePoolConnection {
...
# FILEHANDLE
(unknown stack trace)
# TTYWRAP
temp.direct.mjs:40 - console.log('shutting down while queries are in flight...');
# Timeout
temp.direct.mjs:46 - await new Promise((r) => setTimeout(r, 500));
temp.direct.mjs:46 - await new Promise((r) => setTimeout(r, 500));
How to reproduce
npm init -y && npm pkg set type=modulenpm i @google-cloud/[email protected] [email protected] why-is-node-running- Save the example code above as
repro.mjs, filling inINSTANCE_CONNECTION_NAME,DATABASE,USER. - Ensure
gcloud auth application-default loginhas been run and the IAM user has access to the instance. node repro.mjs- Observe
whyIsNodeRunning()reports NTLSWRAPhandles rooted at@google-cloud/cloud-sql-connector/dist/mjs/socket.js— one per in-flight query that hadn't completed whenpool.end()was called.
Environment details
- OS: MacOS 15.7.4 ARM (M2 Pro)
- Node.js version: v20.20.0 and v24.13.0
- pnpm version: 10.22.0
@google-cloud/cloud-sql-connectorversion: 1.10.0mysql2version: 3.16.3
Steps to reproduce
- Create a
Connectorand amysql2pool per the package README. - Issue several concurrent queries without awaiting them.
- Before the queries resolve, call
await pool.end()andconnector.close(). - Wait long enough (≥ 500 ms) for libuv to collect any handles that were legitimately cleared.
- Call
whyIsNodeRunning()(or otherwise inspect the event loop). - Expected: no
TLSWRAPhandles from the connector. Actual: one leakedTLSWRAPper in-flight query at shutdown time.
- 主要言語
- TypeScript
- スター
- 99
- フォーク
- 18
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 20
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
GoogleCloudPlatform/cloud-sql-nodejs-connector のほかの issue
-
flakybot: issue priority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
GoogleCloudPlatform/cloud-sql-nodejs-connector#658 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
GoogleCloudPlatform/cloud-sql-nodejs-connector#656 ·
メンテナーはふだん 1 日以内に返信
-
flakybot: flaky flakybot: issue priority: p2 type: bug
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
GoogleCloudPlatform/cloud-sql-nodejs-connector#657 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Cloud Build Failure Reporter: node-integration-test-nightly (cloud-sql-connector-testing) failedオープン
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
GoogleCloudPlatform/cloud-sql-nodejs-connector#641 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 半日 初心者へのやさしさ 35/100
GoogleCloudPlatform/cloud-sql-nodejs-connector#634 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
GoogleCloudPlatform/cloud-sql-nodejs-connector の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
メンテナーはふだん 1 日以内に返信
-
Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
bug P3
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信