Skip to content

fix(lockservice): avoid bind fence cleanup deadlock - #26749

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-24766-unknown-bind-deadlock
Aug 6, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-24766-unknown-bind-deadlock

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24766

What this PR does / why we need it:

The latest incident is a lockservice deadlock, not evidence that disconnected
frontend sessions skipped Rollback.

After unknown-commit owner cleanup succeeds, unlockUnknownCommit still holds
activeTxn.Lock while deleting the transaction from its holder shard:

activeTxn.Lock -> activeTxnHolder shard.Lock

At the same time stale-bind cleanup used to retain shard.RLock while waiting
for every transaction mutex:

activeTxnHolder shard.RLock -> activeTxn.Lock

The two production goroutine profiles, captured five hours apart, retain the
same opposing stacks. The deadlock also holds bindChangeMu.Lock, so new lock
requests accumulate behind service.Lock, making live transaction handles look
like a growing txn leak.

This PR:

  • snapshots transaction keys under each holder shard;
  • uses TryLock while the shard protects the pooled transaction pointer;
  • releases the shard and retries on contention, allowing unknown-commit deletion
    to complete;
  • preserves the existing stale-bind fence behavior once the transaction lock is
    acquired;
  • adds deterministic regression coverage for the exact unknown-cleanup/bind-fence
    interleaving and for the holder lock-order invariant.

Normal lock, unlock, Commit, allocator-fence, and proxy handoff semantics are
unchanged. The retry path is reached only during abnormal bind fencing when a
transaction mutex is already busy.

Validation:

  • pre-fix regression: deterministic timeout with
    unlockUnknownCommit -> deleteActiveTxn -> shard.Lock opposing
    fenceByBindChanged -> activeTxn.Lock;
  • go test -race ./pkg/lockservice -run '^(TestUnknownCommitCleanupDoesNotDeadlockBindFence|TestMapBasedTxnHolderFenceReleasesShardWhileTxnIsBusy)$' -count=100 -timeout=5m;
  • go test ./pkg/lockservice -count=1 -timeout=10m;
  • go test -race ./pkg/lockservice -count=1 -timeout=15m;
  • go vet ./pkg/lockservice.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@iamlinjunhong iamlinjunhong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查结论:APPROVE。

Findings:P0、P1、P2、P3 均无。

已核对 base 与 merge-base ffc1603,以及 head 451e272;审查了完整 4 文件 diff、bind-change 发布与 Lock 调用链、普通和 unknown-commit 删除/恢复路径、activeTxn 池化复用边界及新增回归测试。

锁序闭环成立:TryLock 失败时先释放 shard.RLock,使持有 txn.Lock 的 unknown-commit cleanup 能取得 shard.Lock 并完成删除;TryLock 成功时 shard.RLock 又能保证 pooled transaction 在访问期间不会被删除和复用。新事务若在 shard snapshot 后创建,也会被 bindChangeMu 阻挡到 bind 发布完成,再按新 bind 校验,不会漏过旧 bind fence。临时 key snapshot 按单 shard 释放,未形成无界累积。

远端 24 个 checks 均为终态且符合 watcher 门槛。隔离 worktree 的本地 CGo 测试包装器因缺少 cgo/libmo.dylib 和 third-party native artifacts 在编译前退出,因此未将该次本地尝试计为通过。

@mergify

mergify Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 451e272e604f7a522b178a9f240be68465adc18e.

No blocking findings remain.

The deadlock root cause and the fix line up at the ownership boundary. The old bind fence held activeTxnShard.RLock while blocking on activeTxn.Lock, opposing unknown-commit cleanup's activeTxn.Lock -> activeTxnShard.Lock. The new path snapshots keys, retains the shard read lock only while validating/protecting the pooled transaction pointer, and uses TryLock; contention releases the shard before retrying. That removes the cyclic wait while preserving object-lifetime safety.

I traced the adjacent state transitions as well:

  • A transaction deleted after the snapshot is observed as absent and skipped; the shard prevents deletion/reuse after the entry is selected and before the transaction lock is acquired.
  • A lock request that creates a transaction while bindChangeMu is exclusively held cannot publish an old bind: after the fence it rechecks the current table bind under bindChangeMu.RLock and either rejects the stale table or proceeds with the new one.
  • Unknown-commit cleanup can now acquire the shard write lock, delete the entry, finish ownership cleanup, and free the pooled transaction exactly once.
  • The temporary key snapshot is bounded by one holder shard and released before visiting the next shard; the retry cost is confined to abnormal bind fencing with an already-busy transaction.

Q1/Q2/Q3 audit: one terminal owner remains for each pooled transaction; the formerly cyclic wait graph now terminates through either transaction unlock or entry deletion; no unbounded retained accumulation was introduced.

Local exact-head validation passed: the two new focused regressions under -race -count=100, full pkg/lockservice tests, go build, go vet, and git diff --check. Current GitHub CI is complete with no failures.

@mergify mergify Bot added the queued label Aug 6, 2026
@mergify

mergify Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-06 07:55 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-06 10:17 UTC · at dc50467f6b5f83dbacbbb8476f933ccfe8b54238 · squash

This pull request spent 2 hours 21 minutes 52 seconds in the queue, including 1 hour 1 minute 36 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

@mergify
mergify Bot merged commit 6798bd6 into matrixorigin:main Aug 6, 2026
23 of 25 checks passed
@mergify mergify Bot removed the queued label Aug 6, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The latest incident is a lockservice deadlock, not evidence that disconnected
frontend sessions skipped Rollback.

After unknown-commit owner cleanup succeeds, unlockUnknownCommit still holds
activeTxn.Lock while deleting the transaction from its holder shard:

activeTxn.Lock -> activeTxnHolder shard.Lock

At the same time stale-bind cleanup used to retain shard.RLock while waiting
for every transaction mutex:

activeTxnHolder shard.RLock -> activeTxn.Lock

The two production goroutine profiles, captured five hours apart, retain the
same opposing stacks. The deadlock also holds bindChangeMu.Lock, so new lock
requests accumulate behind service.Lock, making live transaction handles look
like a growing txn leak.

This PR:

- snapshots transaction keys under each holder shard;
- uses TryLock while the shard protects the pooled transaction pointer;
- releases the shard and retries on contention, allowing unknown-commit deletion
to complete;
- preserves the existing stale-bind fence behavior once the transaction lock is
acquired;
- adds deterministic regression coverage for the exact unknown-cleanup/bind-fence
interleaving and for the holder lock-order invariant.

Normal lock, unlock, Commit, allocator-fence, and proxy handoff semantics are
unchanged. The retry path is reached only during abnormal bind fencing when a
transaction mutex is already busy.

Validation:

- pre-fix regression: deterministic timeout with
unlockUnknownCommit -> deleteActiveTxn -> shard.Lock opposing
fenceByBindChanged -> activeTxn.Lock;
- go test -race ./pkg/lockservice -run '^(TestUnknownCommitCleanupDoesNotDeadlockBindFence|TestMapBasedTxnHolderFenceReleasesShardWhileTxnIsBusy)$' -count=100 -timeout=5m;
- go test ./pkg/lockservice -count=1 -timeout=10m;
- go test -race ./pkg/lockservice -count=1 -timeout=15m;
- go vet ./pkg/lockservice.

Approved by: @iamlinjunhong, @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The latest incident is a lockservice deadlock, not evidence that disconnected
frontend sessions skipped Rollback.

After unknown-commit owner cleanup succeeds, unlockUnknownCommit still holds
activeTxn.Lock while deleting the transaction from its holder shard:

activeTxn.Lock -> activeTxnHolder shard.Lock

At the same time stale-bind cleanup used to retain shard.RLock while waiting
for every transaction mutex:

activeTxnHolder shard.RLock -> activeTxn.Lock

The two production goroutine profiles, captured five hours apart, retain the
same opposing stacks. The deadlock also holds bindChangeMu.Lock, so new lock
requests accumulate behind service.Lock, making live transaction handles look
like a growing txn leak.

This PR:

- snapshots transaction keys under each holder shard;
- uses TryLock while the shard protects the pooled transaction pointer;
- releases the shard and retries on contention, allowing unknown-commit deletion
to complete;
- preserves the existing stale-bind fence behavior once the transaction lock is
acquired;
- adds deterministic regression coverage for the exact unknown-cleanup/bind-fence
interleaving and for the holder lock-order invariant.

Normal lock, unlock, Commit, allocator-fence, and proxy handoff semantics are
unchanged. The retry path is reached only during abnormal bind fencing when a
transaction mutex is already busy.

Validation:

- pre-fix regression: deterministic timeout with
unlockUnknownCommit -> deleteActiveTxn -> shard.Lock opposing
fenceByBindChanged -> activeTxn.Lock;
- go test -race ./pkg/lockservice -run '^(TestUnknownCommitCleanupDoesNotDeadlockBindFence|TestMapBasedTxnHolderFenceReleasesShardWhileTxnIsBusy)$' -count=100 -timeout=5m;
- go test ./pkg/lockservice -count=1 -timeout=10m;
- go test -race ./pkg/lockservice -count=1 -timeout=15m;
- go vet ./pkg/lockservice.

Approved by: @iamlinjunhong, @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The latest incident is a lockservice deadlock, not evidence that disconnected
frontend sessions skipped Rollback.

After unknown-commit owner cleanup succeeds, unlockUnknownCommit still holds
activeTxn.Lock while deleting the transaction from its holder shard:

activeTxn.Lock -> activeTxnHolder shard.Lock

At the same time stale-bind cleanup used to retain shard.RLock while waiting
for every transaction mutex:

activeTxnHolder shard.RLock -> activeTxn.Lock

The two production goroutine profiles, captured five hours apart, retain the
same opposing stacks. The deadlock also holds bindChangeMu.Lock, so new lock
requests accumulate behind service.Lock, making live transaction handles look
like a growing txn leak.

This PR:

- snapshots transaction keys under each holder shard;
- uses TryLock while the shard protects the pooled transaction pointer;
- releases the shard and retries on contention, allowing unknown-commit deletion
to complete;
- preserves the existing stale-bind fence behavior once the transaction lock is
acquired;
- adds deterministic regression coverage for the exact unknown-cleanup/bind-fence
interleaving and for the holder lock-order invariant.

Normal lock, unlock, Commit, allocator-fence, and proxy handoff semantics are
unchanged. The retry path is reached only during abnormal bind fencing when a
transaction mutex is already busy.

Validation:

- pre-fix regression: deterministic timeout with
unlockUnknownCommit -> deleteActiveTxn -> shard.Lock opposing
fenceByBindChanged -> activeTxn.Lock;
- go test -race ./pkg/lockservice -run '^(TestUnknownCommitCleanupDoesNotDeadlockBindFence|TestMapBasedTxnHolderFenceReleasesShardWhileTxnIsBusy)$' -count=100 -timeout=5m;
- go test ./pkg/lockservice -count=1 -timeout=10m;
- go test -race ./pkg/lockservice -count=1 -timeout=15m;
- go vet ./pkg/lockservice.

Approved by: @iamlinjunhong, @XuPeng-SH

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants