Skip to content

Commit d2bc5d8

Browse files
fix(cmd/gf): run AddSigHandlerShutdown cannot work well (gogf#4441)
gogf#3752 Sending [Interrupt] on Windows is not implemented. --------- Co-authored-by: hailaz <739476267@qq.com>
1 parent 0648fd6 commit d2bc5d8

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

cmd/gf/internal/cmd/cmd_run.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"runtime"
1515
"strings"
16+
"time"
1617

1718
"github.com/gogf/gf/v2/container/gtype"
1819
"github.com/gogf/gf/v2/frame/g"
@@ -207,8 +208,37 @@ func (app *cRunApp) End(ctx context.Context, sig os.Signal, outputPath string) {
207208
// Delete the binary file.
208209
// firstly, kill the process.
209210
if process != nil {
210-
if err := process.Kill(); err != nil {
211-
mlog.Debugf("kill process error: %s", err.Error())
211+
if sig != nil && runtime.GOOS != "windows" {
212+
if err := process.Signal(sig); err != nil {
213+
mlog.Debugf("send signal to process error: %s", err.Error())
214+
if err := process.Kill(); err != nil {
215+
mlog.Debugf("kill process error: %s", err.Error())
216+
}
217+
} else {
218+
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
219+
defer cancel()
220+
done := make(chan error, 1)
221+
go func() {
222+
select {
223+
case <-waitCtx.Done():
224+
done <- waitCtx.Err()
225+
case done <- process.Wait():
226+
}
227+
}()
228+
err := <-done
229+
if err != nil {
230+
mlog.Debugf("process wait error: %s", err.Error())
231+
if err := process.Kill(); err != nil {
232+
mlog.Debugf("kill process error: %s", err.Error())
233+
}
234+
} else {
235+
mlog.Debug("process exited gracefully")
236+
}
237+
}
238+
} else {
239+
if err := process.Kill(); err != nil {
240+
mlog.Debugf("kill process error: %s", err.Error())
241+
}
212242
}
213243
}
214244
if err := gfile.RemoveFile(outputPath); err != nil {

0 commit comments

Comments
 (0)