Skip to content

fix(lockservice): backport stale empty-lock recovery to 4.2-dev - #28529

Merged
XuPeng-SH merged 8 commits into
matrixorigin:4.2-devfrom
VioletQwQ-0:backport-lockservice-empty-lock-4.2-dev-20260909
Sep 15, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:4.2-devfrom
VioletQwQ-0:backport-lockservice-empty-lock-4.2-dev-20260909

Conversation

@VioletQwQ-0

@VioletQwQ-0 VioletQwQ-0 commented Sep 9, 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 #27291
issue #27293

What this PR does / why we need it:

This is a focused backport of the merged upstream #27296 fix to 4.2-dev.

A stale empty row/range lock can reach tryHold. The old 4.2-dev path panics (BUG: try hold on empty lock) and can leave the following admission blocked. This backport:

  • returns errEmptyLock instead of panicking;
  • removes or repairs stale row/range endpoints before retry;
  • preserves structural range pairing and merge/restart behavior;
  • adds direct lockservice regression coverage.

It also closes the empty-endpoint lifecycle hole in the batch cleanup path. closeRangeWaiterLocked can snapshot multiple empty endpoints before deleting them; deleteEmptyLockLocked re-reads the current store entry and verifies the value, creation timestamp, and backing holders/waiter identity before dereferencing or releasing state.

The current formal P1 repair closes the remaining Start-first orphan sequence. For a live [1,10] plus orphan End(5) and Start(7), structural cleanup now crosses an unmatched orphan end and rejects a candidate End that already has an earlier matching Start. This prevents either cleanup order from copying the live pooled state into an orphan key. The regression uses real service.Lock([1,10]), both Start-first and End-first cleanup orders, real owner Unlock, returned-state traps, and an empty-store assertion.

The companion remote-bind lifecycle fixes #28115 and #28343 are intentionally not included. They address departed-owner/CN-replacement fencing, require an adapted ordered backport, and were not established as the cause of this incident. They should be reviewed separately.

QA required: yes. This changes distributed LockService/CN failure and cleanup behavior; after merge it requires dev two-CN validation of the original panic path and post-cleanup admission.

Validation

  • Exact tested base: matrixorigin/matrixone:4.2-dev at b122da6dc20985cbfcb2fcc642ae6729d70d8516.
  • Current backport head: babbdc7bdf0bedec18822a3f774fc48f21644e94.
  • Exact committed base-to-head diff SHA-256: 4196ac635372fa22eec6a7bab304d1cc43b239a569e82f4e869b12981e79df6a.
  • Schema-v7 semantic preflight: PASS; exact-content hash a58df1faf1c94c16a9151454c6cf2caa8174bd19b3f16c6af184ab2fb032de82.
  • Red/green P1 proof: the new Start-first regression failed on the prior exact head 551700dfad0ecf97d9f6acd2e4f66d6e5f74eb29 because the orphan endpoint remained; the repaired head passes.
  • Focused LockService structural/cleanup set, including both cleanup orders, enclosing/multiple/mixed orphan endpoints, mismatched repair, stale batch snapshots, merge restart, waiter-reference clearing, and interleaved range locks: PASS.
  • New regression after exact-base rebase: normal PASS; focused -race -count=100 PASS with 100 pass records and no race report.
  • Full ./pkg/lockservice normal: PASS (111.826s). Full ./pkg/lockservice -race: PASS (115.861s).
  • go list -mod=readonly ./pkg/lockservice ./pkg/sql/colexec/lockop: PASS.
  • gofmt and git diff --check: PASS.
  • mo-cgo-test clean-worktree wrapper: BLOCKED_ENVIRONMENT because this candidate worktree lacks thirdparties/install. Controlled direct CGo execution with explicit native paths passed the lockservice normal/race tests. go build/go vet for lockservice+lockop remain environment-blocked in pkg/common/docfilter by unavailable C.mo_cbitmap_*/C.mo_croaring_* bindings; no build/vet PASS is claimed.
  • BVT: N/A for this internal LockService/CN lifecycle fix; there is no SQL result contract reachable through BVT. The equivalent package regression is provided, and the required post-merge two-CN SQL/admission QA is called out above.
  • New-head required GitHub checks are running after this update; prior green checks belonged to an older head and are not reused as current evidence.

The PR does not modify GitOps/Kubernetes or deploy to dev. Full dev rollout and SQL validation remain post-merge QA gates.

@mergify
mergify Bot requested a review from XuPeng-SH September 9, 2026 09:58
@mergify mergify Bot added the kind/bug Something isn't working label Sep 9, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Sep 9, 2026
@VioletQwQ-0
VioletQwQ-0 marked this pull request as ready for review September 10, 2026 02:19
@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 →

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Current head 15dd7324d1d9217adb4e7d49fb64a958cad1d462, run 34461316425: the failure is a workflow setup/tooling failure, not a lockservice test failure. Classify CI changes and CI Required stop before test execution because .github/ci/change-scope.cjs is missing on the 4.2-dev target workflow. No code-test conclusion is available and no rerun was forced.

XuPeng-SH added a commit that referenced this pull request Sep 11, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Related to #28419 (CI Required / coverage failure reporting)
Related to #27291 and #27293 through #28529's CI startup blocker

## What this PR does / why we need it:

`pull_request_target` currently checks out `.github/ci` from the pull
request base SHA in the `change-scope` and `ci-required` jobs. A
`4.2-dev` pull request can therefore load a workflow that references
`change-scope.cjs` while its base commit does not contain that helper,
failing before product tests start with `Cannot find module
'.github/ci/change-scope.cjs'`.

Both jobs now load the repository-owned CI helper from `${{
github.workflow_sha }}`, the commit that supplied the running workflow.
The PR base and head SHAs remain event metadata only, and all checkout
credentials remain disabled. A checkout-independent provenance step
writes the workflow, base, and head SHAs before loading the helper, so
missing-helper or checkout failures retain diagnostics. No test routing,
required-check policy, or product code is changed.

The existing `3.0-dev` route and failure semantics remain unchanged.
This PR is intentionally separate from #28591, whose purpose is
cancelling doomed PR runs.

## Validation

- Baseline red check: the old workflow fails the trusted workflow-SHA
checkout contract.
- `node --test .github/ci/change-scope.test.cjs` (10/10 passed)
- The fixture parses the actual checkout configuration for both jobs,
resolves the event expressions against separate base/workflow/head
trees, and executes the selected workflow helper. The base tree lacks
the helper, the workflow tree contains the trusted classifier and
verifier, and the head tree contains a malicious same-name helper. Both
jobs select the workflow tree; the classifier and `verifyResults` are
executed from that same trusted version.
- The contract test rejects a floating `main` ref, requires provenance
before checkout, and executes the provenance shell against a simulated
checkout failure to verify the summary remains available.
- `actionlint v1.7.12 .github/workflows/entrypoint.yaml` (passed)
- `git diff --check` (passed)
- The first CI run was correctly blocked by an invalid PR reference to
#28529 (a PR, not an issue). The body now references valid issues
#28419, #27291, and #27293.
- After this PR merges, GitHub's `pull_request_target` behavior uses the
repository default-branch workflow source; a fresh #28529 run can
therefore be created directly without a `4.2-dev` CI-file backport. That
run must prove the workflow SHA contains this fix, classification
succeeds, and required tests actually start. See [GitHub's
change](https://github.blog/changelog/2025-11-07-actions-pull_request_target-and-environment-branch-protections-changes/).

QA required: no; this is a workflow/test-only change and does not alter
MatrixOne runtime behavior. Complete CI startup and classification
validation is a post-merge check.

---------

Co-authored-by: XuPeng-SH <[email protected]>
@VioletQwQ-0
VioletQwQ-0 marked this pull request as draft September 11, 2026 08:12
@VioletQwQ-0
VioletQwQ-0 marked this pull request as ready for review September 11, 2026 08:13
@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.

审查范围:当前 head c198714fae55dce5c2f782e18b9eb01486f12ced,基线 4.2-dev@b122da6dc20985cbfcb2fcc642ae6729d70d8516;已核对完整 7 文件 diff、相关调用链、测试、上一轮 review/thread 及 PR conversation。

结论:当前未发现 P0/P1。上一轮提出的 P1(空 range-end 误接纳 enclosing live range)已由当前 findStructuralRangePair / hasFollowingRangePairLocked 的对称检查修复,TestEmptyRangeEndDoesNotSpliceEnclosingLiveRange 通过。

[P2] pkg/lockservice/lock_table_local.go:1323:mergeContext.restart 仅执行 c.mergedWaiters = c.mergedWaiters[:0],没有清空 backing slots。当 range scan 已通过 mergeWaiter 暂存一个或多个 waiter queue、随后命中 errEmptyLock 时会走此路径;mergeContext 随后经 defer mc.close() 回收到 sync.Pool,旧 slot 仍持有 waiter-queue 引用及其历史 backing capacity。重复 stale-empty 重试或高 waiter 数场景会因此延长这些对象/内存的保留时间,直到 pooled context 被覆盖或回收;close 的同样 reslice 也会保留该引用。建议在缩短切片前 clear,并增加 backing slot 已清空的回归断言。

验证:go test ./pkg/lockservice -count=1、目标 -race 测试、go list -mod=readonly、go vet -mod=readonly 和 git diff --check 通过。lockop 目标测试受环境缺少 roaring.h、xxhash.h、usearch.h 等本地原生头文件阻塞,未据此宣称通过。

按“仅有 P2、首次 review”规则,本次事件为 COMMENT。

@aunjgr aunjgr 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 revision: c198714fae55dce5c2f782e18b9eb01486f12ced.

Independent review against 4.2-dev: no blocking finding. Checked all tryHold callers, current-entry identity validation before pooled-state access, paired/orphan/enclosing range cleanup, rollback of speculative waiter references before rescan, and real unlock/admission regressions. Also checked the LockOp default version checker and independent shared targets.

Reused exact-head CI run 34833159607. Older local pending-validation statements in the PR body are not treated as current CI failures. The separately scoped departed-owner/CN-replacement changes are not required by this approval.

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

GPT-6 medium deep review at exact head c198714.

P1 — multiple orphan range ends can leave pooled live state reachable after owner unlock. In pkg/lockservice/lock_table_local.go:1270–1278, consider a live range [1,10] plus empty orphan ends at 5 and 7. Cleanup of 5 finds start 1, while the pairing scan stops at orphan 7 and fails to find the real end 10; recovery then creates end 5 using the live range's backing state. Cleanup of 7 removes only 7. The owner's normal unlock removes 1 and 10 and returns the backing state to pools, but key 5 remains in the store pointing at that returned state. Extend the regression to clean both orphan ends and then perform normal owner unlock; pairing must prove the predecessor state has no surviving counterpart before adopting it.

The exact-head CI is green. The published artifact does not provide per-test timing for the new cases, so the 45-second limit is not certified.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Addressed the latest lifecycle findings on head 7efb31885003217ccbb2d8a28317f5fbd73f6459.

  • hasFollowingRangePairLocked now continues past mismatched orphan range-end entries and stops at the next range-start boundary. It only treats a predecessor as already paired when a later end has the same backing state, so an orphan cannot adopt a live enclosing range state.
  • Added TestMultipleEmptyRangeEndsDoNotRetainLiveStateAfterUnlock, which drives the actual batch cleanup path with two orphan ends, then performs the owner unlock and asserts the store is empty. The old implementation reproduced one residual entry; the patched implementation passes.
  • mergeContext.restart and mergeContext.close now clear the mergedWaiters backing slots before reslicing. Added TestMergeContextClearsMergedWaiterReferences to assert both paths release queue references.

Local validation on this exact head:

  • targeted tests, 10 repeats: PASS
  • targeted race tests, 10 repeats: PASS
  • related endpoint/cleanup race tests, 3 repeats: PASS
  • go test ./pkg/lockservice -count=1: PASS (107.8s)
  • go list, go build, go vet, git diff --check: PASS

The pushed CI run is 34920860184 and is still running. Please re-review the updated head once the checks complete.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Re-review request for current head 7efb31885003217ccbb2d8a28317f5fbd73f6459 against base 4.2-dev@b122da6dc20985cbfcb2fcc642ae6729d70d8516.

The P1 orphan-end repair is implemented in pkg/lockservice/lock_table_local.go: hasFollowingRangePairLocked now scans past mismatched orphan range ends, stops at the next range-start boundary, and pairs only when the backing holder/waiter state matches. The regression TestMultipleEmptyRangeEndsDoNotRetainLiveStateAfterUnlock covers two orphan ends followed by normal owner unlock and asserts that no key remains backed by returned state. The same change also clears pooled mergedWaiters references on both restart and close, covered by TestMergeContextClearsMergedWaiterReferences.

Exact-head local validation passed:

GOWORK=off go test -mod=readonly ./pkg/lockservice -run 'Test(MultipleEmptyRangeEndsDoNotRetainLiveStateAfterUnlock|MismatchedRangeEndpointRepairsLiveCounterpart|MergeContextClearsMergedWaiterReferences|EmptyRangeEndDoesNotSpliceEnclosingLiveRange)' -count=1 -timeout=10m

The current PR still has pending CI and the 45-second per-test timing requirement has not been certified by this local run. Please re-review the repaired P1 at the current head.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

CI follow-up for exact head 7efb31885003217ccbb2d8a28317f5fbd73f6459 on 4.2-dev@b122da6dc20985cbfcb2fcc642ae6729d70d8516.

Run 34920860184 completed with the changed lockservice-focused jobs and BVT/coverage/SCA otherwise green, but the Ubuntu UT aggregate failed in the unrelated existing pkg/vm/engine/test/TestCheckLeaseFailed: its ISCP watermark condition timed out (expected state was not reached; observed watermark remained 0-0). CI Required failed only as the derived result of that job. This is not evidence of a lockservice regression. I am rerunning the failed jobs once to classify whether it is transient; the formal review/QA gates remain unchanged.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

CI correction for the latest attempt of run 34920860184: Ubuntu UT job 104273461158 failed during reusable-workflow secret evaluation before any runner step or test started (missing EE_DB_* inputs; runner_id=0 / BlobNotFound). CI Required job 104273469317 is derived from that setup failure. The earlier note attributing this attempt to TestCheckLeaseFailed is superseded. No product-code change or blind rerun was made; the CI owner must restore the required secret inputs and rerun only the failed job. The formal review gate remains CHANGES_REQUESTED for XuPeng-SH re-review of head 7efb318.

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

GPT-6 medium deep review at exact head c6927c2.

P1 — mixed orphan endpoints can retain live pooled state after the range owner is unlocked. In pkg/lockservice/lock_table_local.go:1267-1268, using the PR stale-endpoint cleanup model with Start(1)->End(10) as the live range and orphan End(5), Start(7), cleanup can copy the same live holder into both orphan entries. The normal owner Unlock then removes keys 1 and 10 and returns that holder to the global pools, while keys 5 and 7 still reference it.

Consequence: store entries outlive holder ownership; pool reuse can make those entries access another lock's mutable state without its table mutex. The snapshot identity check does not prevent this because both snapshots are current when processed.

Please handle mixed end/start orphan sequences so a prospective counterpart cannot be an enclosing live range, and add a regression covering batch cleanup followed by normal owner unlock and an empty-store assertion. The current two-orphan-end regression does not cover this sequence.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Addressed XuPeng-SH's P1 review 5207128102 in 551700dfad0ecf97d9f6acd2e4f66d6e5f74eb29.

Root cause: during batch cleanup, a live range [1,10] could be treated as the structural counterpart for both an orphan End(5) and an orphan Start(7). Each orphan could then copy the live holders/waiter queue; owner unlock removed only [1,10], leaving keys 5 and 7 pointing at pooled state after release.

Fix: hasFollowingRangePairLocked now scans past intervening orphan range-starts and matches the predecessor's shared backing state before allowing an endpoint to pair. This rejects the enclosing live range as a counterpart for the mixed orphan sequence while preserving repair for an actually unpaired endpoint.

Regression: added TestMixedEmptyRangeEndpointsDoNotRetainLiveStateAfterUnlock. It creates [1,10] through service.Lock, forces batch cleanup of End(5) plus Start(7), calls the real owner Unlock, and asserts that both orphan states are returned and the store is empty. The prior exact head c6927c2d3beffdf21aa4947d9b227a7c5196bfcc fails with store.Len() == 2; the repaired head passes.

Validation: focused structural/cleanup tests PASS; the mixed regression passes -race -count=100; full ./pkg/lockservice normal and race runs, go list, go vet, git diff --check, and schema-v7 semantic preflight PASS. CI was not rerun here; the prior all-green result was for c6927c2d3beffdf21aa4947d9b227a7c5196bfcc, and the new head is left for the normal checks.

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

GPT-6 medium deep review at exact head 551700d\n\nP1 — Start-first cleanup can adopt an enclosing live range's pooled state.\n\nIn pkg/lockservice/lock_table_local.go:1228-1229, the backward scan stops at any preceding range-end, including an orphan. Concrete path: owner A has live [1,10], stale orphan End(5) and Start(7) exist; Lock(row 7) cleans Start(7), the backward scan stops at End(5), the forward scan accepts End(10), and repair copies its backing state into key 7. Retry sees A already owns that state, so no key 7 is recorded. Normal Unlock returns the state for keys 1 and 10 to the pool while key 7 still references it. Pool reuse can then expose another lock's mutable state through the wrong table mutex.\n\nRequired: prove the candidate end has no matching start and scan past orphan endpoints before adopting it. Add a regression using real Lock([1,10]), start-first cleanup through Lock(row 7), and owner Unlock; assert no surviving entry references returned state. Cover both cleanup orders, preserve genuinely unpaired-endpoint repair, and run focused race validation.\n\nExact-head CI passed, but this ownership counterexample remains.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Formal P1 repair is published at exact head babbdc7bdf0bedec18822a3f774fc48f21644e94, based on 4.2-dev@b122da6dc20985cbf2fcc642ae6729d70d8516.

The Start-first orphan sequence is closed: cleanup crosses unmatched End(5), rejects a candidate End with an earlier matching Start, and the real service.Lock/owner Unlock regression covers both Start-first and End-first orders with returned-state and empty-store assertions. The prior exact head reproduced the orphan leak; the new head passes the focused normal test, focused -race -count=100, the full pkg/lockservice normal/race runs, and schema-v7 semantic preflight.

The clean mo-cgo-test wrapper remains blocked by missing candidate thirdparties/install; controlled direct CGo test execution passed. New-head GitHub checks are running. Please re-review the current head; no merge or QA/deployment completion is claimed.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Correction to the previous 551700df validation note: its go vet PASS statement belonged to the earlier environment and is not reused for this head. The current PR body is authoritative: the clean CGo wrapper and static build/vet are environment-blocked by missing native prerequisites, while the controlled lockservice normal/race evidence and all claimed semantic gates are current for babbdc7bdf0bedec18822a3f774fc48f21644e94.

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

GPT-6 medium deep re-review APPROVE at exact head babbdc7\n\nAll changed hunks across eight files reviewed. Earlier orphan-endpoint ownership failures and pooled waiter-reference retention are resolved. Correctness, lifecycle, concurrency, liveness, boundedness, consumer, compatibility, and test closures were checked. Exact-head CI passed race UT, build/SCA, coverage, and active multi-CN BVT. No concrete blockers remain.

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 size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants