Skip to content

Commit 8a5961d

Browse files
committed
[FAB-12084] address simple lostcancel vet issues
Also enable lostcancel checks on vet for all but the two remaining issues. Change-Id: I9cd94c28638e7c7db826ac0aee6e13953bdd8be3 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent a6a07ce commit 8a5961d

File tree

11 files changed

+44
-43
lines changed

11 files changed

+44
-43
lines changed

common/crypto/tlsgen/ca_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func TestTLSCA(t *testing.T) {
6666
}
6767
tlsCfg.RootCAs.AppendCertsFromPEM(ca.CertBytes())
6868
tlsOpts := grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))
69-
ctx := context.Background()
70-
ctx, _ = context.WithTimeout(ctx, time.Second)
69+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
70+
defer cancel()
7171
conn, err := grpc.DialContext(ctx, fmt.Sprintf("127.0.0.1:%d", randomPort), tlsOpts, grpc.WithBlock())
7272
if err != nil {
7373
return err

core/chaincode/accesscontrol/access_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func newClient(t *testing.T, port int, cert *tls.Certificate, peerCACert []byte)
112112
tlsCfg.Certificates = []tls.Certificate{*cert}
113113
}
114114
tlsOpts := grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))
115-
ctx := context.Background()
116-
ctx, _ = context.WithTimeout(ctx, time.Second)
115+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
116+
defer cancel()
117117
conn, err := grpc.DialContext(ctx, fmt.Sprintf("localhost:%d", port), tlsOpts, grpc.WithBlock())
118118
if err != nil {
119119
return nil, err

core/comm/connection.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@ func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled b
205205
if block {
206206
opts = append(opts, grpc.WithBlock())
207207
}
208-
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize),
209-
grpc.MaxCallSendMsgSize(MaxSendMsgSize)))
210-
ctx := context.Background()
211-
ctx, _ = context.WithTimeout(ctx, defaultTimeout)
208+
opts = append(opts, grpc.WithDefaultCallOptions(
209+
grpc.MaxCallRecvMsgSize(MaxRecvMsgSize),
210+
grpc.MaxCallSendMsgSize(MaxSendMsgSize),
211+
))
212+
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
213+
defer cancel()
212214
conn, err := grpc.DialContext(ctx, peerAddress, opts...)
213215
if err != nil {
214216
return nil, err

core/comm/connection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ func testInvoke(
354354
creds, err := cs.GetDeliverServiceCredentials(channelID)
355355
assert.NoError(t, err)
356356
endpoint := fmt.Sprintf("localhost:%d", s.port)
357-
ctx := context.Background()
358-
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
357+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
358+
defer cancel()
359359
conn, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(creds), grpc.WithBlock())
360360
if shouldSucceed {
361361
assert.NoError(t, err)

core/comm/server_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ func (esss *emptyServiceServer) EmptyStream(stream testpb.EmptyService_EmptyStre
130130

131131
// invoke the EmptyCall RPC
132132
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
133-
ctx := context.Background()
134-
ctx, _ = context.WithTimeout(ctx, timeout)
133+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
134+
defer cancel()
135135
//create GRPC client conn
136136
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
137137
if err != nil {
@@ -142,12 +142,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
142142
//create GRPC client
143143
client := testpb.NewEmptyServiceClient(clientConn)
144144

145-
callCtx := context.Background()
146-
callCtx, cancel := context.WithTimeout(callCtx, timeout)
147-
defer cancel()
148-
149145
//invoke service
150-
empty, err := client.EmptyCall(callCtx, new(testpb.Empty))
146+
empty, err := client.EmptyCall(context.Background(), new(testpb.Empty))
151147
if err != nil {
152148
return nil, err
153149
}
@@ -157,8 +153,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
157153

158154
// invoke the EmptyStream RPC
159155
func invokeEmptyStream(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
160-
ctx := context.Background()
161-
ctx, _ = context.WithTimeout(ctx, timeout)
156+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
157+
defer cancel()
162158
//create GRPC client conn
163159
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
164160
if err != nil {
@@ -632,8 +628,7 @@ func TestNewGRPCServerFromListener(t *testing.T) {
632628
_, err = invokeEmptyCall(testAddress, dialOptions)
633629

634630
if err != nil {
635-
t.Fatalf("GRPC client failed to invoke the EmptyCall service on %s: %v",
636-
testAddress, err)
631+
t.Fatalf("GRPC client failed to invoke the EmptyCall service on %s: %v", testAddress, err)
637632
} else {
638633
t.Log("GRPC client successfully invoked the EmptyCall service: " + testAddress)
639634
}
@@ -1543,18 +1538,15 @@ func TestKeepaliveClientResponse(t *testing.T) {
15431538
defer srv.Stop()
15441539

15451540
// test that connection does not close with response to ping
1546-
connectCtx, cancel := context.WithDeadline(
1547-
context.Background(),
1548-
time.Now().Add(1*time.Second))
1541+
connectCtx, cancel := context.WithTimeout(context.Background(), time.Second)
15491542
clientTransport, err := transport.NewClientTransport(
15501543
connectCtx,
15511544
context.Background(),
15521545
transport.TargetInfo{Addr: testAddress},
15531546
transport.ConnectOptions{},
1554-
func() {})
1555-
if err != nil {
1556-
cancel()
1557-
}
1547+
func() {},
1548+
)
1549+
cancel()
15581550
assert.NoError(t, err, "Unexpected error creating client transport")
15591551
defer clientTransport.Close()
15601552
// sleep past keepalive timeout

core/deliverservice/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func (bc *broadcastClient) connect() error {
172172
if err != nil {
173173
logger.Error("Connection to ", endpoint, "established but was unable to create gRPC stream:", err)
174174
conn.Close()
175+
cf()
175176
return err
176177
}
177178
err = bc.afterConnect(conn, abc, cf, endpoint)

core/deliverservice/deliveryclient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ func DefaultConnectionFactory(channelID string) func(endpoint string) (*grpc.Cli
259259
} else {
260260
dialOpts = append(dialOpts, grpc.WithInsecure())
261261
}
262-
ctx := context.Background()
263-
ctx, _ = context.WithTimeout(ctx, getConnectionTimeout())
262+
ctx, cancel := context.WithTimeout(context.Background(), getConnectionTimeout())
263+
defer cancel()
264264
return grpc.DialContext(ctx, endpoint, dialOpts...)
265265
}
266266
}

core/peer/pkg_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {
6161
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
6262
//add DialOptions
6363
dialOptions = append(dialOptions, grpc.WithBlock())
64-
ctx := context.Background()
65-
ctx, _ = context.WithTimeout(ctx, timeout)
64+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
65+
defer cancel()
6666
//create GRPC client conn
6767
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
6868
if err != nil {
@@ -73,12 +73,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
7373
//create GRPC client
7474
client := testpb.NewTestServiceClient(clientConn)
7575

76-
callCtx := context.Background()
77-
callCtx, cancel := context.WithTimeout(callCtx, timeout)
78-
defer cancel()
79-
8076
//invoke service
81-
empty, err := client.EmptyCall(callCtx, new(testpb.Empty))
77+
empty, err := client.EmptyCall(context.Background(), new(testpb.Empty))
8278
if err != nil {
8379
return nil, err
8480
}

gossip/comm/comm_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ func handshaker(endpoint string, comm Comm, t *testing.T, connMutator msgMutator
143143
secureOpts = grpc.WithInsecure()
144144
}
145145
acceptChan := comm.Accept(acceptAll)
146-
ctx := context.Background()
147-
ctx, _ = context.WithTimeout(ctx, time.Second)
146+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
147+
defer cancel()
148148
conn, err := grpc.DialContext(ctx, "localhost:9611", secureOpts, grpc.WithBlock())
149149
assert.NoError(t, err, "%v", err)
150150
if err != nil {
@@ -530,8 +530,8 @@ func TestCloseConn(t *testing.T) {
530530
}
531531
ta := credentials.NewTLS(tlsCfg)
532532

533-
ctx := context.Background()
534-
ctx, _ = context.WithTimeout(ctx, time.Second)
533+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
534+
defer cancel()
535535
conn, err := grpc.DialContext(ctx, "localhost:1611", grpc.WithTransportCredentials(ta), grpc.WithBlock())
536536
assert.NoError(t, err, "%v", err)
537537
cl := proto.NewGossipClient(conn)

gossip/comm/crypto_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func TestCertificateExtraction(t *testing.T) {
8383
Certificates: []tls.Certificate{clientCert},
8484
InsecureSkipVerify: true,
8585
})
86-
ctx := context.Background()
87-
ctx, _ = context.WithTimeout(ctx, time.Second)
86+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
87+
defer cancel()
8888
conn, err := grpc.DialContext(ctx, "localhost:5611", grpc.WithTransportCredentials(ta), grpc.WithBlock())
8989
assert.NoError(t, err, "%v", err)
9090

0 commit comments

Comments
 (0)