Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress throws OverflowException when task takes some time to increment the progress #169

Closed
HolisticDeveloper opened this issue Dec 15, 2020 · 4 comments · Fixed by #171
Assignees
Labels
bug Something isn't working

Comments

@HolisticDeveloper
Copy link

HolisticDeveloper commented Dec 15, 2020

Information

  • OS: Windows 10
  • Version: 0.34.0
  • Terminal: Windows Terminal

Describe the bug
I am using Progress while I am looping through a collection of items to process. It may take a couple of seconds to process each item in the loop. If the first item does not call task.Increment() shortly after creating the task, then Progress throws an OverflowException.

To Reproduce

namespace SpectreRepro
{
    using System.Threading.Tasks;

    using Spectre.Console;

    class Program
    {
        static async Task Main(string[] args)
        {
            await AnsiConsole
                .Progress()
                .Columns(new ProgressColumn[]
                {
                    new RemainingTimeColumn(),
                })
                .AutoClear(true)
                .StartAsync(async context =>
                {
                    var max = 10;

                    var task = context.AddTask($"Processing {max} jobs");
                    task.MaxValue = max;

                    for (int i = 1; i <= max; i++)
                    {
                        task.Description = $"Job {i}";

                        await Task.Delay(1000);
                        task.Increment(1);
                    }

                    task.StopTask();
                });
        }
    }
}

This ends up with the following exception:

Unhandled exception. System.OverflowException: TimeSpan overflowed because the duration is too long.
   at System.TimeSpan.Interval(Double value, Double scale)
   at System.TimeSpan.FromSeconds(Double value)
   at Spectre.Console.ProgressTask.GetRemainingTime() in /_/src/Spectre.Console/Progress/ProgressTask.cs:line 269
   at Spectre.Console.ProgressTask.get_RemainingTime() in /_/src/Spectre.Console/Progress/ProgressTask.cs:line 90
   at Spectre.Console.RemainingTimeColumn.Render(RenderContext context, ProgressTask task, TimeSpan deltaTime) in /_/src/Spectre.Console/Progress/Columns/RemainingTimeColumn.cs:line 23
   at Spectre.Console.Internal.DefaultProgressRenderer.<>c__DisplayClass12_1.<Update>b__0(ProgressColumn column) in /_/src/Spectre.Console/Progress/Renderers/DefaultProgressRenderer.cs:line 96
   at System.Linq.Enumerable.SelectListIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Spectre.Console.Internal.DefaultProgressRenderer.Update(ProgressContext context) in /_/src/Spectre.Console/Progress/Renderers/DefaultProgressRenderer.cs:line 96
   at Spectre.Console.ProgressContext.Refresh() in /_/src/Spectre.Console/Progress/ProgressContext.cs:line 59
   at Spectre.Console.Internal.ProgressRefreshThread.Run() in /_/src/Spectre.Console/Progress/ProgressRefreshThread.cs:line 46
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadHelper.ThreadStart()

Expected behavior
No exception should be thrown if tasks are taking a while. Progress Column should handle a "max value" estimated time remaining.

Screenshots
N/A

Additional context
N/A

@HolisticDeveloper HolisticDeveloper added the bug Something isn't working label Dec 15, 2020
@patriksvensson
Copy link
Contributor

@HolisticDeveloper That exception should only occur if the number of ticks is greater than 9223372036854775807.

I haven't been able to reproduce this myself. Could you try creating a minimal reproducible example of this?

@HolisticDeveloper
Copy link
Author

@patriksvensson I updated the first post with different code that reliably repros this on my system.

@patriksvensson
Copy link
Contributor

@HolisticDeveloper Great! I will see what's causing this

@patriksvensson patriksvensson self-assigned this Dec 16, 2020
patriksvensson added a commit that referenced this issue Dec 16, 2020
When calculating the remaining time for a progress task,
we divide the value delta with the current speed.
If the speed is zero, then the resulting double will
be 'infinity' which will lead to TimeSpan.FromSeconds
throwing.

This commit fixes that bug by setting the speed to 1
if it's 0 when calculating the remaining time.

Closes #169
@patriksvensson
Copy link
Contributor

@HolisticDeveloper I've fixed this bug and the fix should be available in v0.35 or the preview version if you want to try it out asap.

Thanks for reporting this! 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants