From 9570d81a7d92100b4e5cfa76c1f16f71bcde1850 Mon Sep 17 00:00:00 2001 From: Joshua Marantz Date: Mon, 4 Oct 2021 17:06:31 -0400 Subject: [PATCH 1/3] assert threads are quiescent between tests Signed-off-by: Joshua Marantz --- source/common/common/thread.cc | 7 +++++++ source/common/common/thread.h | 2 ++ test/test_listener.cc | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 392a812595820..1df286aa70973 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -31,6 +31,11 @@ struct ThreadIds { return main_thread_id_ == id || test_thread_id_ == id; } + bool hasMainThread() 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); } @@ -101,6 +106,8 @@ struct ThreadIds { bool MainThread::isMainThread() { return ThreadIds::get().inMainOrTestThread(); } +bool MainThread::hasMainThread() { return ThreadIds::get().hasMainThread(); } + TestThread::TestThread() { ThreadIds::get().registerTestThread(); } TestThread::~TestThread() { ThreadIds::get().releaseTestThread(); } diff --git a/source/common/common/thread.h b/source/common/common/thread.h index 896ff79bb11f2..1a5efb8bb1ef2 100644 --- a/source/common/common/thread.h +++ b/source/common/common/thread.h @@ -202,6 +202,8 @@ class MainThread { * TODO(jmarantz): rename to isMainOrTestThread(). */ static bool isMainThread(); + + static bool hasMainThread(); }; // To improve exception safety in data plane, we plan to forbid the use of raw try in the core code diff --git a/test/test_listener.cc b/test/test_listener.cc index 12dba6668f813..3bf3f77c26bc4 100644 --- a/test/test_listener.cc +++ b/test/test_listener.cc @@ -14,6 +14,10 @@ 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::hasMainThread(), + absl::StrCat("MainThreadLeak: [", test_info.test_suite_name(), ".", + test_info.name(), + "] test exited before main thread shut down")); } } // namespace Envoy From 2471b9ff6137c05964099dc3e364787219f24de7 Mon Sep 17 00:00:00 2001 From: Joshua Marantz Date: Wed, 6 Oct 2021 23:54:11 -0400 Subject: [PATCH 2/3] format Signed-off-by: Joshua Marantz --- source/common/common/thread.h | 5 ++++- test/test_listener.cc | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/source/common/common/thread.h b/source/common/common/thread.h index 908abfaed7304..cb22f41ef37dd 100644 --- a/source/common/common/thread.h +++ b/source/common/common/thread.h @@ -197,10 +197,13 @@ 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 hasMainThread(); }; diff --git a/test/test_listener.cc b/test/test_listener.cc index 3bf3f77c26bc4..eb44d7fa09bdb 100644 --- a/test/test_listener.cc +++ b/test/test_listener.cc @@ -16,8 +16,7 @@ void TestListener::OnTestEnd(const ::testing::TestInfo& test_info) { active_singletons)); RELEASE_ASSERT(!Thread::MainThread::hasMainThread(), absl::StrCat("MainThreadLeak: [", test_info.test_suite_name(), ".", - test_info.name(), - "] test exited before main thread shut down")); + test_info.name(), "] test exited before main thread shut down")); } } // namespace Envoy From c23a76ad882ad2215cdd6f24a3bb9f273ce8b3db Mon Sep 17 00:00:00 2001 From: Joshua Marantz Date: Thu, 7 Oct 2021 23:05:02 -0400 Subject: [PATCH 3/3] rename the new method name. Signed-off-by: Joshua Marantz --- source/common/common/thread.cc | 4 ++-- source/common/common/thread.h | 2 +- test/test_listener.cc | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 4a190870677b4..63cac85f6ca0e 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -31,7 +31,7 @@ struct ThreadIds { return main_thread_id_ == id || test_thread_id_ == id; } - bool hasMainThread() const { + bool isMainThreadActive() const { absl::MutexLock lock(&mutex_); return main_thread_use_count_ != 0; } @@ -106,7 +106,7 @@ struct ThreadIds { bool MainThread::isMainOrTestThread() { return ThreadIds::get().inMainOrTestThread(); } -bool MainThread::hasMainThread() { return ThreadIds::get().hasMainThread(); } +bool MainThread::isMainThreadActive() { return ThreadIds::get().isMainThreadActive(); } TestThread::TestThread() { ThreadIds::get().registerTestThread(); } diff --git a/source/common/common/thread.h b/source/common/common/thread.h index cb22f41ef37dd..f1f415cf04d50 100644 --- a/source/common/common/thread.h +++ b/source/common/common/thread.h @@ -204,7 +204,7 @@ class MainThread { /** * @return whether a MainThread has been instantiated. */ - static bool hasMainThread(); + static bool isMainThreadActive(); }; // To improve exception safety in data plane, we plan to forbid the use of raw try in the core code diff --git a/test/test_listener.cc b/test/test_listener.cc index eb44d7fa09bdb..89c8c9251e5a2 100644 --- a/test/test_listener.cc +++ b/test/test_listener.cc @@ -14,7 +14,7 @@ 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::hasMainThread(), + RELEASE_ASSERT(!Thread::MainThread::isMainThreadActive(), absl::StrCat("MainThreadLeak: [", test_info.test_suite_name(), ".", test_info.name(), "] test exited before main thread shut down")); }