1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +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
func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() {
mc := make(chan os.Signal, 2)
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 3)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
go func() {
for {
s := <-mc
switch s {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mc)
close(mainStopCh)
})
case syscall.SIGUSR1:
dumpStack()
}
}
}()
mainStopCh = make(chan struct{})
})
return mainStopCh
}

View File

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