Skip to content

[Bug]: LockService B-tree Delete recursively overflows goroutine stack and crashes AP CN  #28363

Closed

Description

@LeftHandCold

Is there an existing issue for the same bug?

  • I have checked the existing issues.

检查了最近一周的 issue,并补查 stack overflow / growChildAndRemove,未发现覆盖本次 LockService 删除递归故障的重复报告。#27966 的 IVF 内部 SQL 路由问题和 #28357 的 Catalog 校验重试问题不是本报告已确认的机制。

Branch Name

4.2-dev

Commit ID

33640a3adf4bd933de3881b28ab47c157a5ad209

Other Environment Information

Actual Behavior

AP CN 在事务 Commit 调用收尾的 Unlock 路径中,删除 LockService B-tree 元素时发生极深的重复递归,触发 Go runtime fatal stack overflow,整个 CN 退出。不是普通 SQL 错误,也不能归为内存配小后预期出现的 OOMKilled。

确认范围:直接致命故障及完整调用路径已经由现场栈证实;导致删除无法推进的原始状态变化尚未定位。没有证据把它归因于某个具体 PR、某个 key 的 use-after-free,或第三方 B-tree 库本身。

时间线

以下北京时间均为 2026-09-07 UTC+8,对应 UTC 日期是 2026-09-06。

北京时间 UTC 事件
05:28:48 21:28:48 Concurrent 开始 100 worker 的 prepared update_pk 阶段
约05:30 约21:30 AP 2v7h4 第一次 OOM;历史 KSM 在21:31:15已记录 OOMKilled
05:30:51 21:30:51 AP 2v7h4 新进程启动,后续 fatal 属于该进程的 main/1.log
05:30:55 21:30:55 另一个 AP dxpr9 也 OOMKilled / exit137
05:33:52.538 21:33:52.538 AP 2v7h4 打印 fatal error: stack overflow
05:35:33 21:35:33 Concurrent 的 update_pk 阶段结束并记录错误
05:37:53 21:37:53 AP 2v7h4 进程结束,Error / exit134,restartCount=2
06:57:57 22:57:57 TP jjqzm 在后续 ANLI 数据初始化阶段 OOMKilled / exit137

必须区分:05:37:53 是进程结束时间,不是 fatal 首次发生时间。两者约4分钟的间隔尚无 core/内核日志解释。第一次 OOM 和之后的栈溢出是两个不同进程,不能认为旧进程 Go 堆损坏跨重启保留。

原始崩溃栈

来自上述 AP 的 stderr、main/1.log。保留关键帧,省略参数和重复帧:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

goroutine 7856 ... [running]:
github.com/google/btree.(*node[...]).remove
    btree_generic.go:369
github.com/google/btree.(*node[...]).growChildAndRemove
    btree_generic.go:471
github.com/google/btree.(*node[...]).remove
    btree_generic.go:396
github.com/google/btree.(*node[...]).growChildAndRemove
    btree_generic.go:471
...657844 frames elided...
github.com/google/btree.(*BTreeG[...]).deleteItem
    btree_generic.go:734
github.com/google/btree.(*BTreeG[...]).Delete
    btree_generic.go:712
github.com/matrixorigin/matrixone/pkg/lockservice.(*btreeBasedStorage).Delete
    btree_storage.go:69
github.com/matrixorigin/matrixone/pkg/lockservice.(*localLockTable).unlock.func3
    lock_table_local.go:411
github.com/matrixorigin/matrixone/pkg/lockservice.(*fixedSlice).iter
    slice.go:158
github.com/matrixorigin/matrixone/pkg/lockservice.(*localLockTable).unlock
    lock_table_local.go:348
github.com/matrixorigin/matrixone/pkg/lockservice.(*activeTxn).closeWithContextInternal
    txn.go:439
github.com/matrixorigin/matrixone/pkg/lockservice.(*service).Unlock
    service.go:447
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).unlock.func2
    operator.go:2037
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).unlock
    operator.go:2032
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).doWrite.func4
    operator.go:1542
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).doWrite
    operator.go:1617
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).commitOwned
    operator.go:1168
github.com/matrixorigin/matrixone/pkg/txn/client.(*txnOperator).Commit
    operator.go:1124
github.com/matrixorigin/matrixone/pkg/frontend.(*TxnHandler).commitUnsafe
    frontend/txn.go:596

已显示的重复 growChildAndRemove 帧中,child index 反复在1和2之间切换,删除 key 参数的地址/长度保持相同(长度5)。日志没有 key 实际字节,不能据此还原 table ID 或 txn ID。

GOTRACEBACK=crash 与 fatal 后 exit134 一致;这是 runtime 致命错误,外层普通 recover 不能把它转成正常事务失败。

Expected Behavior

  • 正常 SQL、并发事务及远端节点重启后,锁释放应正确终止,不应递归至 runtime 栈上限并杀死整个 CN。
  • 即使高压力下部分 SQL 超时/失败,事务收尾不应引入独立的进程致命故障。
  • 修复必须维护锁互斥、key 生命周期和树排序不变量;不能简单吞掉 Delete、跳过 Unlock 或以正常请求永久持锁作为规避。

Steps to Reproduce

现场真实发生;尚未完成独立、确定性的最小复现。以下是保留的触发背景,不是保证每次触发的脚本。

  1. 使用上述 commit 和 1TP+2AP / 48Gi 配置,运行关联 Weekly Full workload。

  2. Concurrent 先执行大规模建删 Account/Table,再执行并发索引/列/主键等 DDL。

  3. 故障窗口的 update_pk 使用100个 prepared worker,表 test01 初始化100万行,执行:

    update test01 set a=? where b=?;
  4. 两个 AP 在约05:30发生 OOM,2v7h4 重启后的 Commit 收尾 Unlock 在05:33:52触发上述栈。

  5. 为定向复现保留实际 lock table / txn / key 的完整生命周期及 B-tree 状态;不能仅在测试中人为改坏任意 key 后声称已经复现现场根因。

update_pk 与 fatal 的时间重合、调用入口吻合,但没有将 goroutine 7856 精确关联到某一次 prepared 参数或事务。因此目前也不能声称该 SQL 本身就是破坏树状态的写入者。

Additional information

部署版本代码路径及独立 review

  1. txnOperator.doWrite 注册 deferred unlock。栈经过 Commit 不证明 TN 已经成功提交,成功/错误退出均可能执行收尾。
  2. localLockTable.unlock 持有 l.mu;遍历 held keys,查询 holder、处理 mutation,closeTxn 后允许删除时在411行调用 l.mu.store.Delete(key)。本次栈落在该 Delete,而非413行的 range start Delete。
  3. btreeBasedStorage 使用 google/btree v1.1.2,degree=32,比较器 bytes.Compare(a.key,b.key)<0,Delete委托给库。
  4. btree remove/growChildAndRemove 在目标子节点元素不足时先借元素/合并,再重新在当前节点执行 remove。正确且稳定的树及比较关系下应推进删除;现场却出现几十万重复帧,不能用普通树较大解释。
  5. 不能仅凭存储 Add 保存 []byte 引用就认定 buffer 重用:普通 lockop 的 Packer.Bytes() 使用 slices.Clone,远端 Lock protobuf 解码也复制 Rows。正常 localLockTable 增删路径有互斥保护,不能仅凭第三方树非线程安全就认定并发写树。

已确认:LockService 删除路径无法正常终止,导致 CN fatal stack overflow。

尚未闭环:是否存在某条特殊 key 生命周期路径改变比较关系、树结构/状态被破坏、或其他实现问题;具体 table/txn/key;最早引入版本。没有证明是4.0之后或4.2新增的回归,也未证明 main 不受影响。

影响:CN 退出和正在使用该 CN 的请求中断。删除过程持有本地表锁,递归期间同一 lock table 的其他操作可能受阻。未观察到足以认定持久数据损坏的证据,不把可用性故障扩大为已确认的数据一致性破坏。

OOM 背景,不与本 issue 混为同一根因

  • 最终 pods.json 只保存最后一次退出,2v7h4 的历史 OOM 由 KSM 补齐。本轮是三个 CN 都发生过 OOM,另有一次独立的 stack overflow。
  • AP 05:29:30:2v7h4 / dxpr9 RSS约42.20 / 42.38GiB;Go HeapAlloc约17.54 / 18.81GiB;offheap cache约9.64 / 9.60GiB。
  • AP 容器 usage 采样峰值约47.50 / 47.13GiB,接近48Gi限制;不是内核触发 OOM 瞬间的精确峰值。
  • OOM 前 memory:inuse_space:bytes:space:bytes profile 中,applyToCatalogCache 子树占比:2v7h4约78.55%,dxpr9约74.46%;TP OOM前约55.13%。这些是存活分配来源占比,不是精确保留引用者或泄漏证明,合并profile的total也不是单时刻RSS。
  • 同轮 Concurrent 报告留下4237个run-owned accounts;namespace是其清理边界。Catalog和租户元数据负担是重要背景,但不能证明这些account是全部OOM的唯一原因。
  • TP OOM时对应 ANLI 初始化 INSERT INTO ... SELECT ... 复制数据阶段,不是已经进入qvec查询。缓存压力清理实际运行过,将约12GiB降低到9.6GiB,仍未避免之后的OOM。
  • OOM原因仍需单独分析,不能直接以“Catalog泄漏”“IVF读放大”或“标签串扰”定责,更不能说之前OOM直接损坏了重启后进程的内存。

日志/快照定位

Loki 取 2026-09-06 21:33:50-21:34:00 UTC,先查:

{namespace="mo-42-family-34045526748-1", pod="nightly-regression-dis-ap-cn-2v7h4"} |= "stack overflow"

然后按同一 stderr main/1.log 展开完整多行栈,不要仅保留fatal一行。重启状态核对21:29-21:39 UTC。

fatal前已有heap快照写入日志:

2026-09-06 21:33:51 UTC
ETL:/profile/CN_66323832-3435-3137-6336-383134343639_heap_01a078a4-5881-797c-a55a-ba8aa924dfdd.gz

heap profile有助于分析内存构成,但不能替代包含 key/节点实际内容的core。建议优先保留该镜像、完整stderr、core(若生成)以及事故前后的锁操作证据。

Activity

  1. added
    kind/bug
    on Sep 8, 2026
  2. XuPeng-SH commented on Sep 8, 2026

    @XuPeng-SH
    Contributor

    New analysis: recursive Delete requires a broken ordering invariant

    I ran an isolated probe against the incident dependency and configuration: google/btree v1.1.2, degree 32, and the same byte comparator used by LockService.

    The results separate normal tree behavior from the failure mechanism:

    • With immutable keys, serialized access, and 800,000 randomized insert/replace/get/delete operations across eight seeds, the tree remained valid and every operation matched an independent map oracle.
    • With 96 valid entries, changing one key's bytes after insertion caused Get to work but made Delete repeatedly alternate between the same child positions while borrowing separator items. The deletion never made progress.
    • Copying the key at insertion prevented the failure under the same mutation attempt.

    This reproduces a credible mechanism for the observed remove -> growChildAndRemove recursion: a key mutation violates the B-tree ordering invariant, and rebalancing can move the separator back and forth between adjacent children. It does not prove that production caused the mutation.

    The current production evidence still needs to close that gap:

    • The normal lock-row path obtains encoded keys from Packer.Bytes(), which clones its buffer.
    • Local lock-table operations are serialized by the table mutex.
    • The issue does not yet identify the affected table, transaction, key bytes, or the operation immediately preceding the fatal delete.

    Therefore the issue status should be: the failure mechanism is reproduced under an invalid key-ordering state; the production trigger remains unconfirmed.

    The next useful changes are:

    1. Add a test-only B-tree invariant checker and a bounded recent-operation trace containing table, transaction, key length/hash, operation, granularity, and range-pair identity.
    2. Audit every LockStorage.Add caller, including range merge, replacement, remote decode, observability, and cleanup paths; add a direct storage ownership test that mutates the caller buffer after Add.
    3. Add deterministic lifecycle tests for range-pair merge, duplicate/replacement keys, commit, rollback, cancel, timeout, and table close. The oracle must verify ordering, paired endpoints, transaction-held keys, and store length.
    4. Only after identifying the first invariant violation should we choose between an ownership fix, a lock bookkeeping fix, or a B-tree implementation fix.

    A recursive-to-iterative rewrite may prevent stack exhaustion, but by itself it can turn the fatal crash into a non-terminating delete or a partially mutated lock store. It should not be treated as the root fix before the first invalid state is captured.

  3. changed the title [-][Bug]: LockService B-tree Delete recursively overflows goroutine stack and crashes AP CN (4.2-dev)[/-] [+][Bug]: LockService B-tree Delete recursively overflows goroutine stack and crashes AP CN [/+] on Sep 8, 2026
  4. added
    severity/s0
    and removed on Sep 8, 2026
  5. LeftHandCold commented on Sep 8, 2026

    @LeftHandCold
    CollaboratorAuthor

    阶段性调查结论:降为 S1,保持观察

    本 Issue 已确认的事实不变:4.2 事故进程在 LockService 解锁路径的
    btreeBasedStorage.Delete 中进入 google/btree 的重复
    remove -> growChildAndRemove 递归,最终触发 Go runtime stack overflow 并使 AP
    CN 退出。这是 CN 可用性故障,不是普通 SQL 错误;但目前没有证据证明持久数据损坏。

    现场栈只能证明 Delete 时 BTree 已经不满足正常推进所需的不变量;它没有给出 table、
    txn、key 原始字节,或首次破坏不变量的操作。因此不能将某个候选代码路径直接写成根因。

    已完成的代码核查

    • LockService 的 BTree 插入确实借用输入 []byte,所以 key 在进入树后必须保持不可变:
      btree_storage.go。
    • 正常 SQL lock key 由 Packer.Bytes() 复制产生;RPC LockRequest.Unmarshal 也为 Rows
      分配并复制字节。请求池 Reset 只解除请求字段引用,不会清零已保留 key 的底层字节。
    • 已逐一复核 localLockTable 的 store.Add 调用:新 row/range lock 使用 LockContext
      Rows,状态替换使用当前 key 或 BTree 返回的 paired key,范围合并使用已有端点。当前
      main 未找到会改写已插入 key 底层字节的正常调用方。

    确定性验证

    在隔离 Linux harness 中,使用真实 LockService、真实 protobuf 编解码和真实 BTree,
    以下全部通过普通与 -race 运行:

    1. 请求释放/同一请求 Reset 后重新 Unmarshal,再执行 64 key 的范围合并与 Unlock;
    2. 两个 LockService 的远端 waiter 入队后,取消 Lock context 并显式 UnlockWithContext;
    3. handleRemoteLock 返回但 waiter 仍存在时,对同一请求 Reset/Unmarshal,再完成取消和
      Unlock。

    这些测试用事件屏障确认 waiter 状态,不依赖随机 sleep;断言包括原 key 快照、BTree
    顺序和 Get、一轮 Unlock 后表为空、后继事务可重新拿锁。组合运行也通过(普通 0.264s,
    race 1.309s)。因此目前没有复现 request reuse、远端异步 handler、取消清理或范围合并
    导致的 key 改写/锁遗留。

    #27928 的关系

    事故 4.2 SHA 33640a3 不包含 main 已合入的 #27928。旧
    cowSlice.slice 存在零引用 fixedSlice 被 stale reader 重新加引用并重复归还对象池的
    真实生命周期缺陷;在精确测试窗口中,旧代码的归还计数从预期 1/2 变为 2/3,当前
    main 通过同一测试。

    但 #27928 改坏的是事务账本 [][]byte 的元素引用/长度,append、join、close 并不
    写入已经由 BTree 保存的 key 底层字节。它是应独立处理的真实 bug,不是 #28363 已证明
    根因,也不能声称单独修复本次 BTree 递归故障
    。

    当前处置

    将严重性从 S0 调整为 S1:保留 active owner 和观察,不关闭 Issue,也不提交推测性修复。
    不采用全量 clone BTree key、跳过 Delete、递归上限或吞错等方案;这些方案会引入锁热路径
    分配、锁遗留或互斥语义风险,且没有证据能修复现场。

    后续以 main 为主观察。若 main 再出现同类栈溢出,使用已有有界 BTree 诊断构建捕获首次
    key 别名变化、物理树/独立账本差异和最近操作,再按实际写入者做最小修复。出现以下任一
    信号应立即重新升为 S0:main 再现 stack overflow、诊断抓到不变量首次破坏、或出现锁互斥/
    数据一致性影响。

  6. added
    severity/s1
    and removed
    severity/s0
    on Sep 8, 2026
  7. LeftHandCold commented on Sep 15, 2026

    @LeftHandCold
    CollaboratorAuthor

    main没有出现过了先关闭

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions