Skip to content

Commit

Permalink
Make close methods of skynet structs thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Oct 14, 2019
1 parent 3f117ef commit 951e1f1
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions pkg/app2/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync"
"sync/atomic"

"github.com/pkg/errors"

"github.com/skycoin/dmsg/netutil"
"github.com/skycoin/skycoin/src/util/logging"

Expand Down Expand Up @@ -144,18 +146,27 @@ type skywireListener struct {
connsCh chan net.Conn
freePort func()
freePortMx sync.RWMutex
once sync.Once
}

// Accept accepts incoming connection.
func (l *skywireListener) Accept() (net.Conn, error) {
return <-l.connsCh, nil
conn, ok := <-l.connsCh
if !ok {
return nil, errors.New("listening on closed connection")
}

return conn, nil
}

// Close closes listener.
func (l *skywireListener) Close() error {
l.freePortMx.RLock()
defer l.freePortMx.RUnlock()
l.freePort()
l.once.Do(func() {
l.freePortMx.RLock()
defer l.freePortMx.RUnlock()
l.freePort()
close(l.connsCh)
})

return nil
}
Expand All @@ -177,17 +188,23 @@ type skywireConn struct {
net.Conn
freePort func()
freePortMx sync.RWMutex
once sync.Once
}

// Close closes connection.
func (c *skywireConn) Close() error {
defer func() {
c.freePortMx.RLock()
defer c.freePortMx.RUnlock()
if c.freePort != nil {
c.freePort()
}
}()
var err error
c.once.Do(func() {
defer func() {
c.freePortMx.RLock()
defer c.freePortMx.RUnlock()
if c.freePort != nil {
c.freePort()
}
}()

err = c.Conn.Close()
})

return c.Conn.Close()
return err
}

0 comments on commit 951e1f1

Please sign in to comment.