Skip to content

[Bug]: lockservice validTxn timeout treated as retry error, preventing lock release indefinitely #25279

Closed

Description

@matrix-meow

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

2add0b9

Other Environment Information

Actual Behavior

When validTxn sends a GetActiveTxn RPC to a remote CN and the RPC times out (context deadline exceeded), the error is classified as a "retry error" by isRetryError(). The isValidRemoteTxn function then conservatively assumes the transaction might still be valid and returns true, preventing lock release.

// pkg/lockservice/service.go (isValidRemoteTxn)
valid, err := h.validTxn(txn)
if err == nil {
    return valid
}
logValidTxnFailed(h.logger, txn, err)
if isRetryError(err) {
    return true  // ← timeout = "maybe alive" = don't release locks
}
return !h.canUnlockRemoteTxn(txn)

The validTxn callback uses a 10s timeout:

// pkg/lockservice/service_remote.go:109
ctx, cancel := context.WithTimeoutCause(
    context.Background(), defaultRPCTimeout,  // 10s
    moerr.CauseInitRemote2,
)
resp, err := s.remote.client.Send(ctx, req)

When the remote CN's lockservice is slow to respond (e.g., due to DN commit slowness — see #25274), the GetActiveTxn RPC consistently times out. Since context deadline exceeded is classified as a retry error, locks are never released, creating a permanent deadlock.

Observed incident

On CN p6bxw, GetActiveTxn RPCs timed out for 2+ hours because p6bxw's lockservice handlers were blocked (goroutine profile confirmed 4 goroutines stuck in handleGetActiveTxn → IterTxns → RLock blocked). Other CNs kept retrying validTxn every few minutes, always getting timeouts, never releasing locks.

07:02:23  "failed to valid txn"  error="context deadline exceeded\ninternal error: initRemote 2"
07:04:53  "failed to valid txn"  error="context deadline exceeded\ninternal error: initRemote 2"
07:09:53  "failed to valid txn"  error="context deadline exceeded\ninternal error: initRemote 2"
... (repeated every 2-5 min for 2+ hours, same txn)

Expected Behavior

  1. Consecutive timeout threshold: If validTxn fails with timeout N consecutive times (e.g., 3-5 times), the transaction should be treated as dead and locks released. A single timeout may be transient, but repeated timeouts indicate the remote CN is genuinely unreachable.

  2. Separate timeout classification: context deadline exceeded from a 10s RPC timeout should not be treated the same as a network partition or temporary retry. Consider distinguishing:

    • Transient error (network blip) → retry
    • Repeated timeout (remote CN overloaded) → treat as dead after threshold
    • Connection refused (CN crashed) → immediately dead
  3. Force unlock after max wait: If a waiter has been waiting for a lock held by a remote transaction for longer than a configurable threshold (e.g., 30 min), force release the lock regardless of validation result.

Steps to Reproduce

  1. Have CN-A hold locks on shared keys.
  2. Make CN-A's lockservice slow to respond to GetActiveTxn (e.g., by causing DN commit slowness on CN-A).
  3. CN-B tries to acquire the same locks, waits, triggers checkOrphan → validTxn.
  4. GetActiveTxn RPC to CN-A times out (10s).
  5. Observe: CN-B logs failed to valid txn with initRemote 2 error, locks never released.
  6. Waiters on CN-B accumulate indefinitely.

Additional information

Related code

Activity

  1. added
    kind/bug
    on Jun 30, 2026
  2. added
    severity/s0
    on Jun 30, 2026
  3. added theissue type on Jun 30, 2026
  4. added this to the MO-202607 milestone on Jun 30, 2026
  5. matrix-meow commented on Jun 30, 2026

    @matrix-meow
    ContributorAuthor

    Additional: test explicitly asserts buggy behavior

    pkg/lockservice/orphan_txn_test.go:807-846 explicitly asserts that when validTxn returns an error (e.g., ErrTxnNotFound), isValidRemoteTxn(...) returns true. This test solidifies the bug — any fix must also update this test.

  6. added and removed
    severity/s0
    on Jul 1, 2026
  7. aptend commented on Jul 1, 2026

    @aptend
    Contributor

    After checking the incident data, I think the root cause here is not that validTxn timed out and therefore the lock should be cleaned.

    The involved transactions were still active on their original CNs. Raw goroutine profiles show the transaction goroutines were blocked in the lock wait path, such as lockservice.(*waiter).wait, remoteLockTable.lock, and lockservice.(*client).Send. In other words, the lock wait cycle kept these transactions active.

    So a validTxn timeout is only a symptom/amplifier here. It means the remote CN did not answer the validation request in time, but it does not prove the transaction is orphaned or safe to unlock. If we release locks only because validTxn times out, correctness can be broken when the remote transaction is still alive and later continues or commits.

    The issue we should focus on is why this deadlock-like wait cycle was not detected and broken. In the raw profiles from the affected CN, the deadlock detector workers were idle in detector.doCheck, while the transaction goroutines were stuck in waiter.wait. This suggests the wait cycle was not effectively submitted to or processed by the detector, rather than validTxn being the real root cause.

  8. added
    severity/s0
    ai-heavy
    and removed on Jul 1, 2026
  9. 7 remaining items

  10. aptend commented on Jul 8, 2026

    @aptend
    Contributor

    We should still optimize the validTxn path, but this is a performance/robustness improvement rather than the root-cause fix for this incident.

    The current GetActiveTxn path can become expensive or timeout when the origin CN has many active transactions or when txn iteration is blocked. Optimizing it is useful, for example by reducing traversal cost or avoiding broad active-txn scans. This should be correctness-preserving as long as the semantics remain conservative: if the txn is confirmed active, do not release locks; if the result is timeout/unknown, also do not release locks; only release locks when the txn is confirmed inactive and the committing check says it is safe.

    However, this was not the root cause of the observed hung lock case. In the incident, the remote transaction could still be active while stuck in a lock wait / remote lock RPC cycle. Therefore, releasing locks based only on validTxn timeout would be unsafe. The root fix should remain in the lock wait/deadlock path: break owner-local wait cycles during remote lock handling, avoid deadlock-detector enqueue state pollution, and bound owner-side remote lock waits so the origin transaction can be rolled back instead of waiting indefinitely.

    follow-up pr #25509

  11. added
    ai-medium
    and removed
    ai-heavy
    on Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-mediumkind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions