Close events channel when no status listener

This commit is contained in:
Jason A. Donenfeld 2018-05-21 14:16:46 +02:00
parent 7472930d4e
commit 837a12c841
2 changed files with 10 additions and 3 deletions

View File

@ -303,6 +303,9 @@ func (tun *NativeTun) Close() error {
// Surprisingly, on Darwin, simply closing a route socket is enough to unblock it. // Surprisingly, on Darwin, simply closing a route socket is enough to unblock it.
// We don't even need to call shutdown, or use a rwcancel. // We don't even need to call shutdown, or use a rwcancel.
err3 = unix.Close(tun.routeSocket) err3 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
} }
if err1 != nil { if err1 != nil {
return err1 return err1

View File

@ -357,10 +357,14 @@ func (tun *NativeTun) Events() chan TUNEvent {
func (tun *NativeTun) Close() error { func (tun *NativeTun) Close() error {
var err1 error var err1 error
if tun.statusListenersShutdown != nil {
close(tun.statusListenersShutdown) close(tun.statusListenersShutdown)
if tun.netlinkCancel != nil { if tun.netlinkCancel != nil {
err1 = tun.netlinkCancel.Cancel() err1 = tun.netlinkCancel.Cancel()
} }
} else if tun.events != nil {
close(tun.events)
}
err2 := tun.fd.Close() err2 := tun.fd.Close()
err3 := tun.fdCancel.Cancel() err3 := tun.fdCancel.Cancel()