Handle ctrl+c to cancel long-running commands
Forked from #368 - we should look into handling ctrl+c to cancel long-running commands (if one is running) rather than closing the whole session.
cc @sayedihashimi
I looked into this a bit more and lets take these two urls as examples.
-
https://azuresearch-usnc.nuget.org/query?q=templates -
https://www.nuget.org/api/v2/package/SlowCheetah/2.5.48
https://azuresearch-usnc.nuget.org/query?q=templates
When I start httprepl (without a base uri) and issue get https://azuresearch-usnc.nuget.org/query?q=templates when I CTRL + C the console spew continues. Console spew should stop immediately after CTRL + C. It doesn't kill the httprepl command itself though, so that is good.
https://www.nuget.org/api/v2/package/SlowCheetah/2.5.48
In this case a binary file (.nupkg file) is being downloaded. After I issue get https://www.nuget.org/api/v2/package/SlowCheetah/2.5.48 and then press CTRL+C, console spew is stopped but httprepl command is killed. In this case the console spew should stop immediately, but the httprepl command should not be killed.
@sayedihashimi When I try this with the second URL, the download is so fast that by the time I hit Ctrl+C, the spew is done and I'm back at the prompt and that's why it exits. Can you double check and see if that's what you were seeing too in that scenario? I see the same thing as you for the first scenario. I just want to make sure I'm chasing the right problem.
Also, it turns out the whole body is being written out as one Console.WriteLine() call. Which is why you can't Ctrl+C in the first scenario - its just a single call.
We'll probably need to change the code to format and write line by line if we want it to be cancellable. But that's a much bigger lift than just putting in some extra checks for cancellation like I thought this would be. Will have to dig in and take a look.
@tlmii if the second case, if the download is very fast, select a nupkg that is larger, for example https://www.nuget.org/api/v2/package/IdentityServer4.Templates/4.0.1
I wasn't able to repro the second issue, I think the issue was what you were mentioning on the call. The command completed and then I hit CTRL + C.
Alright, so I think this issue just generally boils down to what I said in https://github.com/dotnet/HttpRepl/issues/370#issuecomment-747922846, that we're doing one single Console.WriteLine for the entire already-formatted body and that one Console.WriteLine call, with such a huge string of text is both not cancellable and takes a really long time to render to the screen. So we'll need to come up with a way to address that, while also looking at the relevant performance issues.
For instance, we could format and write each line to the screen and check for cancellation in between. But I am guess that doing 1000s of Console.WriteLine calls will be slower than doing the one. It'd be cancellable in the case the user wanted to cancel it... but it'd take longer in all the other (more common) cases. I'll have to figure out how to get some metrics on this.