Skip to content

CN transaction: permanent workspace/offheap memory leak in FinalizeCommitWithUnknownResult #25198

Closed

Description

@XuPeng-SH

Summary

FinalizeCommitWithUnknownResult never calls delTransaction(), causing permanent workspace, offheap batch vectors, quota, and cached metadata to leak indefinitely when a commit encounters ErrTxnUnknown. The leak persists until CN process restart.

Severity: High

Root Cause

There is a clear asymmetry between the three finalization paths:

Path Calls delTransaction()? Cleans workspace/batch/quota?
FinalizeCommit() ✅ Yes (line 2148) ✅
Rollback() ✅ Yes (line 2236) ✅
FinalizeCommitWithUnknownResult() ❌ No (line 2156–2158) ❌

Code comparison

// disttae/txn.go - FinalizeCommit (line 2143-2148):
func (txn *Transaction) FinalizeCommit(context.Context) {
    if txn.isCCPRTxn... { ... }
    txn.delTransaction()  // ✅ cleans up
}

// disttae/txn.go - FinalizeCommitWithUnknownResult (line 2150-2158):
func (txn *Transaction) FinalizeCommitWithUnknownResult(context.Context) {
    if txn.isCCPRTxn... { ... }
    // Comment says: "Keep workspace/object-stat metadata intact"
    // txn.delTransaction() is NEVER called ❌
}

What delTransaction() actually releases

Resource Type Cleanup
writes[i].bat batch + offheap C-malloc'd vectors bat.Clean(mp) → vec.Free(mp) → mpool.Free → C.free
tableCache / tableOps / databaseOps maps/slices set to nil (GC)
compactWorker goroutine pool Release()
extraWriteWorkspaceThreshold engine quota engine.ReleaseQuota()
deletedBlocks slice set to nil

None of these are freed in the ErrTxnUnknown path.

Full call chain

Commit() → doWrite() → defer {
    if ErrTxnUnknown {
        FinalizeCommitWithUnknownResult()  // never cleans up
        return                              // Rollback is NOT called either
    }
}
→ closeLocked() → ClosedEvent → closeTxn()
  // Only removes txnOperator from txnClient.activeTxns
  // The disttae Transaction object and all its resources persist

Trigger conditions

  • Commit RPC times out (network partition, TN restart)
  • TN returns ErrTxnUnknown (uncertain commit outcome)
  • CN calls FinalizeCommitWithUnknownResult intending to preserve workspace for potential S3 GC

Impact

  • Offheap (C heap) memory leaks: batch vectors allocated via mpool with offHeap=true → C.malloc memory never freed until process exit
  • Quota leak: extraWriteWorkspaceThreshold quota permanently consumed
  • Silent accumulation: no observability or timeout-driven cleanup
  • Risk scenario: long-running CN process with intermittent network issues → hundreds of MB to GB of leaked offheap memory over days/weeks

Suggested Fix

Add either:

  1. A timeout-driven cleanup: If FinalizeCommitWithUnknownResult has been unresolved for N minutes, forcibly call delTransaction() to release resources.

  2. An explicit cleanup trigger: Allow the caller to signal that the unknown result has been resolved (either committed or aborted externally), at which point delTransaction() is called.

  3. Defensive partial cleanup: At minimum, call delTransaction() on CN shutdown or after confirming the commit cannot be recovered.

Related

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-heavy
    and removed
    severity/s0
    on Jul 2, 2026
  5. added a commit that references this issue on Jul 10, 2026
    5b60307
  6. removed
    ai-heavy
    on Jul 13, 2026
  7. aptend commented on Jul 13, 2026

    @aptend
    Contributor

    fixed

  8. added 3 commits that reference this issue on Sep 17, 2026
    1e17044
    d52bc03
    37cdb81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions