all: fix more resource leaks found by staticmajor

Updates #5706

Signed-off-by: Emmanuel T Odeke <emmanuel@orijtech.com>
pull/5093/head
Emmanuel T Odeke 2 years ago committed by Brad Fitzpatrick
parent 614a24763b
commit 680f8d9793

@ -88,6 +88,8 @@ func runMonitor(ctx context.Context, loop bool) error {
if err != nil { if err != nil {
return err return err
} }
defer mon.Close()
mon.RegisterChangeCallback(func(changed bool, st *interfaces.State) { mon.RegisterChangeCallback(func(changed bool, st *interfaces.State) {
if !changed { if !changed {
log.Printf("Link monitor fired; no change") log.Printf("Link monitor fired; no change")
@ -162,7 +164,7 @@ func getURL(ctx context.Context, urlStr string) error {
return res.Write(os.Stdout) return res.Write(os.Stdout)
} }
func checkDerp(ctx context.Context, derpRegion string) error { func checkDerp(ctx context.Context, derpRegion string) (err error) {
req, err := http.NewRequestWithContext(ctx, "GET", ipn.DefaultControlURL+"/derpmap/default", nil) req, err := http.NewRequestWithContext(ctx, "GET", ipn.DefaultControlURL+"/derpmap/default", nil)
if err != nil { if err != nil {
return fmt.Errorf("create derp map request: %w", err) return fmt.Errorf("create derp map request: %w", err)
@ -201,6 +203,12 @@ func checkDerp(ctx context.Context, derpRegion string) error {
c1 := derphttp.NewRegionClient(priv1, log.Printf, getRegion) c1 := derphttp.NewRegionClient(priv1, log.Printf, getRegion)
c2 := derphttp.NewRegionClient(priv2, log.Printf, getRegion) c2 := derphttp.NewRegionClient(priv2, log.Printf, getRegion)
defer func() {
if err != nil {
c1.Close()
c2.Close()
}
}()
c2.NotePreferred(true) // just to open it c2.NotePreferred(true) // just to open it

@ -564,6 +564,8 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, dialer *tsdial.Dialer, na
} }
d, err := dns.NewOSConfigurator(logf, devName) d, err := dns.NewOSConfigurator(logf, devName)
if err != nil { if err != nil {
dev.Close()
r.Close()
return nil, false, fmt.Errorf("dns.NewOSConfigurator: %w", err) return nil, false, fmt.Errorf("dns.NewOSConfigurator: %w", err)
} }
conf.DNS = d conf.DNS = d

@ -187,7 +187,12 @@ func buildWasm(dev bool) ([]byte, error) {
return nil, fmt.Errorf("Cannot create main.wasm output file: %w", err) return nil, fmt.Errorf("Cannot create main.wasm output file: %w", err)
} }
outputPath := outputFile.Name() outputPath := outputFile.Name()
defer os.Remove(outputPath) defer os.Remove(outputPath)
// Running defer (*os.File).Close() in defer order before os.Remove
// because on some systems like Windows, it is possible for os.Remove
// to fail for unclosed files.
defer outputFile.Close()
args := []string{"build", "-tags", "tailscale_go,osusergo,netgo,nethttpomithttp2,omitidna,omitpemdecrypt"} args := []string{"build", "-tags", "tailscale_go,osusergo,netgo,nethttpomithttp2,omitidna,omitpemdecrypt"}
if !dev { if !dev {

@ -88,11 +88,17 @@ func New(opts Options) (*Auto, error) {
} }
// NewNoStart creates a new Auto, but without calling Start on it. // NewNoStart creates a new Auto, but without calling Start on it.
func NewNoStart(opts Options) (*Auto, error) { func NewNoStart(opts Options) (_ *Auto, err error) {
direct, err := NewDirect(opts) direct, err := NewDirect(opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() {
if err != nil {
direct.Close()
}
}()
if opts.Status == nil { if opts.Status == nil {
return nil, errors.New("missing required Options.Status") return nil, errors.New("missing required Options.Status")
} }

@ -1258,7 +1258,7 @@ func randBytes(n int) []byte {
// //
// It writes an asciinema file to // It writes an asciinema file to
// $TAILSCALE_VAR_ROOT/ssh-sessions/ssh-session-<unixtime>-*.cast. // $TAILSCALE_VAR_ROOT/ssh-sessions/ssh-session-<unixtime>-*.cast.
func (ss *sshSession) startNewRecording() (*recording, error) { func (ss *sshSession) startNewRecording() (_ *recording, err error) {
var w ssh.Window var w ssh.Window
if ptyReq, _, isPtyReq := ss.Pty(); isPtyReq { if ptyReq, _, isPtyReq := ss.Pty(); isPtyReq {
w = ptyReq.Window w = ptyReq.Window
@ -1282,6 +1282,12 @@ func (ss *sshSession) startNewRecording() (*recording, error) {
if err := os.MkdirAll(dir, 0700); err != nil { if err := os.MkdirAll(dir, 0700); err != nil {
return nil, err return nil, err
} }
defer func() {
if err != nil {
rec.Close()
}
}()
f, err := os.CreateTemp(dir, fmt.Sprintf("ssh-session-%v-*.cast", now.UnixNano())) f, err := os.CreateTemp(dir, fmt.Sprintf("ssh-session-%v-*.cast", now.UnixNano()))
if err != nil { if err != nil {
return nil, err return nil, err

@ -25,10 +25,13 @@ var (
func main() { func main() {
flag.Parse() flag.Parse()
s := new(tsnet.Server) s := new(tsnet.Server)
defer s.Close()
ln, err := s.Listen("tcp", *addr) ln, err := s.Listen("tcp", *addr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer ln.Close()
if *addr == ":443" { if *addr == ":443" {
ln = tls.NewListener(ln, &tls.Config{ ln = tls.NewListener(ln, &tls.Config{
GetCertificate: tailscale.GetCertificate, GetCertificate: tailscale.GetCertificate,

@ -16,6 +16,7 @@ import (
"net/netip" "net/netip"
"os" "os"
"strconv" "strconv"
"sync"
"time" "time"
"tailscale.com/types/logger" "tailscale.com/types/logger"
@ -323,6 +324,13 @@ func setupBatchTCPTest(logf logger.Logf, traf *TrafficGen) {
log.Fatalf("listen: %v", err) log.Fatalf("listen: %v", err)
} }
var slCloseOnce sync.Once
slClose := func() {
slCloseOnce.Do(func() {
sl.Close()
})
}
s1, err := net.Dial("tcp", sl.Addr().String()) s1, err := net.Dial("tcp", sl.Addr().String())
if err != nil { if err != nil {
log.Fatalf("dial: %v", err) log.Fatalf("dial: %v", err)
@ -340,6 +348,8 @@ func setupBatchTCPTest(logf logger.Logf, traf *TrafficGen) {
go func() { go func() {
// transmitter // transmitter
defer slClose()
defer s1.Close()
bs1 := bufio.NewWriterSize(s1, 1024*1024) bs1 := bufio.NewWriterSize(s1, 1024*1024)
@ -375,6 +385,8 @@ func setupBatchTCPTest(logf logger.Logf, traf *TrafficGen) {
go func() { go func() {
// receiver // receiver
defer slClose()
defer s2.Close()
bs2 := bufio.NewReaderSize(s2, 1024*1024) bs2 := bufio.NewReaderSize(s2, 1024*1024)

Loading…
Cancel
Save