Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak #1644

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions common/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/livepeer/go-livepeer/net"
"go.uber.org/goleak"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
Expand Down Expand Up @@ -76,3 +77,22 @@ func (s *StubServerStream) RecvMsg(m interface{}) error {
func (s *StubServerStream) Send(n *net.NotifySegment) error {
return nil
}

// IgnoreRoutines goroutines to ignore in tests
func IgnoreRoutines() []goleak.Option {
// goleak works by making list of all running goroutines and reporting error if it finds any
// this list tells goleak to ignore these goroutines - we're not interested in these particular goroutines
funcs2ignore := []string{"github.com/golang/glog.(*loggingT).flushDaemon", "go.opencensus.io/stats/view.(*worker).start",
"github.com/rjeczalik/notify.(*recursiveTree).dispatch", "github.com/rjeczalik/notify._Cfunc_CFRunLoopRun", "github.com/ethereum/go-ethereum/metrics.(*meterArbiter).tick",
"github.com/ethereum/go-ethereum/consensus/ethash.(*Ethash).remote", "github.com/ethereum/go-ethereum/core.(*txSenderCacher).cache",
"internal/poll.runtime_pollWait", "github.com/livepeer/go-livepeer/core.(*RemoteTranscoderManager).Manage", "github.com/livepeer/lpms/core.(*LPMS).Start",
"github.com/livepeer/go-livepeer/server.(*LivepeerServer).StartMediaServer", "github.com/livepeer/go-livepeer/core.(*RemoteTranscoderManager).Manage.func1",
"github.com/livepeer/go-livepeer/server.(*LivepeerServer).HandlePush.func1", "github.com/rjeczalik/notify.(*nonrecursiveTree).dispatch",
"github.com/rjeczalik/notify.(*nonrecursiveTree).internal", "github.com/livepeer/lpms/stream.NewBasicRTMPVideoStream.func1"}

res := make([]goleak.Option, 0, len(funcs2ignore))
for _, f := range funcs2ignore {
res = append(res, goleak.IgnoreTopFunction(f))
}
return res
}
9 changes: 4 additions & 5 deletions discovery/db_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (dbo *DBOrchestratorPoolCache) cacheOrchestratorStake() error {
return fmt.Errorf("could not retrieve orchestrators from DB: %v", err)
}

resc, errc := make(chan *common.DBOrch), make(chan error)
resc, errc := make(chan *common.DBOrch, len(orchs)), make(chan error, len(orchs))
ctx, cancel := context.WithTimeout(context.Background(), getOrchestratorsTimeoutLoop)
defer cancel()

Expand Down Expand Up @@ -201,7 +201,7 @@ func (dbo *DBOrchestratorPoolCache) cacheOrchestratorStake() error {
glog.Errorln(err)
case <-ctx.Done():
glog.Info("Done fetching stake for orchestrators, context timeout")
break
return nil
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func (dbo *DBOrchestratorPoolCache) cacheDBOrchs() error {
return fmt.Errorf("could not retrieve orchestrators from DB: %v", err)
}

resc, errc := make(chan *common.DBOrch), make(chan error)
resc, errc := make(chan *common.DBOrch, len(orchs)), make(chan error, len(orchs))
yondonfu marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), getOrchestratorsTimeoutLoop)
defer cancel()

Expand Down Expand Up @@ -270,7 +270,6 @@ func (dbo *DBOrchestratorPoolCache) cacheDBOrchs() error {
}
numOrchs++
go getOrchInfo(orch)

}

for i := 0; i < numOrchs; i++ {
Expand All @@ -283,7 +282,7 @@ func (dbo *DBOrchestratorPoolCache) cacheDBOrchs() error {
glog.Errorln(err)
case <-ctx.Done():
glog.Info("Done fetching orch info for orchestrators, context timeout")
break
return nil
}
}

Expand Down
11 changes: 8 additions & 3 deletions discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slick name. this makes me want to do go.livepeer.org/whatever for purely cosmetic reasons

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea 😃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we totally should, looks like it's as easy as https://github.com/GoogleCloudPlatform/govanityurls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we need package to publish 😃

)

func TestNewDBOrchestratorPoolCache_NilEthClient_ReturnsError(t *testing.T) {
Expand Down Expand Up @@ -144,8 +145,6 @@ func TestPoolSize(t *testing.T) {
func TestDBOrchestratorPoolCacheSize(t *testing.T) {
assert := assert.New(t)
dbh, dbraw, err := common.TempDB(t)
defer dbh.Close()
defer dbraw.Close()
require := require.New(t)
require.Nil(err)

Expand All @@ -156,7 +155,12 @@ func TestDBOrchestratorPoolCacheSize(t *testing.T) {
Sender: sender,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
defer func() {
cancel()
dbh.Close()
dbraw.Close()
goleak.VerifyNone(t, common.IgnoreRoutines()...)
}()

emptyPool, err := NewDBOrchestratorPoolCache(ctx, node, &stubRoundsManager{})
require.NoError(err)
Expand Down Expand Up @@ -1330,6 +1334,7 @@ func TestOrchestratorPool_ShuffleGetOrchestrators(t *testing.T) {
}

func TestOrchestratorPool_GetOrchestratorTimeout(t *testing.T) {
defer goleak.VerifyNone(t, common.IgnoreRoutines()...)
assert := assert.New(t)

addresses := stringsToURIs([]string{"https://127.0.0.1:8936", "https://127.0.0.1:8937", "https://127.0.0.1:8938"})
Expand Down
23 changes: 3 additions & 20 deletions server/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/core"
"github.com/livepeer/go-livepeer/drivers"
"github.com/livepeer/go-livepeer/net"
Expand Down Expand Up @@ -548,7 +549,7 @@ func TestPush_SetVideoProfileFormats(t *testing.T) {
}

func TestPush_ShouldRemoveSessionAfterTimeoutIfInternalMIDIsUsed(t *testing.T) {
defer goleak.VerifyNone(t, ignoreRoutines()...)
defer goleak.VerifyNone(t, common.IgnoreRoutines()...)

oldRI := httpPushTimeout
httpPushTimeout = 2 * time.Millisecond
Expand Down Expand Up @@ -593,26 +594,8 @@ func TestPush_ShouldRemoveSessionAfterTimeoutIfInternalMIDIsUsed(t *testing.T) {
assert.False(extEx)
}

func ignoreRoutines() []goleak.Option {
// goleak works by making list of all running goroutines and reporting error if it finds any
// this list tells goleak to ignore these goroutines - we're not interested in these particular goroutines
funcs2ignore := []string{"github.com/golang/glog.(*loggingT).flushDaemon", "go.opencensus.io/stats/view.(*worker).start",
"github.com/rjeczalik/notify.(*recursiveTree).dispatch", "github.com/rjeczalik/notify._Cfunc_CFRunLoopRun", "github.com/ethereum/go-ethereum/metrics.(*meterArbiter).tick",
"github.com/ethereum/go-ethereum/consensus/ethash.(*Ethash).remote", "github.com/ethereum/go-ethereum/core.(*txSenderCacher).cache",
"internal/poll.runtime_pollWait", "github.com/livepeer/go-livepeer/core.(*RemoteTranscoderManager).Manage", "github.com/livepeer/lpms/core.(*LPMS).Start",
"github.com/livepeer/go-livepeer/server.(*LivepeerServer).StartMediaServer", "github.com/livepeer/go-livepeer/core.(*RemoteTranscoderManager).Manage.func1",
"github.com/livepeer/go-livepeer/server.(*LivepeerServer).HandlePush.func1", "github.com/rjeczalik/notify.(*nonrecursiveTree).dispatch",
"github.com/rjeczalik/notify.(*nonrecursiveTree).internal", "github.com/livepeer/lpms/stream.NewBasicRTMPVideoStream.func1"}

res := make([]goleak.Option, 0, len(funcs2ignore))
for _, f := range funcs2ignore {
res = append(res, goleak.IgnoreTopFunction(f))
}
return res
}

func TestPush_ShouldRemoveSessionAfterTimeout(t *testing.T) {
defer goleak.VerifyNone(t, ignoreRoutines()...)
defer goleak.VerifyNone(t, common.IgnoreRoutines()...)

oldRI := httpPushTimeout
httpPushTimeout = 2 * time.Millisecond
Expand Down