Properly integrate BatchedEnrichmentStage memory tracking with the operation memory tracker across getMore

    • Type: Task
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Integration
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Background

      SERVER-130899 makes $_internalSearchIdLookup inherit from BatchedEnrichmentStage, which buffers events across getNext() calls and tracks that buffered memory with a SimpleMemoryUsageTracker whose base points at the operation's OperationMemoryUsageTracker (OMT).

      Because the stage's buffers outlive a single getMore, the base pointer must survive opCtx swaps. The OMT, however, lives on the OperationContext and is only carried across getMore for ClientCursorPin-managed cursors. The $search results pipeline (and in particular the multi-cursor $$SEARCH_META path) is reattached outside that transfer, so the original OMT is not carried across. Leaving the base pointed at the old OMT caused a use-after-free / SIGSEGV when the old opCtx was destroyed at the end of a getMore, which auto-reverted the original change.

      The reland works around this with a stopgap: BatchedEnrichmentStage drops its tracker base on detachFromOperationContext() and re-binds it to the current opCtx's OMT on reattachToOperationContext(), via two new helpers:

      • SimpleMemoryUsageTracker::resetBase()
      • OperationMemoryUsageTracker::rebindToOperation()

      This fixes the crash and keeps correct OMT roll-up on the normal cursor path, but on the $$SEARCH_META path it mints a fresh OMT each getMore, so memory is not accounted across the whole query lifetime there.

      Goal

      Properly integrate the BatchedEnrichmentStage's memory tracking with the operation memory tracker so that buffered memory is accounted across the entire query lifetime, including the multi-cursor $$SEARCH_META path (i.e. the OMT is carried across the results-pipeline reattach rather than re-created per getMore).

      Scope / acceptance criteria

      • Buffered memory in BatchedEnrichmentStage rolls up to a single, query-lifetime operation memory tracker on all $search/$vectorSearch code paths, including $$SEARCH_META.
      • Remove the stopgap helpers introduced by SERVER-130899:
        • SimpleMemoryUsageTracker::resetBase()
        • OperationMemoryUsageTracker::rebindToOperation() and the BatchedEnrichmentStage detach/reattach overrides that use them.
      • Keep (or extend) the regression test BatchedEnrichmentStageTest.MemoryTrackerRebindsToNewOperationAcrossGetMoreOpCtxSwap so the original dangling-base crash stays covered.

            Assignee:
            Unassigned
            Reporter:
            Chunbin Lin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: