Summary
vcpkg crashes intermittently with "unreachable code was reached" when track_elapsed_us() receives a negative elapsed time value. This happens in containerized/VM environments where std::chrono::high_resolution_clock can go backwards.
Error Message
Elapsed time to handle boost-utility:x64-linux: -1.29e+08 ns
vcpkg internal error: unreachable code was reached
...
Root Cause
In src/vcpkg/metrics.cpp, the track_elapsed_us() function has this assertion:
void MetricsSubmission::track_elapsed_us(double value)
{
if (!isfinite(value) || value <= 0.0)
{
Checks::unreachable(VCPKG_LINE_INFO); // Crashes here
}
elapsed_us = value;
}
The problem is that std::chrono::high_resolution_clock is not guaranteed to be monotonic. In VMs, containers, or when NTP adjusts time, the clock can go backwards, producing negative elapsed time.
Environment
- OS: CBL-Mariner 2.0 (Linux container) (Also happens in Azure Linux 3.0)
- Context: CI/CD pipeline (Azure DevOps)
- vcpkg version: latest (cloned from main)
Workaround
Disable metrics with -disableMetrics flag or VCPKG_DISABLE_METRICS=1 environment variable.
Disabling metrics still invokes track_elapsed_us.
Suggested Fix
Either:
- Use
std::chrono::steady_clock instead of high_resolution_clock (guaranteed monotonic)
- Clamp negative elapsed times to 0 or a small positive value instead of crashing
- Skip metrics submission when elapsed time is invalid
Example fix for option 3:
void MetricsSubmission::track_elapsed_us(double value)
{
if (!isfinite(value) || value <= 0.0)
{
return; // Skip invalid measurements instead of crashing
}
elapsed_us = value;
}
Summary
vcpkg crashes intermittently with "unreachable code was reached" when
track_elapsed_us()receives a negative elapsed time value. This happens in containerized/VM environments wherestd::chrono::high_resolution_clockcan go backwards.Error Message
Root Cause
In
src/vcpkg/metrics.cpp, thetrack_elapsed_us()function has this assertion:The problem is that
std::chrono::high_resolution_clockis not guaranteed to be monotonic. In VMs, containers, or when NTP adjusts time, the clock can go backwards, producing negative elapsed time.Environment
Workaround
Disable metrics with-disableMetricsflag orVCPKG_DISABLE_METRICS=1environment variable.Disabling metrics still invokes
track_elapsed_us.Suggested Fix
Either:
std::chrono::steady_clockinstead ofhigh_resolution_clock(guaranteed monotonic)Example fix for option 3: