Skip to content

[4.2] fix(lockservice): avoid bind fence cleanup deadlock - #27569

Merged
mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
LeftHandCold:backport/4.2-lockservice-bind-fence-cleanup-deadlock
Aug 25, 2026
Merged

mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
LeftHandCold:backport/4.2-lockservice-bind-fence-cleanup-deadlock

Conversation

@LeftHandCold

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:

Fixes #27562

Backport of #26749 to 4.2-dev.

What this PR does / why we need it:

After a TN failure produces an unknown commit, unlockUnknownCommit can retain activeTxn.Lock while deleting the transaction from its holder shard:

activeTxn.Lock -> activeTxnHolder shard.Lock

At the same time stale-bind fencing in the affected 4.2 code retains shard.RLock while waiting for the transaction mutex:

activeTxnHolder shard.RLock -> activeTxn.Lock

The bind-fence path also holds bindChangeMu.Lock, so the inversion blocks every later service.Lock request at bindChangeMu.RLock. Production goroutine profiles from #27562 contain both opposing stacks for the same five-minute interval and accumulating lock requests; restarting the affected CN clears the process-local deadlock.

This backport snapshots transaction keys under each holder shard, uses TryLock while the shard protects the pooled transaction pointer, and releases the shard before retrying a busy transaction. This lets unknown-commit deletion finish without changing normal Lock, Unlock, Commit, allocator-fence, or proxy semantics.

The cherry-pick applied cleanly to current 4.2-dev; its patch-id is identical to the merged main fix.

Validation:

  • go build -mod=readonly ./pkg/lockservice
  • go vet -mod=readonly ./pkg/lockservice
  • focused regression tests selected explicitly
  • both focused regressions with -race -count=100
  • complete pkg/lockservice test package
  • complete pkg/lockservice test package with -race

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

(cherry picked from commit 6798bd6)
@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 →

@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.

Deep-reviewed exact head 29147fadb7570318f5fbe28d25f62fb3f1c60855 against 4.2-dev.

The revised lock order is sound: the holder shard protects the pooled transaction pointer; a failed TryLock releases the shard before retrying, allowing unknown-commit cleanup (txn lock -> shard delete) to complete. A successful claim holds both protections only while inspecting/fencing transaction-owned state. Snapshotting keys does not miss a transaction with old-bind locks because lock acquisition is serialized by bindChangeMu; a transaction inserted before obtaining that read lock has not yet acquired old-bind state.

I also checked delete/reuse, rebind, waiter notification, and new-transaction interleavings. The backport's stable patch-id exactly matches the merged main fix commit. Both focused deadlock regressions pass under the deterministic CGo wrapper with -race -count=20; CI is green. No blocking design or implementation issue found.

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

mergify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-25 05:40 UTC · Rule: release-4.2 · triggered by rule Automatic queue on approval for release-4.2
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-25 05:41 UTC · at 29147fadb7570318f5fbe28d25f62fb3f1c60855 · squash

This pull request spent 38 seconds in the queue, including 3 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 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 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 Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • 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 Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

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.

3 participants