Skip to content

proposal: add context cancellation mechanisms to os/signal #16472

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

Closed
DeedleFake opened this issue Jul 22, 2016 · 2 comments
Closed

proposal: add context cancellation mechanisms to os/signal #16472

DeedleFake opened this issue Jul 22, 2016 · 2 comments

Comments

@DeedleFake
Copy link

Add a new function to os/signal which creates a context which is canceled when a matched signal is received. It's very simple. In fact, it's so simple that I won't be surprised if this gets rejected purely on the basis of it being easy to just write when you need it, but it's also possible that better mechanisms can be implemented by putting it in the actual package and giving it access to the package's innards. I'm not familiar enough with how os/signal works to be sure.

Here's a protoype:

func WithContext(ctx context.Context, sig ...os.Signal) context.Context {
    ctx, cancel := context.WithCancel(ctx)
    go func() {
        c := make(chan os.Signal)
        signal.Notify(c, sig...)
        defer signal.Stop(c)

        select {
        case <-ctx.Done():
        case <-c:
            cancel()
        }
    }()

    return ctx
}
@ianlancetaylor ianlancetaylor added this to the Proposal milestone Jul 22, 2016
@ianlancetaylor
Copy link
Contributor

I'm not convinced this is useful enough. Signals are a fairly heavy-weight mechanism, and usually cause some fairly heavy-weight action (or something small, like printing a message). I don't think many programs are going to simply want to cancel a context, which I think is neither a heavy-weight or a small action. And, as you say, it's easy enough to write yourself for programs that want it.

@pciet
Copy link
Contributor

pciet commented Jul 25, 2016

If the Deadline can be changed then each check in the flow of independent actions on the context can verify the deadline has not passed, effectively stopping the flow of action. I'm not sure what this looks like in practice, just got started with the context package as a solution. And doing a fancy time check is maybe not the most efficient approach.

@adg adg closed this as completed Aug 29, 2016
@golang golang locked and limited conversation to collaborators Aug 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants