From c609805dac86b60c1980a515b4bd1049105b799d Mon Sep 17 00:00:00 2001 From: Peter Andreas Entschev Date: Fri, 18 Oct 2024 03:14:19 -0700 Subject: [PATCH] Check if notifier thread is running before warning --- cpp/include/ucxx/notifier.h | 7 +++++++ cpp/include/ucxx/worker_progress_thread.h | 5 +++++ cpp/python/include/ucxx/python/notifier.h | 7 +++++++ cpp/python/src/notifier.cpp | 6 ++++++ cpp/src/worker.cpp | 2 +- 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cpp/include/ucxx/notifier.h b/cpp/include/ucxx/notifier.h index b1a29a25..554271be 100644 --- a/cpp/include/ucxx/notifier.h +++ b/cpp/include/ucxx/notifier.h @@ -97,6 +97,13 @@ class Notifier { * it should stop and exit. */ virtual void stopRequestNotifierThread() = 0; + + /** + * @brief Returns whether the thread is running. + * + * @returns Whether the thread is running. + */ + [[nodiscard]] virtual bool isRunning() const = 0; }; } // namespace ucxx diff --git a/cpp/include/ucxx/worker_progress_thread.h b/cpp/include/ucxx/worker_progress_thread.h index 70700cb0..19ce1012 100644 --- a/cpp/include/ucxx/worker_progress_thread.h +++ b/cpp/include/ucxx/worker_progress_thread.h @@ -162,6 +162,11 @@ class WorkerProgressThread { */ [[nodiscard]] std::thread::id getId() const; + /** + * @brief Returns whether the thread is running. + * + * @returns Whether the thread is running. + */ [[nodiscard]] bool isRunning() const; void stop(); diff --git a/cpp/python/include/ucxx/python/notifier.h b/cpp/python/include/ucxx/python/notifier.h index 4f2d577c..2e72ebc8 100644 --- a/cpp/python/include/ucxx/python/notifier.h +++ b/cpp/python/include/ucxx/python/notifier.h @@ -139,6 +139,13 @@ class Notifier : public ::ucxx::Notifier { * that it should stop and exit. */ void stopRequestNotifierThread() override; + + /** + * @brief Returns whether the thread is running. + * + * @returns Whether the thread is running. + */ + [[nodiscard]] bool isRunning() const override; }; } // namespace python diff --git a/cpp/python/src/notifier.cpp b/cpp/python/src/notifier.cpp index 0fd2d200..16a97b8c 100644 --- a/cpp/python/src/notifier.cpp +++ b/cpp/python/src/notifier.cpp @@ -122,6 +122,12 @@ void Notifier::stopRequestNotifierThread() _notifierThreadConditionVariable.notify_all(); } +bool Notifier::isRunning() const +{ + return _notifierThreadFutureStatusReady || + _notifierThreadFutureStatusFinished == RequestNotifierThreadState::Running; +} + } // namespace python } // namespace ucxx diff --git a/cpp/src/worker.cpp b/cpp/src/worker.cpp index 122ed5a3..ff0ac930 100644 --- a/cpp/src/worker.cpp +++ b/cpp/src/worker.cpp @@ -170,7 +170,7 @@ Worker::~Worker() "unintended effects, such as destructor being called from that thread."); stopProgressThreadNoWarn(); } - if (_notifier) { + if (_notifier && _notifier->isRunning()) { ucxx_warn( "The notifier thread should be explicitly stopped with `stopNotifierThread()` to prevent " "unintended effects, such as destructor being called from that thread.");