@@ -26,6 +26,7 @@ import (
2626 "path/filepath"
2727 "runtime"
2828 "runtime/debug"
29+ "sync"
2930
3031 "github.com/caddyserver/certmagic"
3132 parser "github.com/foxcpp/maddy/framework/cfgparser"
@@ -206,6 +207,8 @@ func Run(c *cli.Context) error {
206207 defer log .DefaultLogger .Out .Close ()
207208 defer hooks .RunHooks (hooks .EventShutdown )
208209
210+ hooks .AddHook (hooks .EventShutdown , netresource .CloseAllListeners )
211+
209212 if err := moduleMain (c .Path ("config" )); err != nil {
210213 systemdStatusErr (err )
211214 return cli .Exit (err .Error (), 1 )
@@ -395,24 +398,29 @@ func moduleMain(configPath string) error {
395398 }
396399 c .DefaultLogger .Msg ("server started" , "version" , Version )
397400
398- systemdStatus (SDReady , "Listening for incoming connections..." )
401+ systemdStatus (SDReady , "Configuration running." )
402+ asyncStopWg := sync.WaitGroup {} // Some containers might still be waiting on moduleStop
399403 for handleSignals () {
400- systemdStatus (SDReloading , "Reloading state..." )
401404 hooks .RunHooks (hooks .EventReload )
402405
403- c = moduleReload (c , configPath )
406+ c = moduleReload (c , configPath , & asyncStopWg )
404407 }
405408
406409 c .DefaultLogger .Msg ("server stopping..." )
407- systemdStatus (SDStopping , "Waiting for running transactions to complete..." )
408410
411+ systemdStatus (SDStopping , "Waiting for old configuration to stop..." )
412+ asyncStopWg .Wait ()
413+
414+ systemdStatus (SDStopping , "Waiting for current configuration to stop..." )
409415 moduleStop (c )
410416 c .DefaultLogger .Msg ("server stopped" )
417+
411418 return nil
412419}
413420
414- func moduleReload (oldContainer * container.C , configPath string ) * container.C {
421+ func moduleReload (oldContainer * container.C , configPath string , asyncStopWg * sync. WaitGroup ) * container.C {
415422 oldContainer .DefaultLogger .Msg ("reloading server..." )
423+ systemdStatus (SDReloading , "Reloading server..." )
416424
417425 oldContainer .DefaultLogger .Msg ("loading new configuration..." )
418426 newContainer , err := moduleConfigure (configPath )
@@ -430,12 +438,22 @@ func moduleReload(oldContainer *container.C, configPath string) *container.C {
430438 container .Global = oldContainer
431439 return oldContainer
432440 }
433- netresource .CloseUnusedListeners ()
434441
435442 newContainer .DefaultLogger .Msg ("server started" , "version" , Version )
436- oldContainer .DefaultLogger .Msg ("stopping server" )
437- moduleStop (oldContainer )
438- oldContainer .DefaultLogger .Msg ("server stopped" )
443+
444+ systemdStatus (SDReloading , "New configuration running. Waiting for old connections and transactions to finish..." )
445+
446+ asyncStopWg .Add (1 )
447+ go func () {
448+ defer asyncStopWg .Done ()
449+ defer netresource .CloseUnusedListeners ()
450+
451+ oldContainer .DefaultLogger .Msg ("stopping old server" )
452+ moduleStop (oldContainer )
453+ oldContainer .DefaultLogger .Msg ("old server stopped" )
454+
455+ systemdStatus (SDReloading , "Configuration running." )
456+ }()
439457
440458 return newContainer
441459}
0 commit comments