MySQL: query_timeout fallback KILL QUERY uses the handshake thread id, which is wrong behind a proxy
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 70/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- mariadb, mysql, typescript
调研方向
Start in src/connectors/mysql and trace the timeout handling that captures conn.threadId and calls killQuery; read the connection setup hook that runs SET SESSION max_execution_time for #465. Check how that hook can retrieve SELECT CONNECTION_ID() and preserve the existing fallback if it fails. Done means timeout kills target the backend connection through a handshake terminating proxy; run the MySQL connector tests covering timeout behavior.
由索引模型根据 Issue 内容生成。
描述
Summary
On MySQL, the client-side query_timeout fallback kills the wrong thread when DBHub connects through a proxy that answers the MySQL handshake itself (Teleport database access, and likely any MySQL-protocol proxy that terminates the handshake). KILL QUERY uses conn.threadId, which is the connection id from the proxy's greeting, not the backend's thread id.
Since #465 the server-side max_execution_time covers SELECTs in most cases, so this matters less than it did on 1.4.0. The fallback still exists for the cases #465 describes (limit cleared or raised mid-session), and there it either misses or hits an unrelated session that happens to have the same id.
Where
src/connectors/mysql (1.7.1 dist/mysql-*.js):
const threadId = conn.threadId;
...
} catch (error) {
if (isClientSideTimeout(error)) {
isConnectionPoisoned = true;
await this.killQuery(threadId); // KILL QUERY ${threadId}
}
mysql2 fills threadId from the initial handshake packet.
Reproduction
MySQL 8.0.46, a 30-line TCP proxy that rewrites the 4-byte connection id in the server greeting (as a terminating proxy does), DBHub 1.4.0 (before #465, so the fallback is the only mechanism), query_timeout = 5, readonly = true:
SELECT COUNT(*) FROM n a, n b, n c WHERE a.i + b.i + c.i > 0 -- n has 1000 rows
Result:
dbhub answered after 5.1s: "Query inactivity timeout"
processlist 15s later: id 29 | reader | 22 | executing | SELECT COUNT(*) FROM n a, n b, n c ...
general log: KILL QUERY 40001
proxy log: handshake: real thread id 29 -> client sees 40001
With 1.7.1 the same query stops at 5 s with maximum statement execution time exceeded, because of #465. The kill path itself is unchanged.
In production we saw this behind Teleport: one missed kill per timed-out query, a few hundred per week, with one query left running for 14 days.
Proxy used for the repro:
idproxy.py
import asyncio, itertools, struct
UP_HOST, UP_PORT = "mysql", 3306
fake_ids = itertools.count(40001)
def rewrite_handshake(pkt):
body = bytearray(pkt[4:])
end = body.index(0, 1) # protocol(1) + NUL-terminated server version
struct.pack_into("<I", body, end + 1, next(fake_ids))
return pkt[:4] + bytes(body)
async def pipe(r, w):
try:
while data := await r.read(65536):
w.write(data); await w.drain()
finally:
w.close()
async def handle(cr, cw):
ur, uw = await asyncio.open_connection(UP_HOST, UP_PORT)
hdr = await ur.readexactly(4)
pkt = hdr + await ur.readexactly(hdr[0] | hdr[1] << 8 | hdr[2] << 16)
cw.write(rewrite_handshake(pkt)); await cw.drain()
await asyncio.gather(pipe(cr, uw), pipe(ur, cw), return_exceptions=True)
async def main():
async with await asyncio.start_server(handle, "0.0.0.0", 3306) as srv:
await srv.serve_forever()
asyncio.run(main())
Suggested fix
Read the real id from the server once per new pool connection, in the same hook that already runs SET SESSION max_execution_time (#465), and use it for the kill:
SELECT CONNECTION_ID()
CONNECTION_ID() is evaluated by the backend, so it is correct behind any proxy. Falling back to conn.threadId only if that query fails keeps today's behaviour for direct connections. MariaDB has the same function.
Happy to send a PR if the approach works for you.
- 主要语言
- TypeScript
- 星标
- 4k
- 派生
- 370
- 平均合并
- 6 小时 28 分钟
- 30 天内合并 PR
- 41
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
bytebase/dbhub 的其他 Issue
-
execute_sql cannot create a SQLite trigger or a PostgreSQL BEGIN ATOMIC function: the statement is cut at the semicolons inside its body可能已有人在做 @goranai 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
SQLite: max_rows breaks LIMIT -1 OFFSET n with a syntax error可能已有人在做 @goranai 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Oracle: opt-in thick mode for servers that require Native Network Encryption (NJS-533 / ORA-12660)可能已有人在做 @gabriell-prado 于 3 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 25/100
维护者通常 1 天内回复
-
👉👉👉 Please share your use case to shape DBHub roadmap可能重新可做 @tianzhou 于 367 天前认领,目前没有进行中的 PR。 未关闭
bytebase/dbhub#146 · 27 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
相似的 Issue
-
Flaky test: selecting a portion just after inserting it can also match its Change and Delete buttons未关闭ready-for-agent
难度 2/5 1-3 小时 新手友好度 84/100
linagora/dcs-onlyoffice#172 ·
-
bug follow-up
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
nightly-tests
难度 2/5 1-3 小时 新手友好度 68/100
BlackBeltTechnology/pi-agent-dashboard#1226 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
danielmiessler/LifeOS#3028 ·
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 88/100