fix(database):并发写入资产时 SQLite 锁竞争导致 database is locked - #247
Open
zabcxky-123 wants to merge 1 commit into
Open
Conversation
并发调用 create_asset 时报错“更新资产失败: database is locked”。根因是 UpsertAssets 使用延迟事务:多个写入者先 SELECT 去重键、再升级写锁,同时升级时互相竞争。 修复(两层防护): 1. SQLite DSN 增加 _txlock=immediate,所有事务以 BEGIN IMMEDIATE 启动,在事务开始时就获取写锁; 2. DB 层新增写事务互斥(serialTx 包装),进程内写事务严格串行,提交/回滚后释放互斥锁,彻底消除进程内 SQLITE_BUSY。 改动仅限数据库事务层与一个并发回归测试,无其他变更。 验证: - 无修复时:TestUpsertAssetsConcurrentCreatesDoNotLock 失败(创建资产失败: database is locked) - 修复后:回归测试通过(10 轮),go test ./... 全部通过 - go test -race ./internal/database 通过
zabcxky-123
force-pushed
the
fix/sqlite-concurrent-asset-writes
branch
from
August 12, 2026 20:33
efa76ed to
a5c911d
Compare
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.
并发调用 create_asset 时报错“更新资产失败: database is locked”。根因是 UpsertAssets 使用延迟事务:多个写入者先 SELECT 去重键、再升级写锁,同时升级时互相竞争。
修复:
SQLite DSN 增加 _txlock=immediate,所有事务以 BEGIN IMMEDIATE 启动,在事务开始时就获取写锁;
DB 层新增写事务互斥(serialTx 包装),进程内写事务严格串行,提交/回滚后释放互斥锁,彻底消除进程内 SQLITE_BUSY。
改动仅限数据库事务层与一个并发回归测试,无其他变更。
验证:
无修复时:TestUpsertAssetsConcurrentCreatesDoNotLock 失败(创建资产失败: database is locked)
修复后:回归测试通过,go test ./... 全部通过
go test -race ./internal/database 通过
改动文件:
internal/database/database.go
internal/database/asset_test.go
internal/database/rbac.go
internal/database/vulnerability.go