-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
os/exec: CommandContext does not forward the context's error on timeout #21880
Comments
What do you suggest should happen? |
I'm very sorry for the long delay. I missed the notification.
BTW thanks for adding this to the 1.10 milestone! :) |
I guess it makes sense that cmd.Run returns an What if we the ExitError's
|
This gets at a more general problem of chaining errors: ideally, I agree with @mmikulicic that It would be nice if we had some standard means to express that. |
Also note that if you cancel a In general, if you cancel a |
Does this mean it's up to the caller to do something with it, e.g.:
or:
or some custom (or yet to be defined) helper:
? |
None of the callers you're describing there have a func Foo(ctx context.Context) (Bar, error) {
out, err := exec.CommandContext(ctx, …).Output()
if err != nil {
return Bar{}, err
}
return parseBar(out)
}
…
func SomeOtherFunction(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, veryLongWatchdogTimeout)
defer cancel()
bar, err := Foo(ctx)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return watchdogError(err)
}
return internalError(err)
}
return doSomethingWith(bar)
} Honestly, though, I don't really understand most uses of |
yeah I forgot declaring the signature in the
are you suggesting that it's better to explicitly set up a timer that calls cancel instead of having this functionality built in the context library? |
No, I'm suggesting that most callers should propagate cancellation (e.g. from an incoming |
ah, well, the parent timeout is preserved, I guess this is just about whether or not it makes sense to allow a call to further narrow down the timeout; we're getting off topic :-) |
I agree it makes sense to return an error that indicates the process was terminated when a process is spawned but killed, rather than returning a A vaguely related incosistency I'd like to point out: |
As shown in [this issue](golang/go#21880), running a command with a deadline or timeout causes the process to be killed properly, but the error returned is that it was killed, without any more information. To know that it timed out, one has to use the context itself via `context.Err()`. We now do this and also set the timeout flag of the command to add a second, to ensure we can properly detect time outs, as otherwise it's difficult to know why the command exited since it always uses the same exit code on error.
As shown in [this issue](golang/go#21880), running a command with a deadline or timeout causes the process to be killed properly, but the error returned is that it was killed, without any more information. To know that it timed out, one has to use the context itself via `context.Err()`. We now do this and also set the timeout flag of the command to add a second, to ensure we can properly detect time outs, as otherwise it's difficult to know why the command exited since it always uses the same exit code on error.
As shown in [this issue](golang/go#21880), running a command with a deadline or timeout causes the process to be killed properly, but the error returned is that it was killed, without any more information. To know that it timed out, one has to use the context itself via `context.Err()`. We now do this and also set the timeout flag of the command to add a second, to ensure we can properly detect time outs, as otherwise it's difficult to know why the command exited since it always uses the same exit code on error.
As shown in [this issue](golang/go#21880), running a command with a deadline or timeout causes the process to be killed properly, but the error returned is that it was killed, without any more information. To know that it timed out, one has to use the context itself via `context.Err()`. We now do this and also set the timeout flag of the command to add a second, to ensure we can properly detect time outs, as otherwise it's difficult to know why the command exited since it always uses the same exit code on error.
As shown in [this issue](golang/go#21880), running a command with a deadline or timeout causes the process to be killed properly, but the error returned is that it was killed, without any more information. To know that it timed out, one has to use the context itself via `context.Err()`. We now do this and also set the timeout flag of the command to add a second, to ensure we can properly detect time outs, as otherwise it's difficult to know why the command exited since it always uses the same exit code on error.
Go version: Latest release - 1.9
Using
CommandContext
in combination withContext.WithTimeout
shows very few information about the reason of the error when the execution times out.Using a small variation of the CommandContext example
proves this:
Output:
Also, if the context is already done then the
cmd.Run
gets called, the error matches the context's error:Output:
The text was updated successfully, but these errors were encountered: