@@ -63,8 +63,9 @@ func Run(config config.Config) error {
6363 if config .Snapshotter .Metrics .Enable {
6464 sdHost := config .Snapshotter .Metrics .Host
6565 sdPort := config .Snapshotter .Metrics .ServiceDiscoveryPort
66- serviceDiscovery := discovery .NewServiceDiscovery (sdHost , sdPort , cache )
67- monitor , err := initMetricsProxyMonitor (config .Snapshotter .Metrics .PortRange )
66+ serviceDiscovery = discovery .NewServiceDiscovery (sdHost , sdPort , cache )
67+ var err error
68+ monitor , err = initMetricsProxyMonitor (config .Snapshotter .Metrics .PortRange )
6869 if err != nil {
6970 log .G (ctx ).WithError (err ).Fatal ("failed creating metrics proxy monitor" )
7071 return err
@@ -113,19 +114,20 @@ func Run(config config.Config) error {
113114 if err := snapshotter .Close (); err != nil {
114115 log .G (ctx ).WithError (err ).Error ("failed to close snapshotter" )
115116 }
116- if config .Snapshotter .Metrics .Enable {
117- if err := serviceDiscovery .Shutdown (ctx ); err != nil {
118- log .G (ctx ).WithError (err ).Error ("failed to shutdown service discovery server" )
119- }
120- // Senders to this channel would panic if it is closed. However snapshotter.Close() will
121- // shutdown all metrics proxies and ensure there are no more senders over the channel.
122- monitor .Stop ()
123- }
124117 }()
125118
126119 for {
127120 select {
128121 case <- stop :
122+ // cancelling context will cause shutdown to fail; shutdown before cancel
123+ if config .Snapshotter .Metrics .Enable {
124+ if err := serviceDiscovery .Shutdown (ctx ); err != nil {
125+ log .G (ctx ).WithError (err ).Error ("failed to shutdown service discovery server" )
126+ }
127+ // Senders to this channel would panic if it is closed. However snapshotter.Close() will
128+ // shutdown all metrics proxies and ensure there are no more senders over the channel.
129+ monitor .Stop ()
130+ }
129131 cancel ()
130132 return nil
131133 case <- ctx .Done ():
0 commit comments