From 5da5bf16286164704f721fdf345b8712e4afaf51 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Wed, 4 Dec 2019 13:04:25 +0800 Subject: [PATCH] fix p2p connection race condition --- client/local.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/client/local.go b/client/local.go index a66f9c0..9c903f6 100644 --- a/client/local.go +++ b/client/local.go @@ -1,8 +1,10 @@ package client import ( + "errors" "net" "net/http" + "runtime" "sync" "time" @@ -31,6 +33,14 @@ type p2pBridge struct { } func (p2pBridge *p2pBridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error) { + for i := 0; muxSession == nil; i++ { + if i >= 20 { + err = errors.New("p2pBridge:too many times to get muxSession") + logs.Error(err) + return + } + runtime.Gosched() // waiting for another goroutine establish the mux connection + } nowConn, err := muxSession.NewConn() if err != nil { udpConn = nil