Skip to content

os/signal: Notify for SIGUSR2 from the same proc inside a goroutine doesn't work #12484

@willfaught

Description

@willfaught

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions