tun: windows: cancel ongoing reads on closing and delete after close

This reverts commit 52ec440d79 and adds
some spice.
This commit is contained in:
Jason A. Donenfeld 2019-03-26 15:57:53 +01:00
parent 2fa80c0cb7
commit 2e0ed4614a

View file

@ -146,6 +146,7 @@ func (tun *NativeTun) closeTUN() (err error) {
} }
t := tun.tunFileRead t := tun.tunFileRead
tun.tunFileRead = nil tun.tunFileRead = nil
windows.CancelIoEx(windows.Handle(t.Fd()), nil)
err = t.Close() err = t.Close()
tun.tunLock.Unlock() tun.tunLock.Unlock()
break break
@ -158,6 +159,7 @@ func (tun *NativeTun) closeTUN() (err error) {
} }
t := tun.tunFileWrite t := tun.tunFileWrite
tun.tunFileWrite = nil tun.tunFileWrite = nil
windows.CancelIoEx(windows.Handle(t.Fd()), nil)
err2 := t.Close() err2 := t.Close()
tun.tunLock.Unlock() tun.tunLock.Unlock()
if err == nil { if err == nil {
@ -214,13 +216,18 @@ func (tun *NativeTun) Events() chan TUNEvent {
func (tun *NativeTun) Close() error { func (tun *NativeTun) Close() error {
tun.close = true tun.close = true
err1 := tun.closeTUN()
if tun.events != nil { if tun.events != nil {
close(tun.events) close(tun.events)
} }
/* We delete it first, before closing, so that the close operations don't hang with the concurrent read operation. */
_, _, err := tun.wt.DeleteInterface(0) _, _, err2 := tun.wt.DeleteInterface(0)
tun.closeTUN() if err1 == nil {
return err err1 = err2
}
return err1
} }
func (tun *NativeTun) MTU() (int, error) { func (tun *NativeTun) MTU() (int, error) {