Skip to content

Clamp invalid elapsed time instead of crashing in track_elapsed_us#1918

Merged
BillyONeal merged 1 commit intomicrosoft:mainfrom
clee704:fix/clamp-negative-elapsed-time
Feb 13, 2026
Merged

Clamp invalid elapsed time instead of crashing in track_elapsed_us#1918
BillyONeal merged 1 commit intomicrosoft:mainfrom
clee704:fix/clamp-negative-elapsed-time

Conversation

@clee704
Copy link
Contributor

@clee704 clee704 commented Feb 13, 2026

Summary

track_elapsed_us() crashes with Checks::unreachable when it receives a negative or non-finite elapsed time value. This can happen in containerized/VM environments where std::chrono::high_resolution_clock goes backwards (e.g. due to NTP adjustments).

Changes

  • src/vcpkg/metrics.cpp: Replace the Checks::unreachable call with clamping to 0.0 for negative and non-finite values. Since downstream serialization already skips elapsed_us == 0.0, the invalid metric is silently omitted from the JSON payload rather than crashing.
  • src/vcpkg-test/metrics.cpp: Added tests covering negative, NaN, ±infinity, valid positive, and zero inputs.

Fixes #1907

When high_resolution_clock goes backwards (e.g. in VMs/containers),
track_elapsed_us() receives a negative value and hits Checks::unreachable,
crashing vcpkg. Clamp negative and non-finite values to 0.0 instead,
which causes the metric to be silently skipped in the JSON payload.

Fixes microsoft#1907
@clee704 clee704 force-pushed the fix/clamp-negative-elapsed-time branch from 0c0b5f0 to 61a9940 Compare February 13, 2026 02:42
Copy link
Member

@BillyONeal BillyONeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@BillyONeal BillyONeal merged commit b6f9331 into microsoft:main Feb 13, 2026
7 checks passed
BillyONeal added a commit to BillyONeal/vcpkg that referenced this pull request Feb 18, 2026
https://github.com/microsoft/vcpkg-tool/releases/tag/2026-02-17

Meaningful changes:

* Test/fix internal dependencies in top level manifest. by @dg0yt in microsoft/vcpkg-tool#1890
* dependencies.cpp optimizations by @dg0yt in microsoft/vcpkg-tool#1891
* Switch default Ninja minimal version to a reasonable value by @cqundefine in microsoft/vcpkg-tool#1897
* Install files in parallel, redux by @BillyONeal in microsoft/vcpkg-tool#1896
* [tools] Fix missing executable permission on downloaded tools for Unix by @zynfly in microsoft/vcpkg-tool#1911
* Use libcurl rather than curl command line by @BillyONeal in microsoft/vcpkg-tool#1906
* Deduplicate "additional_file" in abi info. by @BillyONeal in microsoft/vcpkg-tool#1914
* Clamp invalid elapsed time instead of crashing in track_elapsed_us by @clee704 in microsoft/vcpkg-tool#1918
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants