Skip to content

Commit

Permalink
Revert "Add more mutexes in dial chain element to fix race conditions (
Browse files Browse the repository at this point in the history
…networkservicemesh#1670)"

This reverts commit b66e1bf.

Signed-off-by: NikitaSkrynnik <[email protected]>
  • Loading branch information
NikitaSkrynnik committed Oct 15, 2024
1 parent 6522b19 commit aef72c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions pkg/networkservice/common/dial/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
)

type dialer struct {
ctx context.Context
clientURL *url.URL
cleanupCancel context.CancelFunc
ctx context.Context
cleanupContext context.Context
clientURL *url.URL
cleanupCancel context.CancelFunc
*grpc.ClientConn
dialOptions []grpc.DialOption
dialTimeout time.Duration
Expand Down Expand Up @@ -65,53 +66,41 @@ func (di *dialer) Dial(ctx context.Context, clientURL *url.URL) error {
}

// Dial
di.mu.Lock()
target := grpcutils.URLToTarget(di.clientURL)
di.mu.Unlock()

cc, err := grpc.DialContext(dialCtx, target, di.dialOptions...)
if err != nil {
if cc != nil {
_ = cc.Close()
}
return errors.Wrapf(err, "failed to dial %s", target)
}
di.mu.Lock()
di.ClientConn = cc
var cleanupContext context.Context
cleanupContext, di.cleanupCancel = context.WithCancel(di.ctx)
di.mu.Unlock()

di.cleanupContext, di.cleanupCancel = context.WithCancel(di.ctx)

go func(cleanupContext context.Context, cc *grpc.ClientConn) {
<-cleanupContext.Done()
_ = cc.Close()
}(cleanupContext, cc)
}(di.cleanupContext, cc)
return nil
}

func (di *dialer) Close() error {
if di != nil && di.cleanupCancel != nil {
di.mu.Lock()
di.cleanupCancel()
di.mu.Unlock()
runtime.Gosched()
}
return nil
}

func (di *dialer) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...grpc.CallOption) error {
di.mu.Lock()
defer di.mu.Unlock()
if di.ClientConn == nil {
return errors.New("no dialer.ClientConn found")
}
return di.ClientConn.Invoke(ctx, method, args, reply, opts...)
}

func (di *dialer) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
di.mu.Lock()
defer di.mu.Unlock()

if di.ClientConn == nil {
return nil, errors.New("no dialer.ClientConn found")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/discoverforwarder/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023-2024 Cisco and/or its affiliates.
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down

0 comments on commit aef72c2

Please sign in to comment.