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
proposal: os/exec/v2: follow Context guidelines #26422
Comments
Like you say, adding those methods might cause confusion with CommandContext. I might be better just wait until Go2 where CommandContext can be removed and the signatures of
That should not be a pointer to a context.Context. |
Have you considered proposing this as a Go2 cleanup? It doesn't seem like adding functions and deprecating others will have any major advantage right now. |
exec
follow guidelines for using Context
objects
Thanks @bontibon. I fixed the call signatures. I agree, Go2 is the best target for this. Is there a different process for proposing Go2 changes? (Although, since the new methods don't break compatibility, they might be OK in a transitional Go 1.x release.) |
Also the names should be |
Proposal: Make
exec
follow guidelines for usingContext
objectsAuthor(s): Casey Barker
Last updated: 2018-07-17
Abstract
This proposal is to add
cmd.StartCtx()
andcmd.RunCtx()
methods to theexec
package, eventually deprecatingexec.CommandContext()
, and bringing it into compliance with the guidelines for use ofContext
objects.Background
The documentation for the
context
package says:The
exec.CommandContext()
function breaks this rule; it stores thectx
parameter inside theCmd
object until later referenced by thecmd.Start()
method (which is also called bycmd.Run()
). A caller ofcmd.Start()
orcmd.Run()
has no control over which context gets used in the execution.Issue originally raised here:
https://groups.google.com/forum/#!topic/golang-nuts/uvJIogNTD2c
Looking at the development history for the
exec
package's use ofcontext,
I suspect this inconsistency arose accidentally, as theCmd.ctx
field was used differently early in development.Proposal
exec
package:func (c *Cmd) StartCtx(ctx context.Context) error
func (c *Cmd) RunCtx(ctx context.Context) error
These two methods honor the passed-in context, rather than any context that might be attached to the
Cmd
.exec
package:func CommandContext(ctx context.Context, name string, arg ...string) *Cmd
Once the deprecation is complete, the private
ctx
field could be removed from theCmd
object.Rationale
The recommendations provided by the
context
package are solid; the context object should be passed at the time and place where a long-running operation is started. This makes it clear which calls are long-running, and it allows the source object (in this case, theCmd
) to be created without needing to know about thectx
that might eventually be used to control the execution.Compatibility
Part 1 (adding two new methods) does not break compatibility, although it introduces some possible confusion in that the new methods would ignore the context provided if
exec.CommandContext()
were initially used to create theCmd
object.Part 2 (deprecating the
exec.CommandContext()
function) is a hard break in compatibility.Implementation
TBD. This is my first proposal and I'm not yet familiar with the Go release process, but I'm willing to provide a patch if this proposal is acceptable.
The text was updated successfully, but these errors were encountered: