Skip to content
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

panic.go 里 checkPanic 遇到 panic 直接退出进程是否合理 #137

Closed
PegasusWang opened this issue Aug 16, 2019 · 0 comments
Closed

Comments

@PegasusWang
Copy link

PegasusWang commented Aug 16, 2019

// panic.go
package tars

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime/debug"
	"time"
)

func checkPanic() {
	if r := recover(); r != nil {
		path, _ := filepath.Abs(filepath.Dir(os.Args[0]))
		os.Chdir(path)
		file, _ := os.Create(fmt.Sprintf("panic.%s", time.Now().Format("20060102-150405")))
		file.WriteString(string(debug.Stack()))
		file.Close()
		os.Exit(-1)   // 这里直接退出进程合理么?
	}
}

请教一个问题。有一个疑问,如果作为 server 的代码不慎出现了 panic,这里会检查之后直接退出进程,会导致后续的请求无法处理,这样是否是合理的呢?内置的 http server 遇到panic会recover不会退出进程,这里为何直接退出了呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant