Skip to content

CN Transaction Rollback: resource leak, delayed waiter notification, and abortC signal loss #25195

Closed as not planned

Description

@XuPeng-SH

Summary

Unhappy-path audit of the CN transaction lifecycle (pkg/txn/) revealed three issues in the rollback and error-signalling paths. Two are in the CN-side Rollback handler (service_cn_handler.go), and one is in the txn client's abort-signal channel (client.go).


Issue 1 — Rollback handler lacks synchronous cleanup, depends entirely on async task

File: pkg/txn/service/service_cn_handler.go
Lines: 353–392 (Rollback handler)

Problem

The Rollback handler never calls removeTxn or releaseTxnContext synchronously. It delegates all cleanup to the async RollbackTNShard task:

Rollback handler
  └→ startAsyncRollbackTask(newTxn)      ← if s.stopper.RunTask() fails, nothing happens
      └→ parallelSendWithRetry(RollbackTNShard)
          └→ RollbackTNShard handler      ← removeTxn + releaseTxnContext only here

If s.stopper.RunTask() returns an error (stopper is closing, queue is full, etc.), the txnCtx remains in sync.Map forever. Compare this to the Commit handler (lines 237–244), which has an immediate defer block that calls removeTxn and releaseTxnContext regardless of what happens downstream.

Impact

  • Resource leak: both the sync.Map entry and the pooled txnContext object leak on every rollback where the async task fails to start.
  • gcZombieTxn amplification: the leaked entry remains in the map, causing gcZombieTxn to retry Rollback on every tick.

Suggested fix

Add a defer block analogous to the Commit handler:

defer func() {
    s.removeTxn(txnID)
    s.releaseTxnContext(txnCtx)
}()

This makes Rollback defensively clean up regardless of whether the async task starts.


Issue 2 — Rollback handler does not notify waiters via changeStatusLocked

File: pkg/txn/service/service_cn_handler.go
Lines: 382–392

Problem

The Rollback handler modifies the transaction status on a value copy of txnMeta, not the stored state:

newTxn := txnCtx.getTxnLocked()            // value copy
response.Txn = &newTxn
response.Txn.Status = txn.TxnStatus_Aborted // mutates copy only

It never calls txnCtx.changeStatusLocked(txn.TxnStatus_Aborted). The stored c.mu.txn.Status remains Active until the async RollbackTNShard handler executes changeStatusLocked. Any goroutine waiting on waiter.wait() (via waitRecoveryCompleted or similar) stays blocked for an extra RPC round-trip.

Commit does not have this problem because the CommitTNShard handlers are invoked synchronously within the Commit handler and call changeStatusLocked themselves.

Impact

  • Delayed waiter unblocking: waiters observe Aborted status only after the async RPC completes, adding latency proportional to RollbackTNShard RTT.
  • If the async task never starts (Issue 1), waiters are never notified.

Suggested fix

Call changeStatusLocked before dispatching the async task:

txnCtx.changeStatusLocked(txn.TxnStatus_Aborted)
s.startAsyncRollbackTask(newTxn)

Issue 3 — abortC signal channel can drop signals under rapid failure

File: pkg/txn/client/client.go
Line: 838

Problem

markAllActiveTxnAborted uses a buffered channel with capacity 1 and a select/default send:

select {
case c.abortC <- now:
default:  // signal silently dropped if channel already has one pending
}

When two or more ErrCannotCommitOnInvalidCN errors arrive before the handler goroutine (handleMarkActiveTxnAborted) drains the first signal, subsequent signals are silently discarded.

Impact

  • Low: the handler marks all txns created before now, so even if a signal is dropped the previously-recorded timestamp will cover the missed event. However, if the handler manages to process signal-1 and a third error arrives before signal-2 is sent, the gap between signal-1's timestamp and signal-3's timestamp could leave some txns unmarked.
  • Requires a rare double- or triple-fault scenario to manifest.

Suggested fix

Options:

  1. Use a sync.WaitGroup or atomic.Bool + closing a channel to ensure one processing cycle per signal.
  2. Use an unbuffered channel + spawn a dedicated drain goroutine that batches timestamps.
  3. If the current behavior is deemed acceptable, add a comment documenting the design decision.

Related code paths

File Lines Role
pkg/txn/service/service_cn_handler.go 353–392 CN Rollback handler (Issues 1, 2)
pkg/txn/service/service_cn_handler.go 220–255 CN Commit handler (reference for symmetric cleanup)
pkg/txn/service/service_cn_handler.go 131–170 gcZombieTxn (amplifies Issue 1)
pkg/txn/service/service_dn_handler.go 161–220 RollbackTNShard handler (async cleanup)
pkg/txn/client/client.go 830–845 markAllActiveTxnAborted / handleMarkActiveTxnAborted (Issue 3)

Severity

Issue Severity Rationale
1 — missing sync cleanup High Leaks sync.Map entries + pooled objects on rollback when stopper rejects the task
2 — missing changeStatusLocked Medium Delays waiter notification by one async RPC round-trip
3 — abortC signal drop Low Requires multi-fault scenario; effectively self-healing via timestamp semantics

Activity

  1. added
    kind/bug
    severity/s0
    on Jun 29, 2026
  2. added theissue type on Jun 29, 2026
  3. added this to the MO-202607 milestone on Jun 29, 2026
  4. added
    ai-medium
    severity/s0
    and removed
    severity/s0
    on Jul 2, 2026
  5. aptend commented on Jul 9, 2026

    @aptend
    Contributor

    After re-checking this issue against the current single-TN deployment model, I do not think this describes an active s0/high-priority rollback lifecycle bug.

    Key points:

    1. Rollback cleanup is intentionally performed in RollbackTNShard, after storage.Rollback has run. The suggested fix of adding removeTxn / releaseTxnContext directly in the CN Rollback handler is unsafe: it can release the txnCtx before the local rollback handler gets a chance to execute storage.Rollback, causing the storage-side rollback to be skipped.

    2. The current production/deployment assumption is single TN. Under this model there is no multi-TN fan-out / 2PC partial-cleanup scenario. The rollback task loops back to the same TN service, so the claimed distributed rollback amplification does not apply to the current runtime shape.

    3. The only valid edge here is startAsyncRollbackTask failing to start. However, Stopper.RunTask mainly fails when the service is no longer running / is stopping; it is not a normal queue-full or backpressure path. That makes this a shutdown/unhappy-path hardening item at most, not a current high-severity production bug.

    4. The waiter argument is also overstated. Normal read waiters are registered on Committed and abort wakeup relies on releaseTxnContext / notifier close after the rollback path completes. Simply calling changeStatusLocked(Aborted) in the CN Rollback handler is not a complete or obviously correct fix.

    5. The abortC signal coalescing concern is independent from the rollback lifecycle. It may deserve a small comment or hardening change, but it should not be bundled into this issue as evidence for an s0 rollback bug.

    So my recommendation is to close or defer this issue as written. If we want follow-up work, it should be a much narrower low-priority hardening task: make async rollback task startup failure explicit and handle it without releasing txnCtx before storage.Rollback completes.

  6. removed
    severity/s0
    on Jul 9, 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/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions