diff --git a/lnd.go b/lnd.go index 8da903e96..cb2d8790c 100644 --- a/lnd.go +++ b/lnd.go @@ -180,14 +180,42 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg, network, ) + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + // Enable http profiling server if requested. if cfg.Profile != "" { + // Create the http handler. + profileRedirect := http.RedirectHandler( + "/debug/pprof", http.StatusSeeOther, + ) + http.Handle("/", profileRedirect) + ltndLog.Infof("Pprof listening on %v", cfg.Profile) + + // Create the pprof server. + pprofServer := &http.Server{ + Addr: cfg.Profile, + Handler: profileRedirect, + ReadHeaderTimeout: 5 * time.Second, + } + + // Shut the server down when lnd is shutting down. + defer func() { + ltndLog.Info("Stopping pprof server...") + err := pprofServer.Shutdown(ctx) + if err != nil { + ltndLog.Errorf("Stop pprof server got err: %v", + err) + } + }() + + // Start the pprof server. go func() { - profileRedirect := http.RedirectHandler("/debug/pprof", - http.StatusSeeOther) - http.Handle("/", profileRedirect) - ltndLog.Infof("Pprof listening on %v", cfg.Profile) - fmt.Println(http.ListenAndServe(cfg.Profile, nil)) + err := pprofServer.ListenAndServe() + if err != nil && !errors.Is(err, http.ErrServerClosed) { + ltndLog.Errorf("Serving pprof got err: %v", err) + } }() } @@ -202,10 +230,6 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg, defer pprof.StopCPUProfile() } - ctx := context.Background() - ctx, cancel := context.WithCancel(ctx) - defer cancel() - // Run configuration dependent DB pre-initialization. Note that this // needs to be done early and once during the startup process, before // any DB access.