fix(driver-sql): pre-create autonumber sequences table to avoid batch/tx deadlock#2537
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 8 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
…/tx deadlock Lazy creation of _objectstack_sequences on the first autonumber INSERT asked the pool for a second connection. Inside a /api/v1/batch transaction on SQLite (pool max=1) the only connection is already held, so the acquire blocked until 'Knex: Timeout acquiring a connection'. Postgres/MySQL hit the same pool exhaustion under concurrent cold first-writes. - initObjects now pre-creates the counter table outside any data transaction. - The lazy fallback runs its DDL on the caller's transaction on SQLite only (never on MySQL, where DDL would implicitly commit the caller's transaction). - Regression test reproduces the deadlock (fails on old code, passes now).
Harden the lazy ensureSequencesTable fallback: when the table is created on the caller's transaction (SQLite in-tx path), the create is commit-conditional — a rollback would drop it. Cache sequencesTableReady only when the DDL ran on a durable pooled connection (this.knex); on the in-tx path leave the flag unset so the next write cheaply re-verifies via hasTable instead of trusting a stale process-level flag. initObjects still sets it durably up front, so the hot path is unchanged.
…ection deadlock Turns the latent 'bare this.knex query while a transaction holds SQLite's only pooled connection' hang (surfacing as 'Knex: Timeout acquiring a connection') into an immediate, actionable error at the call site. - Track open transactions (beginTransaction++/commit+rollback--) with a WeakSet so the decrement is idempotent on double close. - assertBareKnexSafe(op): throws only in the danger combination (non-production + SQLite + an open transaction). No-op in production and on non-SQLite dialects — zero hot-path cost. - Wire it into ensureSequencesTable's bare-knex branch, catching the 'opened a tx but forgot to thread it through as parentTrx' regression. Adds sql-driver-sqlite-tx-guard.test.ts (11 cases: tx bookkeeping, guard fires on the deadlock shape, guard stays quiet on legitimate paths, and the production/non-sqlite/no-tx branch matrix).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
SQLite 下
POST /api/v1/batch创建含auto_number字段的对象会死锁(连接池 acquire 超时):根因
_objectstack_sequences计数表是在第一次 autonumber 写时懒创建的,走的是裸this.knex.schema.*—— 向连接池申请第二条连接。而/batch端点把整批操作包在一个ql.transaction(...)里(rest-server.ts:5637),SQLite 连接池上限硬编码为 1(knexClient_SQLite3.poolDefaults → { min:1, max:1 },better-sqlite3继承),那唯一的连接已被外层事务独占 → 第二条永远拿不到 → 超时。受
sequencesTableReady进程级一次性 flag 守护,所以只有进程启动后冷态的第一次 autonumber 写(若恰好走 batch/事务)才 100% 触发 —— 与报告的「已 seed 库重启后首个 batch 动作中招」完全吻合。不只 SQLite:Postgres/MySQL 池默认
max=10,单请求不死锁,但冷 flag 窗口内 ≥pool.max 个并发首写会同样池耗尽死锁。修复
initObjects在建表阶段(事务外、独立连接)预建计数表,数据写路径永不触发 DDL。验证
sql-driver-autonumber-tx.test.ts:预建断言 + batch 事务首写 + 冷态事务内首写(全部withTimeout抓死锁)。driver-sql套件 237 passed (27 files);DTS typecheck 通过。