Explorar o código

HDFS-16254. Cleanup protobuf on exit of hdfs_allowSnapshot (#3518)

Gautham B A %!s(int64=3) %!d(string=hai) anos
pai
achega
e12cd0c638

+ 0 - 5
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-allow-snapshot/hdfs-allow-snapshot.cc

@@ -23,8 +23,6 @@
 #include <sstream>
 #include <string>
 
-#include <google/protobuf/stubs/common.h>
-
 #include "hdfs-allow-snapshot.h"
 #include "tools_common.h"
 
@@ -112,9 +110,6 @@ bool AllowSnapshot::HandlePath(const std::string &path) const {
     std::cerr << "Error: " << status.ToString() << std::endl;
     return false;
   }
-
-  // Clean up static data and prevent valgrind memory leaks
-  google::protobuf::ShutdownProtobufLibrary();
   return true;
 }
 } // namespace hdfs::tools

+ 14 - 1
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-allow-snapshot/main.cc

@@ -21,9 +21,22 @@
 #include <exception>
 #include <iostream>
 
+#include <google/protobuf/stubs/common.h>
+
 #include "hdfs-allow-snapshot.h"
 
 int main(int argc, char *argv[]) {
+  const auto result = std::atexit([]() -> void {
+    // Clean up static data on exit and prevent valgrind memory leaks
+    google::protobuf::ShutdownProtobufLibrary();
+  });
+  if (result != 0) {
+    std::cerr << "Error: Unable to schedule clean-up tasks for HDFS allow "
+                 "snapshot tool, exiting"
+              << std::endl;
+    std::exit(EXIT_FAILURE);
+  }
+
   hdfs::tools::AllowSnapshot allow_snapshot(argc, argv);
   auto success = false;
 
@@ -35,7 +48,7 @@ int main(int argc, char *argv[]) {
   }
 
   if (!success) {
-    exit(EXIT_FAILURE);
+    std::exit(EXIT_FAILURE);
   }
   return 0;
 }