Copilot commented on code in PR #2292:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2292#discussion_r4387620504
##########
minifi_main/MiNiFiMain.cpp:
##########
@@ -290,7 +290,17 @@ int main(int argc, char **argv) {
process_running.test_and_set();
std::atomic<bool> restart_token{false};
- const auto request_restart = [&] {
+ std::atomic<int32_t> exit_code{0};
+ const auto request_shutdown = [&] (minifi::ShutdownType shutdown_type,
std::optional<int32_t> shutdown_exit_code) {
+ if (shutdown_type == minifi::ShutdownType::SHUTDOWN) {
+ restart_token = false;
+ exit_code = shutdown_exit_code.value_or(0);
Review Comment:
A timeout shutdown can be undone by an in-flight C2 restart. If the watchdog
fires while `produce()` is sending a restart acknowledgement, the producer can
subsequently invoke the RESTART callback and set `restart_token` back to true,
causing the main loop to restart instead of exiting with code 90. Make shutdown
a terminal, latched state and coordinate the callback and restart-loop
transitions so a later restart cannot override it.
##########
libminifi/src/c2/C2Agent.cpp:
##########
@@ -130,6 +152,7 @@ void C2Agent::stop() {
thread_pool_.stopTasks(id.to_string());
}
thread_pool_.shutdown();
+ connection_watchdog_.reset();
Review Comment:
The watchdog remains active while `stopTasks()` waits for running C2
requests to finish (`core-framework/src/utils/ThreadPool.cpp:180-184`). If an
in-flight heartbeat takes longer than the remaining timeout, stopping the agent
can still trigger the shutdown callback and change a normal process exit to
code 90 before this reset is reached. Request watchdog cancellation before
waiting for the workers, while retaining the watchdog object until those
workers can no longer access it.
##########
libminifi/src/c2/C2ConnectionWatchdog.cpp:
##########
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "c2/C2ConnectionWatchdog.h"
+
+#include <condition_variable>
+#include <mutex>
+#include <stdexcept>
+
+#include "c2/C2ExitCodes.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::c2 {
+
+C2ConnectionWatchdog::C2ConnectionWatchdog(std::chrono::milliseconds
connection_timeout, minifi::RequestShutdownCallback request_shutdown)
+ : connection_timeout_(connection_timeout),
+ request_shutdown_(std::move(request_shutdown)) {
+ if (connection_timeout_ <= 0ms) {
+ throw std::invalid_argument("C2 connection timeout must be greater than
0");
+ }
+
+ watchdog_thread_ = std::jthread([this](const std::stop_token& st) {
+ std::condition_variable_any cv;
+ std::mutex sleep_mutex;
+ std::unique_lock<std::mutex> sleep_lock(sleep_mutex);
+ while (!st.stop_requested()) {
+ const auto deadline = last_heartbeat_.load() + connection_timeout_;
+ if (std::chrono::steady_clock::now() >= deadline) {
Review Comment:
The clock read can be compared against a stale heartbeat timestamp. If the
watchdog is descheduled after loading `last_heartbeat_`, successful heartbeats
can keep updating it while the watchdog is paused; when it resumes after the
captured deadline, it shuts down the agent without checking those updates.
Sample the current time before loading the heartbeat timestamp so scheduler
delays cannot turn continuous successful heartbeats into a timeout.
--
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]