-
Notifications
You must be signed in to change notification settings - Fork 385
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
Moving CancelOnProcessTermination out of middleware, better cancellation support #2044
Moving CancelOnProcessTermination out of middleware, better cancellation support #2044
Conversation
…s single thread) and accessed only with Interlocked.Exchange
…(less boilerplate and allocs)
…text.BindingContext getter, as BindingContext ctor already does that
…en in explicit way to Handler
@jonsequitur the Linux CI leg might be red, but please take a look at the PR description and let me know what do you think about such changes. I want to make sure I am going in the right direction before I spend more time working on it. |
There's a third reason why people use |
…s.WaitForExitAsync is available only on .NET 7+
d3016f2
to
577765a
Compare
// Verify the process terminates timely | ||
bool processExited = process.WaitForExit(10000); | ||
if (!processExited) | ||
[Fact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for using the runtime platform check rather than making a LinuxOnlyFact
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial intention was to enable it on macOS too, but then it turned out that RemoteExecutor
does not support this scenario, so instead of adding new attribute (This repo defines only LinuxOnlyTheory
attribute. ) I've added the ugly if statement.
Co-authored-by: Kevin B <Keboo@users.noreply.github.com>
@Keboo thanks a lot for the review! |
This PR had an unexpected effect in my application. I had code like the following: public static RootCommand MakeRootCommand()
{
var rootCommand = new RootCommand();
AddSymbols(rootCommand);
rootCommand.SetHandler(
(InvocationContext context) => MyHandler.InvokeAsync(
parameters: GetMyCommandParameters(context),
console: context.Console));
return rootCommand;
}
private static async Task<int> InvokeAsync(
MyCommandParameters parameters,
System.CommandLine.IConsole console)
{
…
} That code used to call SetHandler(this Command, Func<InvocationContext, Task>), but after this PR, it started calling SetHandler(this Command, Action<InvocationContext>) instead; thus discarding the Task that InvokeAsync returned. Fortunately, not awaiting the Task caused warning CS4014. Anyway, this is a risk for developers upgrading from prior versions of System.CommandLine. |
Thanks for the feedback! I currently don't see a way to avoid that, but in the very near future we are most likely going to replace And in general the |
Asynchronous APIs have two advantages: improved scalability and cancellation support. In the case of command line applications, we mostly care about the latter.
In my opinion storing the
CancellationToken
insideInvocationContext
is not intuitive, as C# devs are just used to passing an instance of CT to async methods. I expect that most of the S.CL users useInvokeAsync
and never check for the cancellation, so they don’t take advantage of any async benefits.I’ve made
CancellationToken
a mandatory argument for the asyncSetHandler
overload. Now, when the users don’t pass it to any async method used inside the handler, they are going to get a compiler warning:It allows us for removing
GetCancellationToken
,LinkToken
andDispose
fromInvocationContext
and gets us closer to the goal of replacing all usages ofInvocationContext
with justParseResult
.Another goal was moving
CancelOnProcessTermination
out of middleware. This was not easy, but I've managed to get it working and make it easier to use.What I don't like is adding another argument to
GetHandler
extension methods. Since we are going to remove all of them soon, I hope it's not an issue.The app I've used for testing:
fixes #1783
fixes #1921
fixes #2019