runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Timeout.InfiniteTimeSpan behaves counter-intuitively for some comparisons

Open invidious9000 opened this issue 6 years ago • 15 comments

Description

Timeout.InfiniteTimeSpan uses an internal representation of -1ms to define a period of infinite time. The operators for <, >, >=, <= in TimeSpan compare ticks between two instances. This leads to comparisons that seem invalid when naively using InfiniteTimeSpan (negative ticks) on one side of the comparison.

Example:

var oneSecond = TimeSpan.FromSeconds(1);
bool oneSecondIsLessThanInfinity = oneSecond < Timeout.InfiniteTimeSpan;

In this scenario oneSecondIsLessThanInfinity is false when one might expect it to be true.

.NET Core 3.1

invidious9000 avatar Jun 11 '20 23:06 invidious9000

FWIW, same on .NET Framework.

danmoseley avatar Jun 12 '20 00:06 danmoseley

I agree it's not intuitive, but there's very little that can be done about this. The meaning of the -1 value as a timeout is entirely up to how the consumer is defined and interprets it. Changing the comparison operations to special-case that value would also be very unintuitive, and a massive breaking change.

stephentoub avatar Jun 12 '20 00:06 stephentoub

TimeSpan remarks indicate that the value contained is some count of ticks, and this can be enough information to reason about the behavior, but the comparison operators seem to be a slightly more opaque implementation detail (you must look at source to see why it behaves this way).

Could this be considered a doc issue? I see that kinda-similar concerns were identified/discussed in https://github.com/dotnet/runtime/issues/30723#issuecomment-574308350

This really should be a doc issue demonstrating correct use of the existing Timeout API

Is there any value in augmenting documentation for InfiniteTimeSpan to explicitly indicate nuances around comparison or is it reasonable to expect consumers to infer these from tick remarks? Would the same suggestions advocating for IntelliCode enhancements as outlined in https://github.com/dotnet/runtime/issues/30723#issuecomment-526676911 apply?

Could it be appropriate to add a see-also for TimeSpan.MaxValue if that can sometimes be an acceptable substitute?

invidious9000 avatar Jun 12 '20 01:06 invidious9000

Thanks for the suggestions. Improving the docs is certainly possible! And we welcome contributions there, too. https://github.com/dotnet/dotnet-api-docs

stephentoub avatar Jun 12 '20 02:06 stephentoub

I agree it's not intuitive, but there's very little that can be done about this.

@stephentoub this could be a prime use case for discriminated unions when it is introduced.

julealgon avatar Jan 07 '25 13:01 julealgon

I agree it's not intuitive, but there's very little that can be done about this.

@stephentoub this could be a prime use case for discriminated unions when it is introduced.

How so?

stephentoub avatar Jan 07 '25 13:01 stephentoub

How so?

@stephentoub by isolating the "Infinite" value and decoupling it from using a negative -1 integer internally: it would become a "special value" that could then have equality handled differently.

julealgon avatar Jan 07 '25 14:01 julealgon

How so?

@stephentoub by isolating the "Infinite" value and decoupling it from using a negative -1 integer internally: it would become a "special value" that could then have equality handled differently.

But how would that help with the existing API?

stephentoub avatar Jan 07 '25 14:01 stephentoub

But how would that help with the existing API?

It would eliminate the need to handle negative numbers as special values which by itself makes the API cleaner. But I was referring to the problem described here of comparing timeout values. That could also be cleanly resolved by having the "infinite" value be a discriminated union possibility instead of a magic "-1".

julealgon avatar Jan 07 '25 15:01 julealgon

That could also be cleanly resolved by having the "infinite" value be a discriminated union possibility instead of a magic "-1".

That would mean changing the type of Infinite{TimeSpan}, which won't happen.

stephentoub avatar Jan 08 '25 14:01 stephentoub

That would mean changing the type of Infinite{TimeSpan}, which won't happen.

I would expect you to say that, but I hope you are (eventually) wrong. If the .NET team doesn't come up with a decent way to deprecate things over time the language will be overwhelmed by technical debt.

Yes, I know you did make some very specific breaking changes, such as the ones around NRT and the field keyword, but that's not enough (IMHO). I hope the team realizes this before things get out of hand.

julealgon avatar Jan 08 '25 18:01 julealgon

Deprecating things causes debt as well. You have to weigh the value you get out of the damage you cause. In the vast majority of cases, things are working fine for the vast majority of users. So breaks would hurt the vast majority just to deal with a minor issue hit by a tiny set of users.

This is not a good way to handle things. Breaks are made when the value is there to justify it.

CyrusNajmabadi avatar Jan 09 '25 15:01 CyrusNajmabadi

Deprecating things causes debt as well. You have to weigh the value you get out of the damage you cause.

This is true. But if done properly, that type of debt is temporary.

Versus permanent tech debt that is created if you keep supporting the old thing forever.

There is certainly a trade-off to be made there.

So breaks would hurt the vast majority just to deal with a minor issue hit by a tiny set of users. ... This is not a good way to handle things. Breaks are made when the value is there to justify it.

@CyrusNajmabadi I disagree with the approach because if you always follow that rule, you will end up in a situation where you are killed by a thousand cuts: each small thing "is too low-value to be considered in isolation", but now you have thousands of such small issues scattered throughout the .NET ecosystem and this makes the language/framework less intuitive, less consistent, etc, turning people off. I'm not saying .NET is already in that position, but I would argue that it will invariably happen, if nothing is done about the problem conceptually.

Again, I don't want to derail this with this meta discussion. I just mentioned it here because this is one clear case where discriminated unions would come in very handy to improve an existing abstraction, and this will keep happening more and more as the language evolves, to a point where (I think) you will need to do something about it, if you want to keep the language ecosystem healthy.

If you'd rather restart from scratch and create a C#++ at that point, then that's up to you... I hope that is not the course Microsoft wants to follow as it would massively fracture the community when it happens (much more so than a breaking change would).

julealgon avatar Jan 09 '25 18:01 julealgon

but I would argue that it will invariably happen,

We're 25 years in. So far, everything is still going strong. In practical terms we do not see that people "are killed by a thousand cuts" or are complaining that that's the case.

If you'd rather restart from scratch and create a C#++ at that point, then that's up to you...

I think such a claim would need a ton of evidence to back it up. C# and .net did a ton to avoid the mistakes of c++. And 25 years in I think shows itself to be much healthier and in a much better than c++ is in.

CyrusNajmabadi avatar Jan 10 '25 14:01 CyrusNajmabadi

Closing. This is by design and comes from using a sentinel value.

tannergooding avatar Jul 23 '26 19:07 tannergooding