-
Notifications
You must be signed in to change notification settings - Fork 36
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
Fixed recvfd leaks #1173
Fixed recvfd leaks #1173
Changes from 1 commit
c7b4986
5d58f01
1895c74
9f4f9ab
6f613aa
a21604d
51703c4
0c19391
55d55c8
a178bfe
007843e
b2faeee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,12 +21,10 @@ package recvfd_test | |||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"net" | ||||||
"net/url" | ||||||
"os" | ||||||
"path" | ||||||
"runtime" | ||||||
"syscall" | ||||||
"testing" | ||||||
"time" | ||||||
|
||||||
|
@@ -36,7 +34,7 @@ import ( | |||||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls" | ||||||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/common" | ||||||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" | ||||||
"github.com/stretchr/testify/assert" | ||||||
"github.com/networkservicemesh/sdk/pkg/tools/grpcfdutils" | ||||||
"github.com/stretchr/testify/require" | ||||||
"go.uber.org/goleak" | ||||||
"google.golang.org/grpc" | ||||||
|
@@ -58,13 +56,6 @@ type checkRecvfdServer struct { | |||||
t *testing.T | ||||||
} | ||||||
|
||||||
type notifiableFDTransceiver struct { | ||||||
grpcfd.FDTransceiver | ||||||
net.Addr | ||||||
|
||||||
onRecvFile map[string]func() | ||||||
} | ||||||
|
||||||
func (n *checkRecvfdServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { | ||||||
return next.Server(ctx).Close(ctx, conn) | ||||||
} | ||||||
|
@@ -76,52 +67,28 @@ func (n *checkRecvfdServer) Request(ctx context.Context, request *networkservice | |||||
transceiver, ok := p.Addr.(grpcfd.FDTransceiver) | ||||||
require.True(n.t, ok) | ||||||
|
||||||
p.Addr = ¬ifiableFDTransceiver{ | ||||||
p.Addr = &grpcfdutils.NotifiableFDTransceiver{ | ||||||
FDTransceiver: transceiver, | ||||||
Addr: p.Addr, | ||||||
onRecvFile: n.onRecvFile, | ||||||
OnRecvFile: n.onRecvFile, | ||||||
} | ||||||
|
||||||
return next.Server(ctx).Request(ctx, request) | ||||||
} | ||||||
|
||||||
func (w *notifiableFDTransceiver) RecvFileByURL(urlStr string) (<-chan *os.File, error) { | ||||||
recv, err := w.FDTransceiver.RecvFileByURL(urlStr) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
var fileCh = make(chan *os.File) | ||||||
go func() { | ||||||
for f := range recv { | ||||||
runtime.SetFinalizer(f, func(file *os.File) { | ||||||
onFileClosedFunc, ok := w.onRecvFile[urlStr] | ||||||
if ok { | ||||||
onFileClosedFunc() | ||||||
} | ||||||
}) | ||||||
fileCh <- f | ||||||
} | ||||||
}() | ||||||
|
||||||
return fileCh, nil | ||||||
} | ||||||
|
||||||
func createFile(fileName string, t *testing.T) (inodeURLStr string, fileClosedContext context.Context, cancelFunc func()) { | ||||||
f, err := os.Create(fileName) | ||||||
require.NoError(t, err, "Failed to create and open a file: %v", err) | ||||||
|
||||||
info, err := f.Stat() | ||||||
assert.NoError(t, err) | ||||||
|
||||||
stat, ok := info.Sys().(*syscall.Stat_t) | ||||||
assert.True(t, ok) | ||||||
|
||||||
err = f.Close() | ||||||
require.NoError(t, err, "Failed to close file: %v", err) | ||||||
|
||||||
fileClosedContext, cancelFunc = context.WithCancel(context.Background()) | ||||||
inodeURLStr = fmt.Sprintf("inode://%d/%d", stat.Dev, stat.Ino) | ||||||
|
||||||
inodeURL, err := grpcfd.FilenameToURL(fileName) | ||||||
require.NoError(t, err) | ||||||
|
||||||
inodeURLStr = inodeURL.String() | ||||||
|
||||||
return | ||||||
} | ||||||
|
@@ -150,7 +117,7 @@ func createClient(ctx context.Context, u *url.URL) networkservice.NetworkService | |||||
func TestRecvfdClosesSingleFile(t *testing.T) { | ||||||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||||||
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Time limit for unit test is 1s.
Suggested change
|
||||||
defer cancel() | ||||||
|
||||||
var dir = t.TempDir() | ||||||
|
@@ -203,7 +170,7 @@ func TestRecvfdClosesSingleFile(t *testing.T) { | |||||
func TestRecvfdClosesMultipleFiles(t *testing.T) { | ||||||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||||||
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||||||
defer cancel() | ||||||
|
||||||
var dir = t.TempDir() | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||||||
// | ||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||
// | ||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
// you may not use this file except in compliance with the License. | ||||||
// You may obtain a copy of the License at: | ||||||
// | ||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||
// | ||||||
// Unless required by applicable law or agreed to in writing, software | ||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
// See the License for the specific language governing permissions and | ||||||
// limitations under the License. | ||||||
|
||||||
package grpcfdutils | ||||||
|
||||||
import ( | ||||||
"net" | ||||||
"os" | ||||||
"runtime" | ||||||
|
||||||
"github.com/edwarnicke/grpcfd" | ||||||
) | ||||||
|
||||||
type NotifiableFDTransceiver struct { | ||||||
grpcfd.FDTransceiver | ||||||
net.Addr | ||||||
|
||||||
OnRecvFile map[string]func() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
func (w *NotifiableFDTransceiver) RecvFileByURL(urlStr string) (<-chan *os.File, error) { | ||||||
recv, err := w.FDTransceiver.RecvFileByURL(urlStr) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
var fileCh = make(chan *os.File) | ||||||
go func() { | ||||||
for f := range recv { | ||||||
runtime.SetFinalizer(f, func(file *os.File) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please set finalizer in |
||||||
onFileClosedFunc, ok := w.OnRecvFile[urlStr] | ||||||
if ok { | ||||||
onFileClosedFunc() | ||||||
} | ||||||
}) | ||||||
fileCh <- f | ||||||
} | ||||||
}() | ||||||
|
||||||
return fileCh, nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use anonymous returns. I wonder why linter is not alerting on this line...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply this to other places.