Skip to content

[Bug]: lockservice deadlock detector close hangs on remote GetTxnLock retry #25795

Closed

Description

@Ariznawlll

问题概述

最新 main 上,lockservice 死锁检测协程在读取远端事务锁时,如果远端 lock-table owner 持续不可达、但 allocator 仍返回原 bind 有效,remoteLockTable.getLock 会无限重试且无法响应 detector stopper 的取消。

此时 detector.close() 会一直阻塞在 stopper.Stop();生产路径中的 service.Close() 会在关闭 remote client 后调用它,因此 CN/lockservice 关闭可能永久卡住。

验证基线

  • 仓库:matrixorigin/matrixone
  • 分支:最新 main
  • Commit:86a0a6a8f66fcf263c81f99df84bd06d0ceef373
  • Commit 时间:2026-07-16T05:06:33+00:00
  • Commit 标题:fix(proxy): retry HAKeeper errors during bootstrap (#25778)
  • 环境:macOS arm64,Go go1.26.4

白盒测试场景

新增临时单测 TestDeadlockDetectorCloseCancelsRemoteGetLockRetry,完整模拟以下路径:

  1. 创建一个 remote lock table,bind 指向 cn2/table=1。
  2. fake RPC client 对 GetTxnLock 持续返回 io.ErrUnexpectedEOF,模拟远端 lock-table owner 不可达。
  3. 对 allocator 的 GetBind 返回完全相同且有效的 bind,模拟 allocator 尚未完成 bind 切换。
  4. 让 deadlock detector 的 waitTxnsFetchFunc 进入 remoteLockTable.getLock。
  5. 确认第一次 GetTxnLock 已发出后,并发调用 detector.close()。
  6. 期望:关闭信号应取消在途 RPC/重试,close() 在 100ms 内返回。
  7. 实际:100ms 后仍未返回;只有把 fake RPC 故障开关恢复后,getLock 返回,stopper.Stop() 才能完成。
  8. 测试在断言后主动恢复 RPC,确保每轮测试均能清理工作协程,不遗留 goroutine。

核心断言:

closed := make(chan struct{})
go func() {
    d.close()
    close(closed)
}()

returnedPromptly := false
select {
case <-closed:
    returnedPromptly = true
case <-time.After(100 * time.Millisecond):
}

client.released.Store(true) // 测试清理
<-closed

require.True(t, returnedPromptly,
    "deadlock detector close is blocked by remote GetTxnLock retry")

执行命令与结果

普通模式,重复 10 次

export GOROOT=/opt/homebrew/opt/go/libexec
export PATH="$GOROOT/bin:$PATH"
export CGO_CFLAGS="-I$PWD/thirdparties/install/include"
export DYLD_LIBRARY_PATH="$PWD/cgo:$PWD/thirdparties/install/lib"

go test ./pkg/lockservice \
  -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
  -count=10 -v

结果:FAIL 10/10。每轮均出现:

--- FAIL: TestDeadlockDetectorCloseCancelsRemoteGetLockRetry (0.10s)
Error: Should be true
Messages: deadlock detector close is blocked by remote GetTxnLock retry

race 模式,重复 5 次

go test -race ./pkg/lockservice \
  -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
  -count=5 -v

结果:FAIL 5/5,失败原因与普通模式一致;未出现 race detector 告警。

根因分析

  1. pkg/lockservice/lock_table_remote.go:275-297 中 remoteLockTable.getLock 是无退出条件的 for 循环。
  2. 每次失败后调用 contextless 的 handleError,然后调用 waitRemoteRetryBackoff;后者直接 time.Sleep,无法响应取消。
  3. doGetLock(约 381-393 行)使用 context.Background() 创建单次 RPC timeout,而不是 detector/service 的生命周期 context。单次 RPC 即使超时,外层仍会永久重试。
  4. detector.doCheck → checkDeadlock → waitTxnsFetchFunc 会同步进入上述调用;stopper context 只在 doCheck 外层 select 处检查,进入 getLock 后无法生效。
  5. detector.close 在 stopper.Stop() 等待工作协程退出,因此被无限重试卡住。
  6. service.Close 先关闭 remote client,再调用 deadlockDetector.close()。已经进入重试的协程在 client 关闭后仍没有退出条件,实际关闭路径同样可能被永久阻塞。

同文件中的 getLockHolder(ctx,...) 已采用 caller context 和 waitRemoteRetryBackoffWithContext,而 getLock 尚未采用同样模式。

影响

  • CN/lockservice 优雅关闭可能永久阻塞,影响滚动升级、缩容和故障恢复。
  • 死锁检测 worker 长期占用,远端 CN 故障期间最多可逐步耗尽 detector worker。
  • 持续产生无意义 RPC 与 allocator bind 查询,放大故障期间负载。
  • 关闭 remote client 后仍继续重试,无法靠当前关闭顺序自愈。

建议修复

  • 给 remoteLockTable.getLock、doGetLock 和 handleErrorWithContext 贯穿 caller context。
  • 把 waitRemoteRetryBackoff 替换为 waitRemoteRetryBackoffWithContext。
  • 从 deadlock detector 的 stopper context 传入该调用链;取消后返回明确错误,让 checkDeadlock 退出。
  • 增加本 issue 的关闭场景回归测试,并覆盖普通模式与 -race。

查重

已搜索 GetTxnLock retry、deadlock detector close、lockservice close hang、remoteLockTable.getLock,未发现相同 issue。

#25202 描述的是旧版 remoteLockTable.unlock 永久重试;本问题发生于 getLock/死锁检测/服务关闭路径,不是同一缺陷。

Activity

  1. added this to the MO-202607 milestone on Jul 16, 2026
  2. added
    kind/bug
    on Jul 16, 2026
  3. added theissue type on Jul 16, 2026
  4. Ariznawlll commented on Jul 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 复验(2026-07-16)

    issue 创建后再次同步远端,发现 main 已前进,因此已将同一白盒用例迁移到新的最新 HEAD 并完整重跑。

    新基线

    • 分支:main
    • Commit:71f8d076a8ea17ac7261705078d130f4700ee720
    • Commit 时间:2026-07-16T06:36:36+00:00
    • Commit 标题:fix: support expressions over DISTINCT select columns in ORDER BY (#25783)

    执行命令

    go test ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=10 -v
    
    go test -race ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=5 -v

    结果

    • 普通模式:FAIL 10/10
    • race 模式:FAIL 5/5
    • 每轮均为 detector.close() 超过 100ms 未返回;恢复 fake RPC 后立即退出。
    • 代表性输出:
    --- FAIL: TestDeadlockDetectorCloseCancelsRemoteGetLockRetry (0.10s)
    Error: Should be true
    Messages: deadlock detector close is blocked by remote GetTxnLock retry
    

    结论:该问题在当前最新 main@71f8d076a8 仍稳定存在,原 issue 结论不变。

  5. Ariznawlll commented on Jul 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 白盒复验(2026-07-16)

    测试基线

    • origin/main@cd741923cd847ae26faacffc5b6adb101bb6dcb7
    • Commit:fix(logservice): close store on initialization failure (#25774)
    • 测试前重新 fetch,并确认 origin/main 未前进。

    执行用例

    临时白盒用例:TestDeadlockDetectorCloseCancelsRemoteGetLockRetry

    1. 构造 remote lock table;
    2. 让 GetTxnLock RPC 持续失败,强制进入 remoteLockTable.getLock 重试;
    3. 启动 deadlock detector 检查任务;
    4. 调用 detector close();
    5. 断言 close 应在 100ms 内返回;超时后仅为清理测试解除 RPC 故障。
    go test ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=10 -v
    
    go test -race ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=5 -v

    实际结果

    • 普通模式:FAIL 10/10
    • Race 模式:FAIL 5/5
    • 每轮均出现:
    Error: Should be true
    Messages: deadlock detector close is blocked by remote GetTxnLock retry
    --- FAIL: TestDeadlockDetectorCloseCancelsRemoteGetLockRetry (0.10s)
    

    结论

    在当前最新 main 仍可稳定复现。remoteLockTable.getLock 的后台重试不受 detector 关闭信号控制,导致关闭等待远程 RPC 恢复;该 issue 仍然有效。

  6. Ariznawlll commented on Jul 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    main@8f3aee9ce 再次复验(2026-07-16)

    main 前进后已将同一个白盒用例迁移到新 HEAD 并重跑,测试前后 fetch 均确认 origin/main=8f3aee9cef5d667c6c7686d83cb155d0ba321e1f。

    go test ./pkg/lockservice -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' -count=10
    go test -race ./pkg/lockservice -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' -count=5
    • 普通模式:FAIL 10/10
    • Race 模式:FAIL 5/5
    • 仍稳定报错:deadlock detector close is blocked by remote GetTxnLock retry

    结论:新 HEAD 未修复该问题,issue 仍有效。

  7. Ariznawlll commented on Jul 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main@824090c2e 白盒复验(2026-07-17)

    今天重新执行 git fetch origin main,将同一临时白盒用例迁移到新的最新 HEAD 后重跑。

    测试基线

    • Commit:824090c2e61351972cc16f866a57d52c7c2cab6e
    • Commit 时间:2026-07-17T01:10:07+00:00
    • Commit 标题:perf(hashmap): allocate int iterator buffers lazily (#25796)

    实际执行

    go test ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=20 -v -timeout=60s
    
    go test -race ./pkg/lockservice \
      -run '^TestDeadlockDetectorCloseCancelsRemoteGetLockRetry$' \
      -count=5 -v -timeout=60s

    测试结果

    • 普通模式:FAIL 20/20
    • Race 模式:FAIL 5/5
    • 每轮均在 detector.close() 等待 100ms 后失败;仅在测试主动恢复 fake GetTxnLock RPC 后,close 才能返回。
    • Race detector 没有报告数据竞争,失败原因仍是关闭流程不可取消。

    代表性输出:

    --- FAIL: TestDeadlockDetectorCloseCancelsRemoteGetLockRetry (0.10s)
    Error:       Should be true
    Messages:    deadlock detector close is blocked by remote GetTxnLock retry
    

    结论

    今天最新 main 没有修改 remoteLockTable.getLock 的无 context 重试路径,该问题仍然稳定存在,issue 继续有效。

  8. added
    severity/s0
    and removed on Jul 21, 2026
  9. added a commit that references this issue on Jul 21, 2026
    78d15d6
  10. assigned and unassigned on Jul 22, 2026
  11. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成复验,deadlock detector 关闭不再被远端 GetTxnLock 重试永久阻塞。

    测试版本

    测试场景

    1. 构造 remote lock table;
    2. fake client 让 GetTxnLock 持续失败并进入重试;
    3. deadlock detector 的 worker 同步进入 remote getLock;
    4. 调用 detector.close(),检查 stopper context 能否取消重试并及时退出;
    5. 额外覆盖 bind refresh 阶段的取消传播。

    执行命令:

    go test ./pkg/lockservice \
      -run '^(TestDeadlockDetectorCloseCancelsRemoteGetLockRetry|TestRemoteGetLockPreservesCancellationDuringBindRefresh)$' \
      -count=20 -timeout=120s
    
    go test -race ./pkg/lockservice \
      -run '^(TestDeadlockDetectorCloseCancelsRemoteGetLockRetry|TestRemoteGetLockPreservesCancellationDuringBindRefresh)$' \
      -count=5 -timeout=180s

    实际结果

    • 普通模式:两个场景各运行 20 次,全部通过;
    • Race 模式:两个场景各运行 5 次,全部通过;
    • detector.close() 无需恢复 fake RPC 即可返回;
    • remote getLock 和 bind refresh 均返回 context.Canceled;
    • 未残留持续重试的 worker,Race detector 未报告数据竞争。

    测试结论

    deadlock detector 的生命周期 context 已贯穿远端取锁和 bind refresh 重试路径,关闭信号能够终止在途 RPC/退避重试。原先 stopper.Stop() 永久等待的问题已修复,验证通过。

  12. added 3 commits that reference this issue on Sep 17, 2026
    7065d2c
    58193fb
    6542312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions