Skip to content

fix(lockservice): enforce lock wait timeout - #24340

Closed
LeftHandCold wants to merge 1 commit into
matrixorigin:3.0-devfrom
LeftHandCold:fix-lock-wait-timeout-3.0-dev
Closed

LeftHandCold wants to merge 1 commit into
matrixorigin:3.0-devfrom
LeftHandCold:fix-lock-wait-timeout-3.0-dev

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:

issue #24333

What this PR does / why we need it:

Fixes a stability failure mode where a multi-statement pessimistic transaction can keep locks acquired by earlier statements while a later statement waits on another row lock indefinitely.

The root cause is that the lock wait path only observes holder notifications or the outer statement context. Although lock_wait_timeout existed, it was not applied to SQL lock acquisition, and its default was effectively unbounded for stability scenarios (31536000 seconds). In issue #24333 this allowed one blocked statement to keep earlier locks for nearly 20 hours, causing waiter buildup and eventually saturating CN active txn capacity.

This PR:

  • applies lock_wait_timeout to pessimistic lock acquisition in lockop, without changing the lockservice protobuf/RPC API,
  • preserves any shorter existing statement/client deadline instead of extending or masking it,
  • converts only the lock-wait-owned deadline into a dedicated ErrLockWaitTimeout using MySQL error 1205 text,
  • treats ErrLockWaitTimeout as a whole-transaction rollback error so explicit multi-statement transactions release locks acquired by previous statements,
  • changes the default lock_wait_timeout to 50 seconds, matching MySQL's default and preventing effectively infinite waits,
  • adds unit coverage for timeout parsing, lock-wait timeout error conversion, shorter caller deadline preservation, and frontend whole-txn rollback classification.

Validation

PROJECTROOT=/Users/shenjiangwei/Work/code/view/matrixone
export CGO_CFLAGS="-I$PROJECTROOT/cgo -I$PROJECTROOT/thirdparties/install/include"
export CGO_LDFLAGS="-Wl,-w,-rpath,$PROJECTROOT/thirdparties/install/lib -L$PROJECTROOT/thirdparties/install/lib -L$PROJECTROOT/cgo -lmo"
GOFLAGS=-mod=mod go test ./pkg/sql/colexec/lockop ./pkg/frontend ./pkg/common/moerr -run 'TestLock|TestConvertLockWaitTimeoutError|TestContextWithLockWaitTimeoutKeepsShorterExistingDeadline|TestLockWaitTimeoutFromValue|Test_isErrorRollbackWholeTxn|TestErrors' -count=1
GOFLAGS=-mod=mod go test ./pkg/sql/colexec/lockop ./pkg/frontend ./pkg/common/moerr -count=1
GOFLAGS=-mod=mod go test ./pkg/lockservice -count=1

Copilot AI review requested due to automatic review settings May 11, 2026 08:05
@qodo-code-review

Copy link
Copy Markdown
ⓘ You've reached your Qodo monthly free-tier limit. Reviews pause until next month — upgrade your plan to continue now, or link your paid account if you already have one.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR enforces lock_wait_timeout during pessimistic row/table lock acquisition to prevent unbounded lock waits from keeping earlier statement locks held indefinitely in multi-statement transactions, and introduces a dedicated ErrLockWaitTimeout (MySQL 1205) that triggers whole-transaction rollback behavior.

Changes:

  • Apply lock_wait_timeout via a dedicated lock-acquire context and convert lock-owned deadline exceed to ErrLockWaitTimeout.
  • Treat ErrLockWaitTimeout as a whole-transaction rollback error and change the default lock_wait_timeout to 50s.
  • Add unit tests covering timeout parsing, error conversion, deadline preservation, and rollback classification.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/sql/colexec/lockop/lock_op.go Adds lock-wait timeout context, variable resolution/parsing, and timeout error conversion to ErrLockWaitTimeout.
pkg/sql/colexec/lockop/lock_op_test.go Adds unit tests for timeout parsing, context deadline preservation, and error conversion.
pkg/frontend/variables.go Changes default lock_wait_timeout from 31536000s to 50s.
pkg/frontend/util.go Classifies ErrLockWaitTimeout as triggering whole-transaction rollback.
pkg/frontend/util_test.go Extends rollback classification tests to include ErrLockWaitTimeout.
pkg/common/moerr/error.go Introduces new error code ErrLockWaitTimeout with MySQL errno 1205 message and constructors.
pkg/common/moerr/error_no_ctx.go Adds NewLockWaitTimeoutNoCtx() constructor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/sql/colexec/lockop/lock_op.go Resolved
Apply lock_wait_timeout to pessimistic lock acquisition so a blocked lock wait cannot keep a multi-statement transaction alive indefinitely. Convert lock wait deadline expiry into a dedicated lock timeout error that rolls back the whole transaction, and align the default with MySQL's 50 seconds.

Co-authored-by: Copilot <[email protected]>
@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Closing this mitigation PR for now. Further investigation points to the root cause being in lockservice/morpc remote response write failure and caller wakeup/timeout semantics introduced around #24206, so I will replace this with a targeted root-cause fix PR.

mergify Bot pushed a commit that referenced this pull request May 12, 2026
This fixes the remote lock caller hang exposed by #24333 and follows up on #24206.

#24206 correctly tightened stale remote bind handling by moving remote keepalive and orphan cleanup to bind granularity, but it also changed remote lock and forward-lock responses from `AsyncWrite` to bounded synchronous `cs.Write`. When that bounded write fails, the old path only returned the error to the handler. The peer can then stay blocked in `remoteLockTable.lock -> client.Send -> Future.Get` because no response is delivered and the morpc session remains open.

This PR closes the lockservice RPC client session on any response write failure. That converts a local response-write failure into a deterministic peer-side connection error, so pending futures fail and the waiting transaction follows the existing rollback and unlock path instead of holding locks indefinitely.

I intentionally do not roll back remote lock state from the write-failure path itself. After a write or flush failure, response delivery is ambiguous; unilateral unlock on the server side is riskier because the caller may still observe success and continue without a valid remote lock.

Also included:
- richer remote lock write-failure logs with request id, method, remote endpoint, txn id, bind, caller service, and row count
- a regression test that verifies write failure closes the session while successful writes do not
- no `lock_wait_timeout` mitigation from #24340 in this PR

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.

3 participants