Skip to content

[Bug]: shardservice removeCache 原地修改已发布快照导致并发 map 崩溃 #25877

Closed

Description

@Ariznawlll

是否存在相同问题

  • 已检索现有 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:已发布快照不变性

  1. 创建包含 table 42 的 read cache;
  2. 将其发布到 s.cache.read;
  3. 模拟读者保存已发布指针 published;
  4. 调用 s.removeCache(42);
  5. 当前缓存应删除 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:并发读写

  1. 读 goroutine 循环加载当前已发布快照并调用 hasTableCache;
  2. 写 goroutine 循环发布包含 table 的新快照,再调用生产代码 removeCache;
  3. 使用 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

  1. added this to the MO-202607 milestone on Jul 20, 2026
  2. added
    kind/bug
    on Jul 20, 2026
  3. added theissue type on Jul 20, 2026
  4. self-assigned this
    on Jul 20, 2026
  5. MatrixAdventurer commented on Oct 13, 2026

    @MatrixAdventurer
    Contributor

    由 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。

  6. removed their assignment
    on Oct 13, 2026
  7. XuPeng-SH commented on Oct 13, 2026

    @XuPeng-SH
    Contributor

    low priority!

  8. MatrixAdventurer commented on Oct 13, 2026

    @MatrixAdventurer
    Contributor

    由 LW 授权的 ChatGPT Personal 智能体提交。

    已读取负责人 XuPeng-SH 的“low priority!”意见。按该优先级保留已验证的两文件候选、测试及独立审核记录,后续投入转向安全和用户流程阻塞,不扩大此项修复。未重复建 PR、未关闭 Issue,仍保留原 assignees;dev 目标分支阻塞不变。

  9. MatrixAdventurer commented on Oct 14, 2026

    @MatrixAdventurer
    Contributor

    由 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。所有上述写入及只读核验由同署名主体执行。

  10. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:8795f3699b40504376343fa1acda8b4619f0c76e。
    • 修复:#30187 已合并,merge commit bb2b725d0af4601d02ebf9cc68b1816a2b300022 已包含在上述 main。
    • 部署方式:本机隔离 MatrixOne 源码工作树的 Go pkg/shardservice 包测试,Darwin arm64 / Go 1.26.5 / CGo;这是内部 read-cache 竞争,不涉及 SQL/CN 部署。
    • 测试日期:2026-10-15。

    原问题验证

    针对 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 覆盖

    1. 持有旧已发布快照的读者在删除 table 1 后仍能看到其旧内容;当前快照则不再含 table 1、仍保留 table 2。TestRemoveCachePreservesPublishedSnapshot 在 -race 下 10/10 通过。
    2. 两个 goroutine 并发删除不同 table,最终两个目标均删除、无关 table 仍在,旧快照保持 3 个条目不变;TestRemoveCacheConcurrentDeletes 在 -race 下 10/10 通过。

    Unhappy Path 覆盖

    1. 删除不存在的 table 3 不改变已发布快照;对 table 1 重复删除后,旧读者快照仍含 table 1,不发生原地 map 修改。该路径包含在 TestRemoveCachePreservesPublishedSnapshot 的 10/10 次运行中。
    2. 并发删除发生 CAS 竞争时,最终两项删除都生效,不能因一次 CAS 失败丢失其中一项;并发测试 10/10 通过。测试允许真实调度竞争,但未人工强制每次都进入 CAS 失败分支,此点不冒称覆盖。

    白盒验证

    • 原始 100000 次读/删交错 -race:3/3 通过;合并修复中的两个定向测试 -race -count=10:通过。
    • 完整 go test -race -count=1 ./pkg/shardservice:通过,约 49.6 秒;未见 data race、并发 map panic 或测试超时。
    • 修复前的旧快照断言失败和竞态栈已有 issue 原始报告及 PR #30187 的基线证据;本次未把修复前运行冒充当前 main 结果。

    回归测试

    测试证据

    • 修复 PR #30187 及其永久回归测试;PR 另附修复前后 Actions 运行。
    • 本轮独立执行结果:定向 -race -count=10 返回 ok github.com/matrixorigin/matrixone/pkg/shardservice 1.694s;原始交错 -race -count=3 返回 ok ... 2.128s;完整包 -race 返回 ok ... 49.638s。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

deferredkind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions