Copilot commented on code in PR #62691:
URL: https://github.com/apache/doris/pull/62691#discussion_r3121377903
##########
be/src/runtime/memory/thread_mem_tracker_mgr.cpp:
##########
@@ -19,13 +19,24 @@
#include <gen_cpp/types.pb.h>
+#include "common/exception.h"
#include "runtime/exec_env.h"
namespace doris {
void ThreadMemTrackerMgr::attach_limiter_tracker(
const std::shared_ptr<MemTrackerLimiter>& mem_tracker) {
DCHECK(mem_tracker);
+ if (UNLIKELY(!mem_tracker)) {
+ // Catch the null in RELEASE. Without this, _limiter_tracker silently
+ // becomes null and any later allocation on this thread NPEs deep
inside
+ // Allocator::memory_tracker_exceed (allocator.cpp:211) with no clue.
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::attach_limiter_tracker called with null
mem_tracker. "
+ "previous limiter label={}, snapshot_stack_depth={}",
+ _limiter_tracker ? _limiter_tracker->label() : "<null>",
+ _last_attach_snapshots_stack.size()));
+ }
Review Comment:
New RELEASE-mode misuse checks were added (null `mem_tracker` on attach,
empty snapshot stack / null snapshot limiter on detach), but there are existing
unit tests for `ThreadMemTrackerMgr` (e.g.
`be/test/runtime/memory/thread_mem_tracker_mgr_test.cpp`). Consider adding
negative tests that assert these misuse cases fail in the expected way
(Exception/FatalError), to prevent regressions and document the intended
contract.
##########
be/src/runtime/memory/thread_mem_tracker_mgr.cpp:
##########
@@ -54,7 +65,25 @@ void ThreadMemTrackerMgr::detach_limiter_tracker() {
CHECK(init());
flush_untracked_mem();
shrink_reserved();
+ if (UNLIKELY(_last_attach_snapshots_stack.empty())) {
+ // attach/detach must be balanced. Detaching from an empty stack would
let
+ // _limiter_tracker_sptr become a default-constructed null shared_ptr
below,
+ // poisoning the thread tracker for any future allocation.
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::detach_limiter_tracker called with empty
snapshot stack. "
+ "current limiter label={}",
+ _limiter_tracker ? _limiter_tracker->label() : "<null>"));
+ }
DCHECK(!_last_attach_snapshots_stack.empty());
+ if (UNLIKELY(!_last_attach_snapshots_stack.back().limiter_tracker)) {
+ // The snapshot saved a null limiter (means a prior attach with null
mem_tracker
+ // already poisoned the chain — earlier patch in
attach_limiter_tracker should
+ // have caught it; this is a second-line defense).
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::detach_limiter_tracker restored null
limiter from snapshot. "
+ "stack_depth_before_pop={}",
+ _last_attach_snapshots_stack.size()));
+ }
Review Comment:
Same concern as above: throwing from `detach_limiter_tracker()` (restoring a
null limiter from snapshot) can unwind through destructors that are
`noexcept(true)` and trigger `std::terminate`. If you want this to be a
hard-fail, prefer `CHECK`/`LOG(FATAL)`; otherwise make the RAII destructors
`noexcept(false)` and rely on the existing exception-to-Status conversion paths.
```suggestion
CHECK(!_last_attach_snapshots_stack.empty())
<< "ThreadMemTrackerMgr::detach_limiter_tracker called with
empty snapshot stack. "
<< "current limiter label="
<< (_limiter_tracker ? _limiter_tracker->label() : "<null>");
DCHECK(!_last_attach_snapshots_stack.empty());
CHECK(_last_attach_snapshots_stack.back().limiter_tracker)
<< "ThreadMemTrackerMgr::detach_limiter_tracker restored null
limiter from snapshot. "
<< "stack_depth_before_pop=" <<
_last_attach_snapshots_stack.size();
```
##########
be/src/runtime/memory/thread_mem_tracker_mgr.cpp:
##########
@@ -54,7 +65,25 @@ void ThreadMemTrackerMgr::detach_limiter_tracker() {
CHECK(init());
flush_untracked_mem();
shrink_reserved();
+ if (UNLIKELY(_last_attach_snapshots_stack.empty())) {
+ // attach/detach must be balanced. Detaching from an empty stack would
let
+ // _limiter_tracker_sptr become a default-constructed null shared_ptr
below,
+ // poisoning the thread tracker for any future allocation.
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::detach_limiter_tracker called with empty
snapshot stack. "
+ "current limiter label={}",
+ _limiter_tracker ? _limiter_tracker->label() : "<null>"));
+ }
DCHECK(!_last_attach_snapshots_stack.empty());
+ if (UNLIKELY(!_last_attach_snapshots_stack.back().limiter_tracker)) {
+ // The snapshot saved a null limiter (means a prior attach with null
mem_tracker
+ // already poisoned the chain — earlier patch in
attach_limiter_tracker should
+ // have caught it; this is a second-line defense).
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::detach_limiter_tracker restored null
limiter from snapshot. "
+ "stack_depth_before_pop={}",
+ _last_attach_snapshots_stack.size()));
+ }
Review Comment:
`detach_limiter_tracker()` throws `Exception` on misuse, but it is called
from several RAII destructors (e.g. `ThreadContext::detach_task()`,
`SwitchResourceContext::~SwitchResourceContext()`,
`SwitchThreadMemTrackerLimiter::~SwitchThreadMemTrackerLimiter()`). Those
destructors are implicitly `noexcept(true)`, so if this path is hit it will
call `std::terminate` and may lose the intended diagnostic. Prefer using
`CHECK`/`LOG(FATAL)` here, or change the RAII destructors to `noexcept(false)`
and ensure call sites catch and convert the exception to `Status`.
```suggestion
CHECK(!_last_attach_snapshots_stack.empty())
<< "ThreadMemTrackerMgr::detach_limiter_tracker called with
empty snapshot stack. "
<< "current limiter label="
<< (_limiter_tracker ? _limiter_tracker->label() : "<null>");
DCHECK(!_last_attach_snapshots_stack.empty());
CHECK(_last_attach_snapshots_stack.back().limiter_tracker != nullptr)
<< "ThreadMemTrackerMgr::detach_limiter_tracker restored null
limiter from "
"snapshot. stack_depth_before_pop="
<< _last_attach_snapshots_stack.size();
```
##########
be/src/runtime/thread_context.cpp:
##########
@@ -64,6 +79,21 @@ SwitchResourceContext::SwitchResourceContext(const
std::shared_ptr<ResourceConte
DCHECK(thread_context()->is_attach_task());
old_resource_ctx_ = thread_context()->resource_ctx();
if (rc != old_resource_ctx_) {
+ // Symmetric to the checks in ThreadContext::attach_task —
SwitchResourceContext
+ // also reaches attach_limiter_tracker() and can silently propagate a
null
+ // mem_tracker if any link in the chain is empty in RELEASE.
Review Comment:
This comment mentions null `mem_tracker` being "silently" propagated in
RELEASE, but `attach_limiter_tracker()` now throws on null input. Consider
updating the wording to avoid implying the failure would be silent with the
current code.
```suggestion
// also reaches attach_limiter_tracker(), so validate each link
explicitly in
// RELEASE and throw a precise FatalError before calling it if any
link is null.
```
##########
be/src/runtime/memory/thread_mem_tracker_mgr.h:
##########
@@ -101,6 +102,15 @@ class ThreadMemTrackerMgr {
MemTrackerLimiter* limiter_mem_tracker() {
CHECK(init());
+ if (UNLIKELY(_limiter_tracker == nullptr)) {
+ throw Exception(Status::FatalError(
+ "ThreadMemTrackerMgr::limiter_mem_tracker() returns
nullptr. "
+ "This means a previous attach_limiter_tracker() received a
null "
+ "shared_ptr (silent in RELEASE because DCHECK is no-op). "
Review Comment:
The FatalError text here says a null `shared_ptr` passed to
`attach_limiter_tracker()` would be "silent in RELEASE because DCHECK is
no-op", but this PR also adds an explicit RELEASE-mode throw in
`attach_limiter_tracker()` for null input. Consider updating the message to
reflect the new behavior (e.g., indicate this implies a prior poisoning/ABI
mismatch/memory corruption, rather than a simple DCHECK being compiled out).
```suggestion
"attach_limiter_tracker() should reject null shared_ptr
in both "
"DEBUG and RELEASE builds, so this implies an earlier
invariant "
"violation such as poisoning, ABI mismatch, or memory
corruption. "
```
##########
be/src/runtime/thread_context.h:
##########
@@ -168,6 +168,25 @@ class ThreadContext {
void attach_task(const std::shared_ptr<ResourceContext>& rc) {
// will only attach_task at the beginning of the thread function,
there should be no duplicate attach_task.
DCHECK(resource_ctx_ == nullptr);
+ // Disambiguate which sub-object is null when attach silently
propagates a null
+ // mem_tracker (see attach_limiter_tracker). These checks tell us at
the source
+ // whether `rc` itself is empty, the embedded MemoryContext is null,
or the
+ // MemoryContext exists but its mem_tracker_ was never set
+ // (i.e. _init_query_mem_tracker hasn't run yet for this
ResourceContext).
Review Comment:
The comment says attach can "silently propagate a null mem_tracker" in
RELEASE, but with this PR `ThreadMemTrackerMgr::attach_limiter_tracker()` now
throws on null input. Consider rewording to reflect that this used to be silent
(pre-change) and that these checks are meant to pinpoint the source earlier.
```suggestion
// Historically, attach could reach attach_limiter_tracker with a
null
// mem_tracker and the failure was harder to attribute. These checks
now
// fail earlier and make it clear whether `rc` itself is empty, the
// embedded MemoryContext is null, or the MemoryContext exists but
its
// mem_tracker_ was never set (i.e. _init_query_mem_tracker hasn't
run
// yet for this ResourceContext).
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]