Fix a stuck mutex

This commit is contained in:
Qian Wang 2019-08-07 13:59:27 +01:00
parent ae4fc917b6
commit e7aa4cd04b
2 changed files with 3 additions and 11 deletions

View File

@ -58,10 +58,10 @@ func (sb *switchboard) removeConn(connId uint32) {
// a pointer to connId is passed here so that the switchboard can reassign it // a pointer to connId is passed here so that the switchboard can reassign it
func (sb *switchboard) send(data []byte, connId *uint32) (int, error) { func (sb *switchboard) send(data []byte, connId *uint32) (int, error) {
var conn net.Conn
sb.connsM.RLock() sb.connsM.RLock()
defer sb.connsM.RUnlock()
var conn net.Conn
conn, ok := sb.conns[*connId] conn, ok := sb.conns[*connId]
sb.connsM.RUnlock()
if ok { if ok {
return conn.Write(data) return conn.Write(data)
} else { } else {
@ -72,13 +72,11 @@ func (sb *switchboard) send(data []byte, connId *uint32) (int, error) {
// in particular if newConnId is removed between the RUnlock and RLock, conns[newConnId] will return // in particular if newConnId is removed between the RUnlock and RLock, conns[newConnId] will return
// a nil pointer. To prevent this we must get newConnId and the reference to conn itself in one single mutex // a nil pointer. To prevent this we must get newConnId and the reference to conn itself in one single mutex
// protection // protection
sb.connsM.RLock()
if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 { if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 {
return 0, errBrokenSwitchboard return 0, errBrokenSwitchboard
} }
newConnId := rand.Intn(len(sb.conns)) newConnId := rand.Intn(len(sb.conns))
conn = sb.conns[uint32(newConnId)] conn = sb.conns[uint32(newConnId)]
sb.connsM.RUnlock()
return conn.Write(data) return conn.Write(data)
} }
@ -86,11 +84,11 @@ func (sb *switchboard) send(data []byte, connId *uint32) (int, error) {
func (sb *switchboard) assignRandomConn() (uint32, error) { func (sb *switchboard) assignRandomConn() (uint32, error) {
sb.connsM.RLock() sb.connsM.RLock()
defer sb.connsM.RUnlock()
if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 { if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 {
return 0, errBrokenSwitchboard return 0, errBrokenSwitchboard
} }
connId := rand.Intn(len(sb.conns)) connId := rand.Intn(len(sb.conns))
sb.connsM.RUnlock()
return uint32(connId), nil return uint32(connId), nil
} }

View File

@ -161,12 +161,6 @@ func parseClientHello(data []byte) (ret *ClientHello, err error) {
return return
} }
func xor(a []byte, b []byte) {
for i := range a {
a[i] ^= b[i]
}
}
func composeServerHello(sessionId []byte, sharedSecret []byte, sessionKey []byte) ([]byte, error) { func composeServerHello(sessionId []byte, sharedSecret []byte, sessionKey []byte) ([]byte, error) {
nonce := make([]byte, 12) nonce := make([]byte, 12)
rand.Read(nonce) rand.Read(nonce)