From 837a12c84151968fac6477f75d6bcb52e7ae64db Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 21 May 2018 14:16:46 +0200 Subject: Close events channel when no status listener --- tun_darwin.go | 3 +++ tun_linux.go | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tun_darwin.go b/tun_darwin.go index d2cdcd5..b8afdfd 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -303,6 +303,9 @@ func (tun *NativeTun) Close() error { // 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. err3 = unix.Close(tun.routeSocket) + tun.routeSocket = -1 + } else if tun.events != nil { + close(tun.events) } if err1 != nil { return err1 diff --git a/tun_linux.go b/tun_linux.go index f5b24dc..629a5ec 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -357,9 +357,13 @@ func (tun *NativeTun) Events() chan TUNEvent { func (tun *NativeTun) Close() error { var err1 error - close(tun.statusListenersShutdown) - if tun.netlinkCancel != nil { - err1 = tun.netlinkCancel.Cancel() + if tun.statusListenersShutdown != nil { + close(tun.statusListenersShutdown) + if tun.netlinkCancel != nil { + err1 = tun.netlinkCancel.Cancel() + } + } else if tun.events != nil { + close(tun.events) } err2 := tun.fd.Close() err3 := tun.fdCancel.Cancel() -- cgit v1.2.3