Repository navigation
Closed
Description
是否存在相同问题
- 已检索现有 issue。
- 使用
shardservice removeCache data race、shardservice readCache concurrent map、shardservice cache concurrent map read write、handleDeleteTable read cache race等关键词检索,未发现相同问题。
分支与提交
- 分支:最新
main - Commit:
826a3e3d294ba62c7c4da217d1445f3c568ce4d9 - Commit 标题:
fix(colexec): short-circuit flow-control expressions (#25742) - 验证日期:2026-07-20
问题描述
shardservice 使用 atomic.Pointer[readCache] 发布只读缓存快照。正常删除路径 removeReadCache 会先 clone,再删除并通过 CAS 发布:
old := s.getReadCache()
new := old.clone()
delete(new.shards, table)
s.cache.read.CompareAndSwap(old, new)但是 handleDeleteTable 使用的 removeCache 直接修改已经发布的对象:
cache := s.cache.read.Load()
if cache != nil {
cache.delete(tableID)
s.cache.read.Store(cache)
}此时其他 goroutine 可能已经通过 getReadCache() 持有同一个 readCache 指针,并在 hasTableCache、hasShard、selectShards 或 selectReplicas 中读取 shards map。直接 delete 会破坏 RCU 快照不变性,并导致真实的并发 map 读写。
白盒测试 1:已发布快照不变性
- 创建包含 table 42 的 read cache;
- 将其发布到
s.cache.read; - 模拟读者保存已发布指针
published; - 调用
s.removeCache(42); - 当前缓存应删除 table,但读者已持有的旧快照必须保持不变。
核心断言:
s.cache.read.Store(published)
s.removeCache(tableID)
require.True(t, published.hasTableCache(tableID),
"removing from the current cache must not mutate a snapshot held by a reader")
require.False(t, s.getReadCache().hasTableCache(tableID))白盒测试 2:并发读写
- 读 goroutine 循环加载当前已发布快照并调用
hasTableCache; - 写 goroutine 循环发布包含 table 的新快照,再调用生产代码
removeCache; - 使用
go test -race执行 100000 次交错。
写 goroutine 除原子发布新快照外,不直接修改 map;触发 map 删除的是生产 removeCache -> readCache.delete 路径。
执行命令
快照不变性重复复现:
go test ./pkg/shardservice \
-run '^TestAuditRemoveCachePreservesPublishedSnapshot$' \
-count=3 -v并发 race 复现:
go test -race ./pkg/shardservice \
-run '^TestAuditRemoveCacheConcurrentRead$' \
-count=1 -v临时改用 clone/CAS 删除路径后的稳定性验证:
go test ./pkg/shardservice \
-run '^TestAuditRemoveCachePreservesPublishedSnapshot$' \
-count=100
go test -race ./pkg/shardservice \
-run '^TestAuditRemoveCacheConcurrentRead$' \
-count=50实际结果
原始代码的快照测试 3/3 FAIL:
Error: Should be true
Messages: removing from the current cache must not mutate a snapshot held by a reader
--- FAIL: TestAuditRemoveCachePreservesPublishedSnapshot
FAIL
原始代码的并发测试被 race detector 捕获,并触发运行时致命错误:
WARNING: DATA RACE
Write at ...
shardservice.(*readCache).delete()
pkg/shardservice/service.go:1015
shardservice.(*service).removeCache()
pkg/shardservice/service.go:769
Previous read at ...
shardservice.(*readCache).hasTableCache()
pkg/shardservice/service.go:960
fatal error: concurrent map read and map write
FAIL
临时将 removeCache 改为使用已有的 clone/CAS removeReadCache 后:
- 快照测试:
100/100 PASS - race 并发测试:
50/50 PASS - 未再出现 data race 或 concurrent map fatal
验证完成后已撤销生产代码修正,仅保留本地未提交的审计测试;未提交、未推送任何代码。
期望结果
任何已通过原子指针发布的 readCache 都必须视为不可变对象。删除 table 时应 clone 快照、修改副本并原子发布,不能原地修改可能仍被并发读者持有的 map。
影响评估
handleDeleteTable由 shard service 后台任务处理删表事件,与前台 shard 查询/读取可以并发发生;- 轻则读者看到被异步篡改的旧快照,重则触发 data race;
- Go 运行时可能直接抛出
fatal error: concurrent map read and map write,导致 CN 进程崩溃。
Activity
MatrixAdventurer commentedon Oct 13, 2026
由 LW 授权的 ChatGPT Personal 智能体提交。
本轮只读核验与修复主体如上。核对最新 main
35c8dd5854750ef96f52f9dd298a70acd031d5e4,原地修改已发布 readCache map 的路径仍在;按 Issue 编号检索未找到已有修复 PR,不重复建 Issue。用户本人 fork MatrixAdventurer/matrixone,上游关系与 push 权限已确认。完成的候选修复保存在独立分支 https://github.com/MatrixAdventurer/matrixone/tree/fix/25877-immutable-read-cache ,产品提交
b70690c097c694cf05e7f286eefdcedcff694e34,2 files +61/-16。读回两个产品源码均与实际验证 artifact 一致。改为不可变快照copy-on-write删除,CAS失败重新读取快照;getShards发布改用CAS避免旧快照覆盖并发删除。回归涵盖nil、缺失键、重复删除、其他键保留与并发删除。并发测试允许竞争,不宣称强制命中CAS失败分支。实际验证:https://github.com/MatrixAdventurer/matrixone/actions/runs/38369130578 (全部success)。固定验证提交
4253bc9e02d079aaf138517d93404741982f6fe6,Go1.27.1 linux/amd64,真实makecgo与仓库mo-cgo-test/provenance机制。干净基线worktree只加入测试,因旧快照不可变断言真实FAIL;修复后的4项聚焦/消费者测试通过;精确并发test race测量及N=100全部通过;owning package normal/race各75项顶层测试通过;gofmt/vet/仓库版本golangci-lint增量检查0issues。artifact11797959963(源码、完整补丁、命令日志、toolchain、native依赖、race预算及SHA256),ZIPdigest
f595df2e79c4924b61529883940feee0748515bb23bdee205ae59f8d0180ca57。独立review agent对最终artifact及真实证据明确Approve,无finding。证据证明所选包及相关门禁通过,不等于全仓CI;该修复是内部map快照所有权,不改SQL结果/持久格式,未追加重复BVT。操作记录:本人fork验证分支及产品分支创建、commit与Issue指派均由该智能体执行,commit message保留署名;Issue读回assignees包含
XuPeng-SH,保留原负责人matrix-meow。剩余PR发布阻塞:上游没有名为dev的分支。按用户明确要求不改投main/4.2-dev,不创建上游分支。因此目前没有修复PR,也没有PR审阅请求;不自动关闭Issue或合并。完整可复用补丁与源码保存在上述fork产品commit和验证artifact。
XuPeng-SH commentedon Oct 13, 2026
low priority!
MatrixAdventurer commentedon Oct 13, 2026
由 LW 授权的 ChatGPT Personal 智能体提交。
已读取负责人
XuPeng-SH的“low priority!”意见。按该优先级保留已验证的两文件候选、测试及独立审核记录,后续投入转向安全和用户流程阻塞,不扩大此项修复。未重复建 PR、未关闭 Issue,仍保留原 assignees;dev 目标分支阻塞不变。MatrixAdventurer commentedon Oct 14, 2026
由 LW 授权的 ChatGPT Personal 智能体提交。
已发布修复 PR #30187 ,目标 main(按用户最新无 dev 时使用 main 的授权),2 files +61/-16。本轮复核实际既有测试证据并独立复审 Approve,未重复新建 Issue。
操作记录更正:本轮初次读回本 Issue 只有
matrix-meow,并非仍含此前指派的 XuPeng-SH;现在已补指派并读回确认matrix-meow与XuPeng-SH均在 assignees,保留原负责人。PR 指派及审阅请求也确认含XuPeng-SH;未删除仓库自动请求的其他审阅者。PR mergeable=true,但上游 CI 尚待完成,不自动合并或关闭本 Issue。所有上述写入及只读核验由同署名主体执行。fix(shardservice): preserve immutable read cache snapshots (#30187)
Ariznawlll commentedon Oct 15, 2026
测试版本
main8795f3699b40504376343fa1acda8b4619f0c76e。bb2b725d0af4601d02ebf9cc68b1816a2b300022已包含在上述 main。pkg/shardservice包测试,Darwin arm64 / Go 1.26.5 / CGo;这是内部 read-cache 竞争,不涉及 SQL/CN 部署。原问题验证
针对 issue 的原始交错重建临时审计测试:写 goroutine 循环发布含 table 42 的新
readCache并调用生产removeCache(42);读 goroutine 同时循环读取当前已发布快照并调用hasTableCache(42)。每轮 100000 次交错,在当前 main 上执行go test -race -count=3 -run '^TestIssue25877ConcurrentReadRemove$' ./pkg/shardservice,3/3 通过,未出现原报告的 data race 或concurrent map read and map write。临时审计测试运行后已从隔离工作树移除;未修改用户工作树。Happy Path 覆盖
TestRemoveCachePreservesPublishedSnapshot在-race下 10/10 通过。TestRemoveCacheConcurrentDeletes在-race下 10/10 通过。Unhappy Path 覆盖
TestRemoveCachePreservesPublishedSnapshot的 10/10 次运行中。白盒验证
-race:3/3 通过;合并修复中的两个定向测试-race -count=10:通过。go test -race -count=1 ./pkg/shardservice:通过,约 49.6 秒;未见 data race、并发 map panic 或测试超时。回归测试
pkg/shardservice/service_read_test.go,随 fix(shardservice): preserve immutable read cache snapshots #30187 已进入 main,覆盖已发布快照不变性及并发删除。readCache指针和调度交错才能触发的故障,公开 SQL 无法稳定强制原路径;按回归分层放在 Go UT /-race,不硬塞进 MOTR、big-data 或 chaos。测试证据
-race -count=10返回ok github.com/matrixorigin/matrixone/pkg/shardservice 1.694s;原始交错-race -count=3返回ok ... 2.128s;完整包-race返回ok ... 49.638s。测试结论
测试通过。