Run cleanup before exiting instead of calling atexit
MODIFY
include/vcpkg_Checks.h
include/vcpkg_Checks.h
namespace vcpkg::Checks
{
+ void register_console_ctrl_handler();
+
// Indicate that an internal error has occurred and exit the tool. This should be used when invariants have been
// broken.
[[noreturn]] void unreachable(const LineInfo& line_info);

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -191,12 +191,7 @@ int wmain(const int argc, const wchar_t* const* const argv)
GlobalState::timer = ElapsedTime::create_started();
- atexit([]() {
- auto elapsed_us = GlobalState::timer.microseconds();
- Metrics::track_metric("elapsed_us", elapsed_us);
- GlobalState::debugging = false;
- Metrics::flush();
- });
+ // Checks::register_console_ctrl_handler();
Metrics::track_property("version", Commands::Version::version());

MODIFY
src/vcpkg_Checks.cpp
src/vcpkg_Checks.cpp
#include "pch.h"
+#include "metrics.h"
#include "vcpkg_Checks.h"
+#include "vcpkg_GlobalState.h"
#include "vcpkg_System.h"
#include "vcpkglib.h"
namespace vcpkg::Checks
{
+ [[noreturn]] static void cleanup_and_exit(const int exit_code)
+ {
+ auto elapsed_us = GlobalState::timer.microseconds();
+ Metrics::track_metric("elapsed_us", elapsed_us);
+ GlobalState::debugging = false;
+ Metrics::flush();
+
+ ::exit(exit_code);
+ }
+
+ static BOOL CtrlHandler(DWORD fdwCtrlType)
+ {
+ Metrics::track_metric("SignalCaptured", fdwCtrlType);
+ cleanup_and_exit(EXIT_FAILURE);
+ }
+
+ void register_console_ctrl_handler() { SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE); }
+
[[noreturn]] void unreachable(const LineInfo& line_info)
{
System::println(System::Color::error, "Error: Unreachable code was reached");
@@ -13,14 +33,14 @@ namespace vcpkg::Checks
#ifndef NDEBUG
std::abort();
#else
- ::exit(EXIT_FAILURE);
+ cleanup_and_exit(EXIT_FAILURE);
#endif
}
[[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code)
{
Debug::println(System::Color::error, line_info.to_string());
- ::exit(exit_code);
+ cleanup_and_exit(exit_code);
}
[[noreturn]] void exit_with_message(const LineInfo& line_info, const CStringView errorMessage)

Run cleanup before exiting instead of calling atexit