fix(telemetry): guard MetricMacros.h against WinSock1/Boost.Asio clash

On Windows the telemetry unit test src/tests/libxrpl/telemetry/MetricMacros.cpp
fails to compile with boost/asio/detail/socket_types.hpp C1189
"WinSock.h has already been included". Root cause traced with clang -E -H:
MetricMacros.h pulls MetricsRegistry.h -> OTel spin_lock_mutex.h, which
defines _WINSOCKAPI_ and includes <windows.h> (WinSock 1); it then pulls
ServiceRegistry.h -> <boost/asio.hpp>, whose socket_types.hpp requires
winsock2.h first. Production consumers (RCLConsensus.cpp, PerfLogImp.cpp)
happen to include Boost.Asio via other xrpld headers first, so only the test
TU -- which includes MetricMacros.h first -- hit it.

Pre-include boost/asio/detail/socket_types.hpp under _MSC_VER at the top of
the header so winsock2.h wins for every consumer. Mirrors the existing guard
in MetricsRegistry.cpp.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pratik Mankawde
2026-07-22 18:48:13 +01:00
parent 3c86cea742
commit b68f7dc1f4

View File

@@ -101,6 +101,18 @@
* tasks/metric-macro-plan.md.
*/
// On Windows, OTel's spin_lock_mutex.h (transitively included from
// MetricsRegistry.h) defines _WINSOCKAPI_ and includes <windows.h>, which
// pulls in WinSock 1. ServiceRegistry.h below then includes <boost/asio.hpp>,
// whose socket_types.hpp requires winsock2.h first and errors out if WinSock 1
// arrived earlier. Pre-including boost's socket types header here gets
// winsock2.h in before the OTel headers, so any translation unit that includes
// MetricMacros.h first (e.g. the telemetry unit tests) still compiles. The
// production MetricsRegistry.cpp carries the same guard.
#ifdef _MSC_VER
#include <boost/asio/detail/socket_types.hpp>
#endif
#include <xrpld/telemetry/MetricsRegistry.h> // IWYU pragma: keep
#include <xrpl/core/ServiceRegistry.h> // IWYU pragma: keep