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

main: Correct defer handling in main entrypoint. #351

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,8 @@ func genCertPair(certFile, keyFile string) error {
}

// newConfigParser returns a new command line flags parser.
func newConfigParser(cfg *config, options flags.Options) (*flags.Parser, error) {
parser := flags.NewParser(cfg, options)
return parser, nil
func newConfigParser(cfg *config, options flags.Options) *flags.Parser {
return flags.NewParser(cfg, options)
}

// cleanAndExpandPath expands environment variables and leading ~ in the
Expand Down Expand Up @@ -370,13 +369,8 @@ func loadConfig() (*config, []string, error) {
// help message error can be ignored here since they will be caught by
// the final parse below.
preCfg := cfg
preParser, err := newConfigParser(&preCfg, flags.HelpFlag)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

_, err = preParser.Parse()
preParser := newConfigParser(&preCfg, flags.HelpFlag)
_, err := preParser.Parse()
if err != nil {
var e *flags.Error
if errors.As(err, &e) {
Expand Down Expand Up @@ -491,12 +485,7 @@ func loadConfig() (*config, []string, error) {

// Load additional config from file.
var configFileError error
parser, err := newConfigParser(&cfg, flags.Default)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, nil, err
}

parser := newConfigParser(&cfg, flags.Default)
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
var e *os.PathError
Expand Down
18 changes: 14 additions & 4 deletions dcrpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ func newPool(db pool.Database, cfg *config) (*miningPool, error) {
return p, nil
}

func main() {
// realMain is the real main function for dcrpool. It is necessary to work
// around the fact that deferred functions do not run when os.Exit() is called.
func realMain() error {
// Listen for interrupt signals.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, signals...)
Expand All @@ -142,7 +144,7 @@ func main() {
// logging and configures it accordingly.
cfg, _, err := loadConfig()
if err != nil {
os.Exit(1)
return err
}
defer func() {
if logRotator != nil {
Expand All @@ -160,13 +162,13 @@ func main() {

if err != nil {
mpLog.Errorf("failed to initialize database: %v", err)
os.Exit(1)
return err
}

p, err := newPool(db, cfg)
if err != nil {
mpLog.Errorf("failed to initialize pool: %v", err)
os.Exit(1)
return err
}

if cfg.Profile != "" {
Expand Down Expand Up @@ -215,4 +217,12 @@ func main() {

db.Close()
mpLog.Info("dcrpool shut down.")
return nil
}

func main() {
// Work around defer not working after os.Exit()
if err := realMain(); err != nil {
os.Exit(1)
}
}
Loading