Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/common/common/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ struct ThreadIds {
return main_thread_id_ == id || test_thread_id_ == id;
}

bool isMainThreadActive() const {
absl::MutexLock lock(&mutex_);
return main_thread_use_count_ != 0;
}

// Returns a singleton instance of this. The instance is never freed.
static ThreadIds& get() { MUTABLE_CONSTRUCT_ON_FIRST_USE(ThreadIds); }

Expand Down Expand Up @@ -101,6 +106,8 @@ struct ThreadIds {

bool MainThread::isMainOrTestThread() { return ThreadIds::get().inMainOrTestThread(); }

bool MainThread::isMainThreadActive() { return ThreadIds::get().isMainThreadActive(); }

TestThread::TestThread() { ThreadIds::get().registerTestThread(); }

TestThread::~TestThread() { ThreadIds::get().releaseTestThread(); }
Expand Down
7 changes: 6 additions & 1 deletion source/common/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,14 @@ class MainThread {
~MainThread();

/**
* Returns whether the current thread is the main thread or test thread.
* @return whether the current thread is the main thread or test thread.
*/
static bool isMainOrTestThread();

/**
* @return whether a MainThread has been instantiated.
*/
static bool isMainThreadActive();
};

// To improve exception safety in data plane, we plan to forbid the use of raw try in the core code
Expand Down
3 changes: 3 additions & 0 deletions test/test_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ void TestListener::OnTestEnd(const ::testing::TestInfo& test_info) {
"]: Active singletons exist. Something is leaking. Consider "
"commenting out this assert and letting the heap checker run:\n",
active_singletons));
RELEASE_ASSERT(!Thread::MainThread::isMainThreadActive(),
absl::StrCat("MainThreadLeak: [", test_info.test_suite_name(), ".",
test_info.name(), "] test exited before main thread shut down"));
}

} // namespace Envoy