Closed soh335 closed 7 years ago
あざます
いまさらなのですが、この処理は
という処理で、Exited がしばらく false を返すのはいいのかなーと思っています。おそらく CTRL-C した時点で sleep が死んでいたという事ですか?(すいません、先に言えって話ですね)
シグナルを送って死んだ場合は、Exited はずっと false になってしまうみたいなんです。自分から死んだ場合のみ Exited() が true のようです。 なのでプロセスが終わったかどうかは cmd.Wait が抜ける時に設定する cmd.ProcessState があるかどうかで判断するのが良いのかなという感じです。
お手数なのですが、これ試してもらってもいいでしょうか。
diff --git a/proc_posix.go b/proc_posix.go
index ce7a4fa..3e462b4 100644
--- a/proc_posix.go
+++ b/proc_posix.go
@@ -19,19 +19,13 @@ func (g *goemon) terminate() error {
if g.cmd != nil && g.cmd.Process != nil {
if err := g.cmd.Process.Signal(os.Interrupt); err != nil {
g.Logger.Println(err)
- } else {
- cd := 5
- for cd > 0 {
- if g.cmd.ProcessState != nil {
- break
- }
- time.Sleep(time.Second)
- cd--
- }
+ return g.cmd.Process.Kill()
}
- if g.cmd.ProcessState == nil {
+ t := time.AfterFunc(5*time.Second, func() {
g.cmd.Process.Kill()
- }
+ })
+ defer t.Stop()
+ return g.cmd.Wait()
}
return nil
}
問題なく動いてる気がします。 signal 直後で sleep を一回するのもなくなっているようです。
やたー! thx
日本語で失礼します
起きてること
goemon.terminate() を呼ぶと必ず 5 秒間待ってしまう(windows では確認出来ていません)
cmd.Process.Signal
を送り cmd が終了した場合ProcessState.Exited()
は false を返すこことが原因のようです。下のログはどちらも起動直後に ctrl-c を送ったものです。
before
after
何か勘違いしてたらすいません!
環境