Kaynağa Gözat

tsnet,wgenegine/netstack: add test and fix resource leaks

We were not closing the http.Server and were also not waiting for
netstack to fully close.

Signed-off-by: Maisem Ali <[email protected]>
Maisem Ali 3 yıl önce
ebeveyn
işleme
920ec69241
3 değiştirilmiş dosya ile 14 ekleme ve 1 silme
  1. 10 1
      tsnet/tsnet.go
  2. 3 0
      tsnet/tsnet_test.go
  3. 1 0
      wgengine/netstack/netstack.go

+ 10 - 1
tsnet/tsnet.go

@@ -117,6 +117,7 @@ type Server struct {
 	loopbackListener net.Listener           // optional loopback for localapi and proxies
 	localAPIListener net.Listener           // in-memory, used by localClient
 	localClient      *tailscale.LocalClient // in-memory
+	localAPIServer   *http.Server
 	logbuffer        *filch.Filch
 	logtail          *logtail.Logger
 	logid            logid.PublicID
@@ -329,6 +330,13 @@ func (s *Server) Close() error {
 			s.logbuffer.Close()
 		}
 	}()
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		if s.localAPIServer != nil {
+			s.localAPIServer.Shutdown(ctx)
+		}
+	}()
 
 	if _, isMemStore := s.Store.(*mem.Store); isMemStore && s.Ephemeral && s.lb != nil {
 		wg.Add(1)
@@ -566,8 +574,9 @@ func (s *Server) start() (reterr error) {
 	lal := memnet.Listen("local-tailscaled.sock:80")
 	s.localAPIListener = lal
 	s.localClient = &tailscale.LocalClient{Dial: lal.Dial}
+	s.localAPIServer = &http.Server{Handler: lah}
 	go func() {
-		if err := http.Serve(lal, lah); err != nil {
+		if err := s.localAPIServer.Serve(lal); err != nil {
 			logf("localapi serve error: %v", err)
 		}
 	}()

+ 3 - 0
tsnet/tsnet_test.go

@@ -34,6 +34,7 @@ import (
 	"tailscale.com/ipn/store/mem"
 	"tailscale.com/net/netns"
 	"tailscale.com/tailcfg"
+	"tailscale.com/tstest"
 	"tailscale.com/tstest/integration"
 	"tailscale.com/tstest/integration/testcontrol"
 	"tailscale.com/types/logger"
@@ -223,6 +224,7 @@ func startServer(t *testing.T, ctx context.Context, controlURL, hostname string)
 }
 
 func TestConn(t *testing.T) {
+	tstest.ResourceCheck(t)
 	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
 	defer cancel()
 
@@ -280,6 +282,7 @@ func TestConn(t *testing.T) {
 }
 
 func TestLoopbackLocalAPI(t *testing.T) {
+	tstest.ResourceCheck(t)
 	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
 	defer cancel()
 

+ 1 - 0
wgengine/netstack/netstack.go

@@ -222,6 +222,7 @@ func Create(logf logger.Logf, tundev *tstun.Wrapper, e wgengine.Engine, mc *magi
 func (ns *Impl) Close() error {
 	ns.ctxCancel()
 	ns.ipstack.Close()
+	ns.ipstack.Wait()
 	return nil
 }