Skip to content

Commit

Permalink
[vchanmanager] Move lock inside vchan
Browse files Browse the repository at this point in the history
We should lock only creating and closing vchan API. Currently we lock also
tls handshake as result open channel was blocked till secure channel is created.

Signed-off-by: Oleksandr Grytsov <[email protected]>
  • Loading branch information
al1img committed Feb 15, 2024
1 parent d518c9d commit 4029726
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 9 additions & 0 deletions vchanmanager/vchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"crypto/tls"
"errors"
"net"
"sync"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -111,6 +112,8 @@ var (
errWriteVChan = errors.New("write failed")
)

var lock sync.Mutex //nolint: gochecknoglobals

/***********************************************************************************************************************
* Public
**********************************************************************************************************************/
Expand Down Expand Up @@ -265,6 +268,9 @@ func (vc *connection) Write(buffer []byte) (int, error) {
}

func (vc *connection) Close() error {
lock.Lock()
defer lock.Unlock()

if vc.vchanReader != nil {
log.WithFields(log.Fields{"name": vc.name}).Debug("Close read vchan")

Expand Down Expand Up @@ -311,6 +317,9 @@ func (v *VChan) newConnection(name string) (conn *connection, err error) {
}
}()

lock.Lock()
defer lock.Unlock()

log.WithFields(log.Fields{"rxPath": v.xsRxPath, "txPath": v.xsTxPath}).Debug("New connection")

conn = &connection{name: name}
Expand Down
8 changes: 0 additions & 8 deletions vchanmanager/vchanmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"os"
"reflect"
"sync"
"time"

"github.com/aoscloud/aos_common/aoserrors"
Expand Down Expand Up @@ -86,8 +85,6 @@ type Message struct {

// Vchan vchan instance.
type VChanManager struct {
sync.Mutex

vchanOpen VChanItf
vchanSecure VChanItf
recvSMChan chan []byte
Expand Down Expand Up @@ -292,10 +289,7 @@ func (v *VChanManager) GetAllNodeIDs(context context.Context,

func (v *VChanManager) run(ctx context.Context, name string, vchan VChanItf, sendChan chan Message) {
for {
v.Lock()
err := vchan.Connect(ctx, name)
v.Unlock()

if err == nil {
errCh := make(chan error, 2) //nolint:gomnd // 2 is enough
localCtx, cancel := context.WithCancel(ctx)
Expand All @@ -312,11 +306,9 @@ func (v *VChanManager) run(ctx context.Context, name string, vchan VChanItf, sen
case err := <-errCh:
log.Errorf("Failed to read/write from/to vchan: %v", err)

v.Lock()
if err = vchan.Disconnect(); err != nil {
log.Errorf("Failed to disconnect from vchan: %v", err)
}
v.Unlock()

cancel()
}
Expand Down

0 comments on commit 4029726

Please sign in to comment.