This is an automated email from the ASF dual-hosted git repository. fgerlits pushed a commit to branch MINIFICPP-2877_Reorganize-BackTrace in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
commit 1a63c38f94e3acb76cdde52898685d24f83d8260 Author: Ferenc Gerlits <[email protected]> AuthorDate: Mon Nov 16 15:04:26 2026 +0100 Fix a bug in ThreadedRepository::getTraces() The logic was reversed: we can only get a stack trace when the thread is joinable, not when is it non-joinable. But if the thread is joinable and the run() function has already finished, then we can't get a stack trace, either. --- core-framework/include/core/ThreadedRepository.h | 28 ++++-- libminifi/test/unit/ThreadedRepositoryTests.cpp | 123 +++++++++++++++++++++++ 2 files changed, 140 insertions(+), 11 deletions(-) diff --git a/core-framework/include/core/ThreadedRepository.h b/core-framework/include/core/ThreadedRepository.h index ea1565484..fae6e32a1 100644 --- a/core-framework/include/core/ThreadedRepository.h +++ b/core-framework/include/core/ThreadedRepository.h @@ -30,8 +30,7 @@ class ThreadedRepositoryImpl : public core::RepositoryImpl, public virtual core: // Starts repository monitor thread bool start() override { // if Stopped, turn to Starting, otherwise return - RunningState expected{RunningState::Stopped}; - if (!running_state_.compare_exchange_strong(expected, RunningState::Starting)) { + if (!changeRunningState(RunningState::Stopped, RunningState::Starting)) { return false; } if (purge_period_ <= std::chrono::milliseconds(0)) { @@ -41,7 +40,10 @@ class ThreadedRepositoryImpl : public core::RepositoryImpl, public virtual core: // must set Running state before calling run(), as run() might check state running_state_.store(RunningState::Running); - getThread() = std::thread(&ThreadedRepositoryImpl::run, this); + getThread() = std::thread([this] { + run(); + changeRunningState(RunningState::Running, RunningState::RunFinished); + }); logger_->log_debug("{} ThreadedRepository monitor thread start", name_); return true; @@ -49,9 +51,9 @@ class ThreadedRepositoryImpl : public core::RepositoryImpl, public virtual core: // Stops repository monitor thread bool stop() override { - // if RUNNING, turn to STOPPING, otherwise return - RunningState expected{RunningState::Running}; - if (!running_state_.compare_exchange_strong(expected, RunningState::Stopping)) { + // if Running or RunFinished, turn to Stopping, otherwise return + if (!(changeRunningState(RunningState::Running, RunningState::Stopping) + || changeRunningState(RunningState::RunFinished, RunningState::Stopping))) { return false; } if (getThread().joinable()) { @@ -63,14 +65,13 @@ class ThreadedRepositoryImpl : public core::RepositoryImpl, public virtual core: } bool isRunning() const override { - return running_state_.load() == RunningState::Running; + const auto running_state = running_state_.load(); + return running_state == RunningState::Running || running_state == RunningState::RunFinished; } utils::trace_resolver::BackTrace getTraces() override { - // If the thread is joinable then it is finished, we cannot interrupt and collect its stack traces - // It can occur with an empty run() implementation like the NoOpThreadedRepository where the stack trace is unavailable - if (getThread().joinable()) { - return {}; + if (!getThread().joinable() || running_state_.load() != RunningState::Running) { + return {.name = getName(), .trace = {}}; } return utils::trace_resolver::getBackTrace(getName(), getThread().native_handle()); } @@ -89,10 +90,15 @@ class ThreadedRepositoryImpl : public core::RepositoryImpl, public virtual core: enum class RunningState : uint8_t { Starting, Running, + RunFinished, Stopping, Stopped }; + bool changeRunningState(RunningState old_state, RunningState new_state) { + return running_state_.compare_exchange_strong(old_state, new_state); + } + std::atomic<RunningState> running_state_{RunningState::Stopped}; std::shared_ptr<logging::Logger> logger_ {logging::LoggerFactory<ThreadedRepository>::getLogger()}; }; diff --git a/libminifi/test/unit/ThreadedRepositoryTests.cpp b/libminifi/test/unit/ThreadedRepositoryTests.cpp new file mode 100644 index 000000000..05b1899aa --- /dev/null +++ b/libminifi/test/unit/ThreadedRepositoryTests.cpp @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements +#include <algorithm> +#include <atomic> +#include <string> +#include <thread> + +#include "core/ThreadedRepository.h" +#include "core/repository/NoOpThreadedRepository.h" +#include "unit/TestBase.h" +#include "unit/Catch.h" +#include "unit/TestUtils.h" +#include "utils/BackTrace.h" + +using namespace std::literals::chrono_literals; + +namespace org::apache::nifi::minifi::core::test { + +using BackTrace = utils::trace_resolver::BackTrace; + +class BlockingThreadedRepository : public core::ThreadedRepositoryImpl { + public: + BlockingThreadedRepository() : ThreadedRepositoryImpl("BlockingRepo") {} + + BlockingThreadedRepository(BlockingThreadedRepository&&) = delete; + BlockingThreadedRepository(const BlockingThreadedRepository&) = delete; + BlockingThreadedRepository& operator=(BlockingThreadedRepository&&) = delete; + BlockingThreadedRepository& operator=(const BlockingThreadedRepository&) = delete; + + ~BlockingThreadedRepository() override { + release(); + stop(); + } + + uint64_t getRepositorySize() const override { + return 0; + } + + uint64_t getRepositoryEntryCount() const override { + return 0; + } + + void waitUntilRunIsInProgress() { + run_in_progress_.wait(false); + } + + void release() { + release_.test_and_set(); + release_.notify_all(); + } + + private: + void run() override { + run_in_progress_.test_and_set(); + run_in_progress_.notify_all(); + release_.wait(false); + } + + std::thread& getThread() override { + return thread_; + } + + std::atomic_flag run_in_progress_; + std::atomic_flag release_; + std::thread thread_; +}; + +TEST_CASE("ThreadedRepository returns the stack trace of its thread while it is running", "[ThreadedRepository][getTraces]") { + LogTestController::getInstance().setTrace<BackTrace>(); + + BlockingThreadedRepository repository; + + SECTION("before start") { + const BackTrace back_trace = repository.getTraces(); + CHECK(back_trace.name == "BlockingRepo"); + CHECK(back_trace.trace.empty()); + } + + SECTION("while running") { + REQUIRE(repository.start()); + repository.waitUntilRunIsInProgress(); + + const BackTrace back_trace = repository.getTraces(); + CHECK(back_trace.name == "BlockingRepo"); +#ifdef HAS_EXECINFO + CHECK_FALSE(back_trace.trace.empty()); +#endif + } + + SECTION("after stop") { + REQUIRE(repository.start()); + repository.waitUntilRunIsInProgress(); + repository.release(); + REQUIRE(repository.stop()); + + const BackTrace back_trace = repository.getTraces(); + CHECK(back_trace.name == "BlockingRepo"); + CHECK(back_trace.trace.empty()); + } +} + +TEST_CASE("ThreadedRepository returns an empty stack trace if run() has finished", "[ThreadedRepository][getTraces]") { + LogTestController::getInstance().setTrace<BackTrace>(); + + core::repository::NoOpThreadedRepository repository{"NoOpRepo"}; + REQUIRE(repository.start()); + CHECK(repository.isRunning()); + + // the thread exits as soon as it starts, as run() is empty; but if it hasn't exited yet, we'll get a non-empty trace + std::this_thread::sleep_for(100ms); + REQUIRE(minifi::test::utils::verifyEventHappenedInPollTime(1s, [&] { + const BackTrace back_trace = repository.getTraces(); + return back_trace.name == "NoOpRepo" && back_trace.trace.empty(); + })); + CHECK_FALSE(LogTestController::getInstance().contains("timed out waiting for the stack trace", 0s)); + + CHECK(repository.isRunning()); + CHECK(repository.stop()); + CHECK_FALSE(repository.isRunning()); +} + +} // namespace org::apache::nifi::minifi::core::test
