Skip to content

Crash with "unreachable code was reached" when elapsed time is negative in metrics tracking #1907

@clee704

Description

@clee704

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:

  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:

void MetricsSubmission::track_elapsed_us(double value)
{
    if (!isfinite(value) || value <= 0.0)
    {
        return;  // Skip invalid measurements instead of crashing
    }
    elapsed_us = value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions