Azure / Azure/azure-functions-host

Make MetricEvent.Completed more robust

Open
#6,973 0 comments 0 reactions 2 assignees Claimed by @mathewc View on GitHub
bug
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

The check performed to determine whether an event is complete is based on checking whether the Duration (as set when the event ends) is equal to the default TimeSpan. When an event ends, generally Duration is set to DateTime.UtcNow - Timestamp. However for events that happen quickly (within the resolution of the system clock, e.g. 15ms) these times can be equal, resulting in Completed returning false, when it should return true. We should change this event class so completion is explicitly set as a boolean on event completion and not computed.

Based on how this Completed property is used, it can cause metering issues.

The fact that DateTime.UtcNow can return the same value on subsequent calls can be reproed via the below code. Note that the repro is system dependent.

```csharp
for (int i = 0; i < 1000000; i++)
{
var t1 = DateTime.UtcNow;
var delta = DateTime.UtcNow - t1;
if (delta == default(TimeSpan))
{
throw new Exception("Kaboom!");
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.