Skip to content

goroutine leak at cache.go:1079 - runJanitor #166

Description

@tigerinus
package main

import (
	"testing"
	"time"

	"github.com/patrickmn/go-cache"
	"go.uber.org/goleak"
)

func leaktest(t *testing.T) {
	defer goleak.VerifyNone(t)
	cache.New(5*time.Minute, 10*time.Minute)
}

func main() {
	testSuite := []testing.InternalTest{{Name: "leaktest", F: leaktest}}
	testing.Main(func(a, b string) (bool, error) { return a == b, nil }, testSuite, nil, nil)
}

Run above code (https://go.dev/play/p/PxN4Q4-F2rG)

Result:

        [Goroutine 6 in state select, with github.com/patrickmn/go-cache.(*janitor).Run on top of the stack:
        goroutine 6 [select]:
        github.com/patrickmn/go-cache.(*janitor).Run(0xc0002f16d0, 0x0?)
        	/home/wxh/go/pkg/mod/github.com/patrickmn/go-cache@v2.1.0+incompatible/cache.go:1079 +0x85
        created by github.com/patrickmn/go-cache.runJanitor
        	/home/wxh/go/pkg/mod/github.com/patrickmn/go-cache@v2.1.0+incompatible/cache.go:1099 +0xed
        ]

It looks like https://github.com/patrickmn/go-cache/blob/v2.1.0/cache.go#L1076-L1087 does not handle exit condition properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions