-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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 doesn't catch signals when connecting os.Stdin to a subcommand #20159
Comments
If I understand correctly, what you want to do here is to capture the interrupt request of the program. This isn't an issue with the // Suppose we build this into a binary "hi"
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("HI!")
time.Sleep(20 * time.Millisecond)
}
} Then we modify your program a bit to be: package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("[SIGINT force quit]")
os.Exit(0)
}()
err := Run()
fmt.Println(err)
}
func Run() error {
cmd := exec.Command("./hi")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
return cmd.Run()
} If you run this, you'll see that In conclusion, the issue seems to be with how the |
/cc Mr. Signals @ianlancetaylor |
Let's not all start calling me Mr. Signals.... |
I don't know much about docker, but @matipan 's comment looks plausible to me. I'm going to close this issue, since it doesn't seem that there is anything we can do to fix it in Go. @mbertschler please comment if you disagree. |
In some cases node Docker containers do not exit when you press ^C. I am writing a tool that starts these Docker containers, and want to catch the ^C and kill the program manually. The problem is just that once I connect the subcommands stdin with
cmd.Stdin = os.Stdin
, I do not get notified of ^C anymore.I tried to reproduce the Docker behavior with a small Go program by catching signals and ignoring them, but that attempt was unsuccessful. This is why the reproducer is using Docker.
What version of Go are you using (
go version
)?go version go1.8.1 darwin/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/GQ1YHd2Af-
What did you expect to see?
$ signalkiller > [SIGINT force quit]
What did you see instead?
To exit the node prompt you have to press ^C twice, but with this code it should exit immediately after the first one.
The text was updated successfully, but these errors were encountered: