1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

fix wrong channel closed in SetupMainSignalHandler (#939)

This commit is contained in:
povsister 2021-06-29 19:43:20 +08:00 committed by GitHub
parent 70558dc965
commit a87d0e9f8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 22 deletions

View File

@ -12,24 +12,22 @@ import (
// SetupMainSignalHandler is for main to use at last // SetupMainSignalHandler is for main to use at last
func SetupMainSignalHandler() <-chan struct{} { func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() { mainOnce.Do(func() {
mc := make(chan os.Signal, 2) mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 3)
closeOnce := sync.Once{} closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1) signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
go func() { go func() {
for { for {
s := <-mc switch <-mc {
switch s {
case os.Interrupt, syscall.SIGTERM: case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() { closeOnce.Do(func() {
close(mc) close(mainStopCh)
}) })
case syscall.SIGUSR1: case syscall.SIGUSR1:
dumpStack() dumpStack()
} }
} }
}() }()
mainStopCh = make(chan struct{})
}) })
return mainStopCh return mainStopCh
} }

View File

@ -18,8 +18,8 @@ import (
) )
var ( var (
validTasks = []string{ validTasks = map[string]func(){
"dumpstack", "dumpstack": dumpStack,
} }
) )
@ -30,11 +30,11 @@ func SetupMainSignalHandler() <-chan struct{} {
pipeName := fmt.Sprintf(`\\.\pipe\go-cqhttp-%d`, os.Getpid()) pipeName := fmt.Sprintf(`\\.\pipe\go-cqhttp-%d`, os.Getpid())
pipe, err := winio.ListenPipe(pipeName, &winio.PipeConfig{}) pipe, err := winio.ListenPipe(pipeName, &winio.PipeConfig{})
if err != nil { if err != nil {
log.Error("创建 named pipe 失败. 将无法使用 dumpstack 功能") log.Errorf("创建 named pipe 失败. 将无法使用 dumpstack 功能: %v", err)
} else { } else {
maxTaskLen := 0 maxTaskLen := 0
for i := range validTasks { for t := range validTasks {
if l := len(validTasks[i]); l > maxTaskLen { if l := len(t); l > maxTaskLen {
maxTaskLen = l maxTaskLen = l
} }
} }
@ -58,27 +58,26 @@ func SetupMainSignalHandler() <-chan struct{} {
return return
} }
cmd := string(buf[:n]) cmd := string(buf[:n])
switch cmd { if task, ok := validTasks[cmd]; ok {
case "dumpstack": task()
dumpStack() return
default:
log.Warnf("named pipe 读取到未知指令: %q", cmd)
} }
log.Warnf("named pipe 读取到未知指令: %q", cmd)
}() }()
} }
}() }()
} }
// setup the main stop channel
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 2) mc := make(chan os.Signal, 2)
closeOnce := sync.Once{} closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM) signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
for { for {
s := <-mc switch <-mc {
switch s {
case os.Interrupt, syscall.SIGTERM: case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() { closeOnce.Do(func() {
close(mc) close(mainStopCh)
if pipe != nil { if pipe != nil {
_ = pipe.Close() _ = pipe.Close()
} }
@ -86,8 +85,6 @@ func SetupMainSignalHandler() <-chan struct{} {
} }
} }
}() }()
mainStopCh = make(chan struct{})
}) })
return mainStopCh return mainStopCh
} }