all: fix more resource leaks found by staticmajor

Updates #5706

Signed-off-by: Emmanuel T Odeke <emmanuel@orijtech.com>
This commit is contained in:
Emmanuel T Odeke
2022-09-27 00:37:27 +02:00
committed by Brad Fitzpatrick
parent 614a24763b
commit 680f8d9793
7 changed files with 45 additions and 3 deletions

View File

@@ -88,6 +88,8 @@ func runMonitor(ctx context.Context, loop bool) error {
if err != nil {
return err
}
defer mon.Close()
mon.RegisterChangeCallback(func(changed bool, st *interfaces.State) {
if !changed {
log.Printf("Link monitor fired; no change")
@@ -162,7 +164,7 @@ func getURL(ctx context.Context, urlStr string) error {
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)
if err != nil {
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)
c2 := derphttp.NewRegionClient(priv2, log.Printf, getRegion)
defer func() {
if err != nil {
c1.Close()
c2.Close()
}
}()
c2.NotePreferred(true) // just to open it