Skip to content

Commit 03b43ac

Browse files
committed
Trap close error on shutdown
Fixes containerd#1499 This traps any Accept() errors on server shutdown so that it exits cleanly. It also changes gracefulstop to stop on the grpc server as the daemon will not shutdown if there are connected clients. Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent 558b46f commit 03b43ac

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

server/server.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/pprof"
99
"os"
1010
"path/filepath"
11+
"strings"
1112

1213
"github.com/boltdb/bolt"
1314
containers "github.com/containerd/containerd/api/services/containers/v1"
@@ -129,14 +130,14 @@ func (s *Server) ServeGRPC(l net.Listener) error {
129130
// handler. This needs to be the last service registered so that it can collect
130131
// metrics for every other service
131132
grpc_prometheus.Register(s.rpc)
132-
return s.rpc.Serve(l)
133+
return trapClosedConnErr(s.rpc.Serve(l))
133134
}
134135

135136
// ServeMetrics provides a prometheus endpoint for exposing metrics
136137
func (s *Server) ServeMetrics(l net.Listener) error {
137138
m := http.NewServeMux()
138139
m.Handle("/metrics", metrics.Handler())
139-
return http.Serve(l, m)
140+
return trapClosedConnErr(http.Serve(l, m))
140141
}
141142

142143
// ServeDebug provides a debug endpoint
@@ -150,12 +151,12 @@ func (s *Server) ServeDebug(l net.Listener) error {
150151
m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
151152
m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
152153
m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
153-
return http.Serve(l, m)
154+
return trapClosedConnErr(http.Serve(l, m))
154155
}
155156

156-
// Stop gracefully stops the containerd server
157+
// Stop the containerd server canceling any open connections
157158
func (s *Server) Stop() {
158-
s.rpc.GracefulStop()
159+
s.rpc.Stop()
159160
}
160161

161162
func loadPlugins(config *Config) ([]*plugin.Registration, error) {
@@ -219,3 +220,13 @@ func interceptor(
219220
}
220221
return grpc_prometheus.UnaryServerInterceptor(ctx, req, info, handler)
221222
}
223+
224+
func trapClosedConnErr(err error) error {
225+
if err == nil {
226+
return nil
227+
}
228+
if strings.Contains(err.Error(), "use of closed network connection") {
229+
return nil
230+
}
231+
return err
232+
}

0 commit comments

Comments
 (0)