Skip to content

Commit bd2fd11

Browse files
committed
Skip closing an initializing connection
Without this change, if a cert is updated (e.g. to add CNs) while the listener is in the middle of Accept()ing a new connection, the connection gets dropped, we'll see a message like this in the server logs: http: TLS handshake error from 127.0.0.1:51232: write tcp 127.0.7.1:8443->127.0.0.1:51232: use of closed network connection and the client (like a browser) won't necessarily reconnect. This change modifies the GetCertificate routine in the listener's tls.Config to check the connection descriptor in the client hello and skip closing it, so that only old established connections are closed.
1 parent 94e2249 commit bd2fd11

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

listener.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,14 @@ func (c *closeWrapper) Close() error {
291291
}
292292

293293
func (l *listener) getCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
294+
newConn := hello.Conn
294295
if hello.ServerName != "" {
295296
if err := l.updateCert(hello.ServerName); err != nil {
296297
return nil, err
297298
}
298299
}
299300

300-
return l.loadCert()
301+
return l.loadCert(newConn)
301302
}
302303

303304
func (l *listener) updateCert(cn ...string) error {
@@ -339,7 +340,7 @@ func (l *listener) updateCert(cn ...string) error {
339340
return nil
340341
}
341342

342-
func (l *listener) loadCert() (*tls.Certificate, error) {
343+
func (l *listener) loadCert(currentConn net.Conn) (*tls.Certificate, error) {
343344
l.RLock()
344345
defer l.RUnlock()
345346

@@ -373,6 +374,10 @@ func (l *listener) loadCert() (*tls.Certificate, error) {
373374
if l.conns != nil && l.cert != nil {
374375
l.connLock.Lock()
375376
for _, conn := range l.conns {
377+
// Don't close a connection that's in the middle of completing a TLS handshake
378+
if conn == currentConn {
379+
continue
380+
}
376381
_ = conn.close()
377382
}
378383
l.connLock.Unlock()

0 commit comments

Comments
 (0)