Crash with "unreachable code was reached" when elapsed time is negative in metrics tracking
## 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:
```cpp
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:
1. Use `std::chrono::steady_clock` instead of `high_resolution_clock` (guaranteed monotonic)
2. Clamp negative elapsed times to 0 or a small positive value instead of crashing
3. Skip metrics submission when elapsed time is invalid
Example fix for option 3:
```cpp
void MetricsSubmission::track_elapsed_us(double value)
{
if (!isfinite(value) || value <= 0.0)
{
return; // Skip invalid measurements instead of crashing
}
elapsed_us = value;
}
```
关闭于 2026-02-13 2 条评论