Repository navigation
Description
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.Mapentry and the pooledtxnContextobject leak on every rollback where the async task fails to start. - gcZombieTxn amplification: the leaked entry remains in the map, causing
gcZombieTxnto retryRollbackon 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 onlyIt 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
Abortedstatus only after the async RPC completes, adding latency proportional toRollbackTNShardRTT. - 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:
- Use a
sync.WaitGrouporatomic.Bool+ closing a channel to ensure one processing cycle per signal. - Use an unbuffered channel + spawn a dedicated drain goroutine that batches timestamps.
- 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
aptend commentedon Jul 9, 2026
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:
Rollback cleanup is intentionally performed in
RollbackTNShard, afterstorage.Rollbackhas run. The suggested fix of addingremoveTxn/releaseTxnContextdirectly in the CNRollbackhandler is unsafe: it can release thetxnCtxbefore the local rollback handler gets a chance to executestorage.Rollback, causing the storage-side rollback to be skipped.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.
The only valid edge here is
startAsyncRollbackTaskfailing to start. However,Stopper.RunTaskmainly 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.The waiter argument is also overstated. Normal read waiters are registered on
Committedand abort wakeup relies onreleaseTxnContext/ notifier close after the rollback path completes. Simply callingchangeStatusLocked(Aborted)in the CNRollbackhandler is not a complete or obviously correct fix.The
abortCsignal 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
txnCtxbeforestorage.Rollbackcompletes.