Skip to content
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/signal: Notify for SIGUSR2 from the same proc inside a goroutine doesn't work #12484

Closed
willfaught opened this issue Sep 3, 2015 · 2 comments

Comments

@willfaught
Copy link
Contributor

A process can't use os/signal.Notify inside a goroutine to signal itself with SIGUSR2.

Works as expected (outside the goroutine):

func main() {
    var c = make(chan os.Signal, 1)
    signal.Notify(c) // outside the goroutine

    var done = make(chan struct{})
    go func() {
        fmt.Println(<-c)
        done <- struct{}{}
    }()
    if err := syscall.Kill(os.Getpid(), syscall.SIGUSR2); err != nil {
        panic(err)
    }
    <-done
}

Doesn't work as expected (inside the goroutine):

func main() {
    var done = make(chan struct{})
    go func() {
        var c = make(chan os.Signal, 1)
        signal.Notify(c) // inside the goroutine

        fmt.Println(<-c)
        done <- struct{}{}
    }()
    if err := syscall.Kill(os.Getpid(), syscall.SIGUSR2); err != nil {
        panic(err)
    }
    <-done
}

Expected: Doesn't hang
Actual: Hangs

The Notify documentation doesn't say it has to be called in the main goroutine.

@stemar94
Copy link

stemar94 commented Sep 3, 2015

Beware of race conditions...

func main() {
    var done = make(chan struct{})
    var doneReg = make(chan struct{})
    go func() {
        var c = make(chan os.Signal, 1)
        signal.Notify(c) // inside the goroutine
        doneReg <- struct{}{} //signal registration
        fmt.Println(<-c)
        done <- struct{}{}
    }()
    <-doneReg  //wait for registration
    if err := syscall.Kill(os.Getpid(), syscall.SIGUSR2); err != nil {
        panic(err)
    }
    <-done
}

Marvin

@willfaught
Copy link
Contributor Author

Ouch, didn't see it. Thanks!

@golang golang locked and limited conversation to collaborators Sep 4, 2016
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

3 participants