From 6f08a100410fb61093df5321b685b07ff8b801e6 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Tue, 9 Feb 2021 20:18:21 +0100
Subject: [PATCH] rwcancel: add an explicit close call

This lets us collect FDs even if the GC doesn't do it for us.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 device/sticky_linux.go | 1 +
 ipc/uapi_linux.go      | 1 +
 rwcancel/rwcancel.go   | 5 +++++
 tun/tun_linux.go       | 1 +
 4 files changed, 8 insertions(+)

diff --git a/device/sticky_linux.go b/device/sticky_linux.go
index 594cd01..a984f24 100644
--- a/device/sticky_linux.go
+++ b/device/sticky_linux.go
@@ -49,6 +49,7 @@ func (device *Device) routineRouteListener(bind conn.Bind, netlinkSock int, netl
 	var reqPeer map[uint32]peerEndpointPtr
 	var reqPeerLock sync.Mutex
 
+	defer netlinkCancel.Close()
 	defer unix.Close(netlinkSock)
 
 	for msg := make([]byte, 1<<16); ; {
diff --git a/ipc/uapi_linux.go b/ipc/uapi_linux.go
index 3213543..e03a00b 100644
--- a/ipc/uapi_linux.go
+++ b/ipc/uapi_linux.go
@@ -99,6 +99,7 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
 	go func(l *UAPIListener) {
 		var buff [0]byte
 		for {
+			defer uapi.inotifyRWCancel.Close()
 			// start with lstat to avoid race condition
 			if _, err := os.Lstat(socketPath); os.IsNotExist(err) {
 				l.connErr <- err
diff --git a/rwcancel/rwcancel.go b/rwcancel/rwcancel.go
index 8a6300a..70eb4ca 100644
--- a/rwcancel/rwcancel.go
+++ b/rwcancel/rwcancel.go
@@ -119,3 +119,8 @@ func (rw *RWCancel) Cancel() (err error) {
 	_, err = rw.closingWriter.Write([]byte{0})
 	return
 }
+
+func (rw *RWCancel) Close() {
+	rw.closingReader.Close()
+	rw.closingWriter.Close()
+}
diff --git a/tun/tun_linux.go b/tun/tun_linux.go
index be36c69..501f3a3 100644
--- a/tun/tun_linux.go
+++ b/tun/tun_linux.go
@@ -106,6 +106,7 @@ func (tun *NativeTun) routineNetlinkListener() {
 		unix.Close(tun.netlinkSock)
 		tun.hackListenerClosed.Lock()
 		close(tun.events)
+		tun.netlinkCancel.Close()
 	}()
 
 	for msg := make([]byte, 1<<16); ; {