In Development
Last Updated: 29 Sep 2025 14:27 by ADMIN
Piet
Created on: 11 Jun 2025 13:40
Category: Telerik Document Processing
Type: Bug Report
0
DocumentProcessing: CancelationTokenSourceFactory.CreateTokenSource(TimeSpan? timeSpan) does not do proper argument checking

Read the documentation for CancelationTokenSource.CancelAfter:

this method will throw an ArgumentOutOfRangeException when: delay.TotalMilliseconds is less than -1 or greater than Int32.MaxValue (or UInt32.MaxValue - 1 on some versions of .NET). Note that this upper bound is more restrictive than TimeSpan.MaxValue.

----------------------------------------------------

your code in CancelationTokenSourceFactory.CreateTokenSource does this check:

if (timeSpan.HasValue && timeSpan.Value != TimeSpan.MaxValue)

this check for TimeSpan.MaxValue seems totally pointless here, if timeSpan is anything between ~2147483647 and 922337203685476 milliseconds long this will still just throw a ArgumentOutOfRangeException.

I suspect that this check was intended as a way to prevent creating a cancellation timer that never triggers in the CancellationTokenSource, which should look like this:

if (timeSpan.HasValue && timeSpan != Timeout.InfiniteTimeSpan) //Timeout.InfiniteTimeSpan is -1 milliseconds
which still seems like premature optimization with no noticeable benefit but at least it isn't completely pointless.
3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 29 Sep 2025 14:27

Hello,

I would like to follow up with additional information about the existing behavior of our timeout mechanism and how it will be changed in the next version.

Our CancelationTokenSourceFactory internlly uses the CancelAfter(TimeSpan delay) method. It is expected to throw an ArgumentOutOfRangeException when delay.TotalMilliseconds is less than -1 or greater than Int32.MaxValue (or UInt32.MaxValue - 1 on some versions of .NET). Note that this upper bound is more restrictive than TimeSpan.MaxValue:
https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancelafter?view=net-9.0#system-threading-cancellationtokensource-cancelafter%28system-timespan%29 

Now, the timeout mechanism will use Timeout.InfiniteTimeSpan as an upper boundary. InfiniteTimeSpan is a constant used to specify an infinite waiting period, for methods that accept a TimeSpan parameter: https://learn.microsoft.com/en-us/dotnet/api/system.threading.timeout.infinitetimespan?view=net-9.0 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Your perspective matters! Join other professionals in the State of Designer-Developer Collaboration 2025: Workflows, Trends and AI survey to share how AI and new workflows are impacting collaboration, and be among the first to see the key findings.
Start the 2025 Survey
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 17 Jun 2025 07:09

Hi, Piet,

I am sorry to hear that you are facing any difficulties with the timeout mechanism offered by Telerik Document Processing.

Indeed, the referred feedback item is related to the TimeSpan.MaxValue handling. The TimeSpan interval is up to the developer and should be considered with the environment-specific configurations. In case of developing a web application for example, set such a timeout interval value that would be safe enough to protect the application from potential DDoS attacks. If the application is expected to be delivered directly to the end-users, it is possible to use TimeSpan=null as well.

Since the CancellationTokenSource class offers two overloads for the CancelAfter method, CancelAfter(Int32) and CancelAfter(TimeSpan), we are expected to handle any exceptions if a valid interval is passed as an argument.

According to the MSDN information, ArgumentOutOfRangeException is thrown when: 

delay.TotalMilliseconds is less than -1 or greater than Int32.MaxValue (or UInt32.MaxValue - 1 on some versions of .NET). Note that this upper bound is more restrictive than TimeSpan.MaxValue.

I have approved this bug report. I have also updated your Telerik points for bringing this to our attention. We will do our best to address it properly. Make sure that you click the Follow button to get notified for any status changes.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Piet
Posted on: 11 Jun 2025 13:50

this check was probably added in reponse this this bug ticket: https://feedback.telerik.com/document-processing/1680730-spreadprocessing-argumentoutofrangeexception-specified-argument-was-out-of-the-range-of-valid-values-parameter-delay-when-using-timeout-with-timespan-maxvalue

but this is not a good solution at all, plus it only 'solves' the thrown exception for a single case out of literal trillions, and does so by simply ignoring the given TimeSpan.

If an invalid timeout is provided, the method should throw an ArgumentOutOfRangeException. If users do not want a timeout, they should pass either a null or Timeout.InfiniteTimespan, not TimeSpan.MaxValue.

now there is just this weird situation where passing TimeSpan.MaxValue does nothing even though is this different from normal C# timeout cancellation behaviour and therefore unexpected and not intuitive.