Handle Accept errors gracefully.

Originally when Accept fails we log the error and let the program flow
continue. This can lead to us spawning handling connection go routines
on nil connections which in turn leads to Go panics.
This commit is contained in:
Milos Gajdos 2019-10-15 15:03:47 +01:00
parent fdfeb437f9
commit fed5af68e6
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

View File

@ -269,14 +269,21 @@ func (n *network) acceptNetConn(l tunnel.Listener, recv chan *transport.Message)
for { for {
// accept a connection // accept a connection
conn, err := l.Accept() conn, err := l.Accept()
if err != nil {
log.Debugf("Network tunnel [%s] accept error: %v", NetworkChannel, err)
}
select { select {
case <-n.closed: case <-n.closed:
// only try to close the connection if it has been successfully opened
if err != nil {
if closeErr := conn.Close(); closeErr != nil {
log.Debugf("Network tunnel [%s] failed to close connection: %v", NetworkChannel, closeErr)
}
}
return return
default: default:
if err != nil {
log.Debugf("Network tunnel [%s] accept error: %v", NetworkChannel, err)
continue
}
// go handle NetworkChannel connection // go handle NetworkChannel connection
go n.handleNetConn(conn, recv) go n.handleNetConn(conn, recv)
} }
@ -558,14 +565,21 @@ func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *transport.Message
for { for {
// accept a connection // accept a connection
conn, err := l.Accept() conn, err := l.Accept()
if err != nil {
log.Debugf("Network tunnel [%s] accept error: %v", ControlChannel, err)
}
select { select {
case <-n.closed: case <-n.closed:
// only try to close the connection if it has been successfully opened
if err != nil {
if closeErr := conn.Close(); closeErr != nil {
log.Debugf("Network tunnel [%s] failed to close connection: %v", ControlChannel, closeErr)
}
}
return return
default: default:
if err != nil {
log.Debugf("Network tunnel [%s] accept error: %v", ControlChannel, err)
continue
}
// go handle ControlChannel connection // go handle ControlChannel connection
go n.handleCtrlConn(conn, recv) go n.handleCtrlConn(conn, recv)
} }