Fix race with closing event channel

There's still a tiny race on Linux, since the tun channel is written to
from two places.
This commit is contained in:
Jason A. Donenfeld 2018-05-20 06:38:39 +02:00
parent 058cedcf66
commit 18f43705ec
2 changed files with 6 additions and 3 deletions

View file

@ -53,6 +53,8 @@ func (tun *NativeTun) RoutineRouteListener(tunIfindex int) {
statusMTU int statusMTU int
) )
defer close(tun.events)
data := make([]byte, os.Getpagesize()) data := make([]byte, os.Getpagesize())
for { for {
n, err := unix.Read(tun.routeSocket, data) n, err := unix.Read(tun.routeSocket, data)
@ -302,7 +304,6 @@ func (tun *NativeTun) Close() error {
// 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)
} }
close(tun.events)
if err1 != nil { if err1 != nil {
return err1 return err1
} }

View file

@ -89,7 +89,10 @@ func createNetlinkSocket() (int, error) {
} }
func (tun *NativeTun) RoutineNetlinkListener() { func (tun *NativeTun) RoutineNetlinkListener() {
defer unix.Close(tun.netlinkSock) defer func() {
unix.Close(tun.netlinkSock)
close(tun.events)
}()
for msg := make([]byte, 1<<16); ; { for msg := make([]byte, 1<<16); ; {
@ -357,7 +360,6 @@ func (tun *NativeTun) Close() error {
} }
err2 := tun.fd.Close() err2 := tun.fd.Close()
err3 := tun.fdCancel.Cancel() err3 := tun.fdCancel.Cancel()
close(tun.events)
if err1 != nil { if err1 != nil {
return err1 return err1