Skip to content

Commit

Permalink
Fix send on closed channel in Client
Browse files Browse the repository at this point in the history
Protect concurrent run of
trMap.Find, trMap.CloseAndDeleteAll, and trMap.Delete.
  • Loading branch information
at-wat authored and Sean-Der committed Jan 30, 2020
1 parent 50d4c3d commit 3713a18
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Client struct {
listenTryLock client.TryLock // thread-safe
net *vnet.Net // read-only
mutex sync.RWMutex // thread-safe
mutexTrMap sync.Mutex // thread-safe
log logging.LeveledLogger // read-only
}

Expand Down Expand Up @@ -189,6 +190,9 @@ func (c *Client) Listen() error {

// Close closes this client
func (c *Client) Close() {
c.mutexTrMap.Lock()
defer c.mutexTrMap.Unlock()

c.trMap.CloseAndDeleteAll()
}

Expand Down Expand Up @@ -462,8 +466,11 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
// - stun.ClassErrorResponse

trKey := b64.StdEncoding.EncodeToString(msg.TransactionID[:])

c.mutexTrMap.Lock()
tr, ok := c.trMap.Find(trKey)
if !ok {
c.mutexTrMap.Unlock()
// silently discard
c.log.Debugf("no transaction for %s", msg.String())
return nil
Expand All @@ -472,6 +479,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
// End the transaction
tr.StopRtxTimer()
c.trMap.Delete(trKey)
c.mutexTrMap.Unlock()

if !tr.WriteResult(client.TransactionResult{
Msg: msg,
Expand Down Expand Up @@ -511,6 +519,9 @@ func (c *Client) handleChannelData(data []byte) error {
}

func (c *Client) onRtxTimeout(trKey string, nRtx int) {
c.mutexTrMap.Lock()
defer c.mutexTrMap.Unlock()

tr, ok := c.trMap.Find(trKey)
if !ok {
return // already gone
Expand Down

0 comments on commit 3713a18

Please # to comment.