Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 22:15 UTC
http: clean up listeners if some of the listeners fail to bind (#7176)
* http: clean up listeners if some of the listeners fail to bind * check for nil server due to failure to start --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
WeidiDeng committed
Aug 22, 2025 at 02:14 UTC
f11c780fdc2e4e5298a64ef88d110dd392060a36
1 file changed
+21
modules/caddyhttp/app.go
+21
@@ -151,6 +151,11 @@ type App struct {
151
logger *zap.Logger
152
tlsApp *caddytls.TLS
153
154
+ // stopped indicates whether the app has stopped
155
+ // It can only happen if it has started successfully in the first place.
156
+ // Otherwise, Cleanup will call Stop to clean up resources.
157
+ stopped bool
158
+
159
// used temporarily between phases 1 and 2 of auto HTTPS
160
allCertDomains map[string]struct{}
161
}
@@ -708,6 +713,11 @@ func (app *App) Stop() error {
713
defer finishedShutdown.Done()
714
startedShutdown.Done()
715
716
+ // possible if server failed to Start
717
+ if server.server == nil {
718
+ return
719
+ }
720
+
721
if err := server.server.Shutdown(ctx); err != nil {
722
app.logger.Error("server shutdown",
723
zap.Error(err),
@@ -791,9 +801,20 @@ func (app *App) Stop() error {
801
}
802
}
803
804
+ app.stopped = true
805
return nil
806
}
807
808
+// Cleanup will close remaining listeners if they still remain
809
+// because some of the servers fail to start.
810
+// It simply calls Stop because Stop won't be called when Start fails.
811
+func (app *App) Cleanup() error {
812
+ if app.stopped {
813
+ return nil
814
+ }
815
+ return app.Stop()
816
+}
817
+
818
func (app *App) httpPort() int {
819
if app.HTTPPort == 0 {
820
return DefaultHTTPPort