Repository navigation
fix(lockservice): prevent fixedSlice refcount resurrection - #27928
Conversation
XuPeng-SH
left a comment
There was a problem hiding this comment.
The zero-ref non-resurrection design fixes the interleaving in #27884, but the stricter lifetime rule exposes a deterministic use-after-close in the existing cleanup path.
closeWithContextInternal calls removeClosedLockTable(...) on a successful detach (or a non-local direct unlock), which synchronously executes cs.close() and resets the current fixedSlice to ref=0. It then calls logTxnUnlockTableCompleted(..., cs); with debug logging enabled that function calls cs.slice().
With this patch, if the pooled cowSlice has not yet been reused, tryRef() keeps returning false and slice() spins forever, so Unlock/cleanup (and a parallel cleanup's WaitGroup) never completes. If the cowSlice is reused before the log call, the log can instead read an unrelated transaction generation. The new tests cover an already-acquired reader surviving close, but not a read started after close, which is the production ordering here.
Please log/capture the retained slice before removeClosedLockTable frees the cowSlice (or otherwise enforce a generation-safe closed-state contract), and add a deterministic regression test for the successful-detach + debug-log ordering.
|
Addressed in commit The completion log now reads the The exact-base read-only overlay reproduces the original zero-ref duplicate-release counterexample, while the current candidate passes the close-order regression. Please re-review this head; a fresh Linux/arm64 two-CN smoke remains required for QA. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep-reviewed exact head 9ac0d9f against exact base b23beee from the refcount, generation/ABA, pool ownership, close ordering, retry, starvation, map-mutation, logging, performance, and unhappy-path angles.
The design establishes the required invariant: zero is terminal for one fixedSlice generation, only pool acquisition may claim 0 to 1, and a reader may increment only a positive generation. A stale reader racing replacement cannot resurrect a retired generation; if the address has already been reused, its balanced transient ref applies to the new positive generation and the cowSlice version check prevents returning it as old data. The unique 1 to 0 transition remains the only cleanup/pool-return owner.
The latest close-order fix closes my earlier blocker: completion logging acquires and releases its reader reference before successful detach releases the cowSlice owner. Retryable detach remains sequential, while the parallel release path does not concurrently mutate holder maps. I found no remaining leak, double-release, use-after-close, wait cycle, busy-spin production path, or material hot-path regression. This is a focused internal bug fix and does not require the feature design gate.
Exact-head diff check passed. Focused fixedSlice/cowSlice and production close regressions passed under race detection for 50 iterations, and the complete pkg/lockservice suite passed locally in 99.753s. Latest substantive CI is terminal green. Formal Linux/arm64 multi-CN QA remains appropriate for this crash-class change but is not a code-review blocker.
Merge Queue Status
This pull request spent 2 minutes 23 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull request #27928 has been dequeued Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.
Failing checks:
HintYou should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. Tick the box to put this pull request back in the merge queue (same as
|
…gin#27928) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#27884 ## What this PR does / why we need it: `cowSlice.slice` could increment a `fixedSlice` reference count after a concurrent COW replacement had retired the object and reduced its count to zero. The reader could therefore resurrect a pooled generation and, after the version check failed, return the same generation to the pool a second time. That duplicate pool ownership can alias live owners and corrupt the slice seen by `fixedSlice.append`, causing a CN panic. Changes: - Add `tryRef`, which acquires a transient reader reference only from a positive count with a CAS loop. - Make `initRef` the only `0 -> 1` pool-generation claim and reject non-retired objects. - Retry `cowSlice.slice` when the observed generation is already retired, and symmetrically `unref` a reference when version validation loses its CAS race. - Keep the unique `1 -> 0` transition as the only cleanup and pool return path. - Read the transaction completion log through `slice()` before a successful close path detaches and releases its `cowSlice` owner; this prevents a post-close debug logger from spinning on a zero-reference slice. Non-goals: - No public API, RPC, configuration, catalog, wire schema, or persisted-format change. - No FULLTEXT2 cache-coherence behavior from matrixorigin#27806 is implemented here; this is the independent lockservice crash blocker found while exercising that distributed path. Validation: - Exact PR base: `b23beeea896f574a57628b44fb769cda3775cb84`; current source patch SHA-256: `ddd7beb2aa819ee716a1ba0a70a8169c91c88fe81dcca6b1d56d886eff138ed5`. - Base behavior: on exact base `b23beeea896f574a57628b44fb769cda3775cb84`, a read-only oracle overlay that moves `fs.ref()` after the existing slice hook deterministically reproduces duplicate release counts (`expected 1/2`, observed `2/3`) in `TestCowSliceSliceReadConcurrentWithAppend`. - Candidate behavior: 16 focused lifecycle tests (15 fixedSlice/cowSlice tests plus `TestCloseWithoutFreeWithContextLogsBeforeSliceRelease`) passed; every focused test passed independently with `-race -count=100`; complete `pkg/lockservice` normal and race suites passed. The close-order regression uses the production transaction close path and a one-second bounded-return guard, and now asserts `txn.lockHolders` is empty plus `fsp.releaseV == 1`. - Test-authenticity attack: a read-only overlay deleting the successful `removeClosedLockTable` call fails at the holder-empty assertion; the prior log-only test passed that same no-detach mutation. - Build gates: `go list ./pkg/lockservice`, `go build ./pkg/lockservice`, `go vet ./pkg/lockservice`, `git diff --check`, and the repository CGo-contract `cmd/mo-service` host build passed. The local build was Darwin/arm64 with binary SHA-256 `12747dc2bc0f26e11dfbc0cd2c79b7a6d02a93b166ab9438f3482b418aa722cb`. - Benchmark: `BenchmarkCowSliceRead` used 15 samples per side on the same Darwin/arm64 host and implementation; base median `16.80 ns/op`, candidate median `16.93 ns/op` (`+0.77%`, within the 10% gate); both sides reported `0 B/op` and `0 allocs/op`. - Semantic preflight: clean schema-v6 manifest at head `9ac0d9fbf01192bca0ba6ecacebe7cf0aab7e8e6`, diff hash `8df3051771ab199391d8e3782a3e7da550bf723bd8eab84b2f8f3192b31aab4d`, validator exact output `PASS semantic=PASS`. - Manual two-CN: the earlier Linux/arm64 binary and two-CN run predates the close-order fix and is intentionally superseded. No current two-CN PASS is claimed for this head; a fresh user-run Linux/arm64 smoke is required before QA acceptance. - BVT: N/A — the zero-reference linearization and close/log ordering are internal lockservice interleavings that SQL/BVT cannot deterministically control. Equivalent regression coverage uses the production-path barrier tests, close-order regression, and race stress listed above. - QA required: Yes — this shared-state lifecycle defect can crash or hang a CN, and formal Linux/arm64 multi-CN verification remains outstanding. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…gin#27928) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#27884 ## What this PR does / why we need it: `cowSlice.slice` could increment a `fixedSlice` reference count after a concurrent COW replacement had retired the object and reduced its count to zero. The reader could therefore resurrect a pooled generation and, after the version check failed, return the same generation to the pool a second time. That duplicate pool ownership can alias live owners and corrupt the slice seen by `fixedSlice.append`, causing a CN panic. Changes: - Add `tryRef`, which acquires a transient reader reference only from a positive count with a CAS loop. - Make `initRef` the only `0 -> 1` pool-generation claim and reject non-retired objects. - Retry `cowSlice.slice` when the observed generation is already retired, and symmetrically `unref` a reference when version validation loses its CAS race. - Keep the unique `1 -> 0` transition as the only cleanup and pool return path. - Read the transaction completion log through `slice()` before a successful close path detaches and releases its `cowSlice` owner; this prevents a post-close debug logger from spinning on a zero-reference slice. Non-goals: - No public API, RPC, configuration, catalog, wire schema, or persisted-format change. - No FULLTEXT2 cache-coherence behavior from matrixorigin#27806 is implemented here; this is the independent lockservice crash blocker found while exercising that distributed path. Validation: - Exact PR base: `b23beeea896f574a57628b44fb769cda3775cb84`; current source patch SHA-256: `ddd7beb2aa819ee716a1ba0a70a8169c91c88fe81dcca6b1d56d886eff138ed5`. - Base behavior: on exact base `b23beeea896f574a57628b44fb769cda3775cb84`, a read-only oracle overlay that moves `fs.ref()` after the existing slice hook deterministically reproduces duplicate release counts (`expected 1/2`, observed `2/3`) in `TestCowSliceSliceReadConcurrentWithAppend`. - Candidate behavior: 16 focused lifecycle tests (15 fixedSlice/cowSlice tests plus `TestCloseWithoutFreeWithContextLogsBeforeSliceRelease`) passed; every focused test passed independently with `-race -count=100`; complete `pkg/lockservice` normal and race suites passed. The close-order regression uses the production transaction close path and a one-second bounded-return guard, and now asserts `txn.lockHolders` is empty plus `fsp.releaseV == 1`. - Test-authenticity attack: a read-only overlay deleting the successful `removeClosedLockTable` call fails at the holder-empty assertion; the prior log-only test passed that same no-detach mutation. - Build gates: `go list ./pkg/lockservice`, `go build ./pkg/lockservice`, `go vet ./pkg/lockservice`, `git diff --check`, and the repository CGo-contract `cmd/mo-service` host build passed. The local build was Darwin/arm64 with binary SHA-256 `12747dc2bc0f26e11dfbc0cd2c79b7a6d02a93b166ab9438f3482b418aa722cb`. - Benchmark: `BenchmarkCowSliceRead` used 15 samples per side on the same Darwin/arm64 host and implementation; base median `16.80 ns/op`, candidate median `16.93 ns/op` (`+0.77%`, within the 10% gate); both sides reported `0 B/op` and `0 allocs/op`. - Semantic preflight: clean schema-v6 manifest at head `9ac0d9fbf01192bca0ba6ecacebe7cf0aab7e8e6`, diff hash `8df3051771ab199391d8e3782a3e7da550bf723bd8eab84b2f8f3192b31aab4d`, validator exact output `PASS semantic=PASS`. - Manual two-CN: the earlier Linux/arm64 binary and two-CN run predates the close-order fix and is intentionally superseded. No current two-CN PASS is claimed for this head; a fresh user-run Linux/arm64 smoke is required before QA acceptance. - BVT: N/A — the zero-reference linearization and close/log ordering are internal lockservice interleavings that SQL/BVT cannot deterministically control. Equivalent regression coverage uses the production-path barrier tests, close-order regression, and race stress listed above. - QA required: Yes — this shared-state lifecycle defect can crash or hang a CN, and formal Linux/arm64 multi-CN verification remains outstanding. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…gin#27928) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#27884 ## What this PR does / why we need it: `cowSlice.slice` could increment a `fixedSlice` reference count after a concurrent COW replacement had retired the object and reduced its count to zero. The reader could therefore resurrect a pooled generation and, after the version check failed, return the same generation to the pool a second time. That duplicate pool ownership can alias live owners and corrupt the slice seen by `fixedSlice.append`, causing a CN panic. Changes: - Add `tryRef`, which acquires a transient reader reference only from a positive count with a CAS loop. - Make `initRef` the only `0 -> 1` pool-generation claim and reject non-retired objects. - Retry `cowSlice.slice` when the observed generation is already retired, and symmetrically `unref` a reference when version validation loses its CAS race. - Keep the unique `1 -> 0` transition as the only cleanup and pool return path. - Read the transaction completion log through `slice()` before a successful close path detaches and releases its `cowSlice` owner; this prevents a post-close debug logger from spinning on a zero-reference slice. Non-goals: - No public API, RPC, configuration, catalog, wire schema, or persisted-format change. - No FULLTEXT2 cache-coherence behavior from matrixorigin#27806 is implemented here; this is the independent lockservice crash blocker found while exercising that distributed path. Validation: - Exact PR base: `b23beeea896f574a57628b44fb769cda3775cb84`; current source patch SHA-256: `ddd7beb2aa819ee716a1ba0a70a8169c91c88fe81dcca6b1d56d886eff138ed5`. - Base behavior: on exact base `b23beeea896f574a57628b44fb769cda3775cb84`, a read-only oracle overlay that moves `fs.ref()` after the existing slice hook deterministically reproduces duplicate release counts (`expected 1/2`, observed `2/3`) in `TestCowSliceSliceReadConcurrentWithAppend`. - Candidate behavior: 16 focused lifecycle tests (15 fixedSlice/cowSlice tests plus `TestCloseWithoutFreeWithContextLogsBeforeSliceRelease`) passed; every focused test passed independently with `-race -count=100`; complete `pkg/lockservice` normal and race suites passed. The close-order regression uses the production transaction close path and a one-second bounded-return guard, and now asserts `txn.lockHolders` is empty plus `fsp.releaseV == 1`. - Test-authenticity attack: a read-only overlay deleting the successful `removeClosedLockTable` call fails at the holder-empty assertion; the prior log-only test passed that same no-detach mutation. - Build gates: `go list ./pkg/lockservice`, `go build ./pkg/lockservice`, `go vet ./pkg/lockservice`, `git diff --check`, and the repository CGo-contract `cmd/mo-service` host build passed. The local build was Darwin/arm64 with binary SHA-256 `12747dc2bc0f26e11dfbc0cd2c79b7a6d02a93b166ab9438f3482b418aa722cb`. - Benchmark: `BenchmarkCowSliceRead` used 15 samples per side on the same Darwin/arm64 host and implementation; base median `16.80 ns/op`, candidate median `16.93 ns/op` (`+0.77%`, within the 10% gate); both sides reported `0 B/op` and `0 allocs/op`. - Semantic preflight: clean schema-v6 manifest at head `9ac0d9fbf01192bca0ba6ecacebe7cf0aab7e8e6`, diff hash `8df3051771ab199391d8e3782a3e7da550bf723bd8eab84b2f8f3192b31aab4d`, validator exact output `PASS semantic=PASS`. - Manual two-CN: the earlier Linux/arm64 binary and two-CN run predates the close-order fix and is intentionally superseded. No current two-CN PASS is claimed for this head; a fresh user-run Linux/arm64 smoke is required before QA acceptance. - BVT: N/A — the zero-reference linearization and close/log ordering are internal lockservice interleavings that SQL/BVT cannot deterministically control. Equivalent regression coverage uses the production-path barrier tests, close-order regression, and race stress listed above. - QA required: Yes — this shared-state lifecycle defect can crash or hang a CN, and formal Linux/arm64 multi-CN verification remains outstanding. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…33206) ## What type of PR is this? - [x] BUG - [x] Test and CI ## Which issue(s) this PR fixes: Related to #27884. Backports #27928 to `4.2-dev`. ## What this PR does / why we need it: A reader must not take a reference to a retired fixedSlice while its copy-on-write owner publishes a replacement. Initialize references at pool checkout, acquire reader references only while the count is positive, and complete transaction unlock logging before detaching the lock slice on the 4.2 close path. Base `bb197e5118507d830f5e50f99a2676bb88a8d62f`, head `a15820bab01714bad98a65785a7497270066b4d5`. Focused lockservice tests passed on Go 1.26.4/darwin-arm64, including deterministic reader/publication interleavings, pool ownership and debug logging during transaction detach. `git diff --check` passed. BVT: N/A for the deterministic regression. SQL cannot suspend the copy-on-write reader between pointer load and reference acquisition or inspect the pool's reference ownership. The lockservice tests drive those transitions and verify the final state directly. QA required: yes. Cross-CN lock churn and rolling restart still need release validation. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
What type of PR is this?
Which issue(s) this PR fixes:
issue #27884
What this PR does / why we need it:
cowSlice.slicecould increment afixedSlicereference count after a concurrent COW replacement had retired the object and reduced its count to zero. The reader could therefore resurrect a pooled generation and, after the version check failed, return the same generation to the pool a second time. That duplicate pool ownership can alias live owners and corrupt the slice seen byfixedSlice.append, causing a CN panic.Changes:
tryRef, which acquires a transient reader reference only from a positive count with a CAS loop.initRefthe only0 -> 1pool-generation claim and reject non-retired objects.cowSlice.slicewhen the observed generation is already retired, and symmetricallyunrefa reference when version validation loses its CAS race.1 -> 0transition as the only cleanup and pool return path.slice()before a successful close path detaches and releases itscowSliceowner; this prevents a post-close debug logger from spinning on a zero-reference slice.Non-goals:
Validation:
b23beeea896f574a57628b44fb769cda3775cb84; current source patch SHA-256:ddd7beb2aa819ee716a1ba0a70a8169c91c88fe81dcca6b1d56d886eff138ed5.b23beeea896f574a57628b44fb769cda3775cb84, a read-only oracle overlay that movesfs.ref()after the existing slice hook deterministically reproduces duplicate release counts (expected 1/2, observed2/3) inTestCowSliceSliceReadConcurrentWithAppend.TestCloseWithoutFreeWithContextLogsBeforeSliceRelease) passed; every focused test passed independently with-race -count=100; completepkg/lockservicenormal and race suites passed. The close-order regression uses the production transaction close path and a one-second bounded-return guard, and now assertstxn.lockHoldersis empty plusfsp.releaseV == 1.removeClosedLockTablecall fails at the holder-empty assertion; the prior log-only test passed that same no-detach mutation.go list ./pkg/lockservice,go build ./pkg/lockservice,go vet ./pkg/lockservice,git diff --check, and the repository CGo-contractcmd/mo-servicehost build passed. The local build was Darwin/arm64 with binary SHA-25612747dc2bc0f26e11dfbc0cd2c79b7a6d02a93b166ab9438f3482b418aa722cb.BenchmarkCowSliceReadused 15 samples per side on the same Darwin/arm64 host and implementation; base median16.80 ns/op, candidate median16.93 ns/op(+0.77%, within the 10% gate); both sides reported0 B/opand0 allocs/op.9ac0d9fbf01192bca0ba6ecacebe7cf0aab7e8e6, diff hash8df3051771ab199391d8e3782a3e7da550bf723bd8eab84b2f8f3192b31aab4d, validator exact outputPASS semantic=PASS.