Skip to content

Commit

Permalink
Merge pull request #9 from pojntfx/feature/fix-close-race-conditions
Browse files Browse the repository at this point in the history
Check if line channel is closed before sending to it
  • Loading branch information
pojntfx authored Aug 17, 2022
2 parents 464a913 + 3b4bdf8 commit e5eedbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Hydrunfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [ "$1" = "go" ]; then
make depend

# Build
CGO_ENABLED=0 bagop -j "$(nproc)" -b "$2" -x '(android/*|ios/*|plan9/*|aix/*)' -p "make build/$2 DST=\$DST" -d out
CGO_ENABLED=0 bagop -j "$(nproc)" -b "$2" -x '(android/*|ios/*|plan9/*|aix/*|linux/loong64)' -p "make build/$2 DST=\$DST" -d out

exit 0
fi
Expand Down
28 changes: 20 additions & 8 deletions pkg/wrtcconn/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ type Adapter struct {
config *AdapterConfig
ctx context.Context

cancel context.CancelFunc
done bool
lines chan []byte
cancel context.CancelFunc
done bool
doneSync sync.Mutex
lines chan []byte

peers chan *Peer

Expand Down Expand Up @@ -96,6 +97,17 @@ func NewAdapter(
}
}

func (a *Adapter) sendLine(line []byte) {
a.doneSync.Lock()
defer a.doneSync.Unlock()

if a.done {
return
}

a.lines <- line
}

// Open connects the adapter to the signaler
func (a *Adapter) Open() (chan string, error) {
settingEngine := webrtc.SettingEngine{}
Expand Down Expand Up @@ -253,7 +265,7 @@ func (a *Adapter) Open() (chan string, error) {
return
}

a.lines <- p
a.sendLine(p)

log.Debug().Str("address", u.String()).Str("id", id).Msg("Introduced to signaler")
}()
Expand Down Expand Up @@ -376,7 +388,7 @@ func (a *Adapter) Open() (chan string, error) {
}

go func() {
a.lines <- p
a.sendLine(p)

log.Debug().
Str("address", conn.RemoteAddr().String()).
Expand Down Expand Up @@ -506,7 +518,7 @@ func (a *Adapter) Open() (chan string, error) {
peerLock.Unlock()

go func() {
a.lines <- p
a.sendLine(p)

log.Debug().
Str("address", conn.RemoteAddr().String()).
Expand Down Expand Up @@ -606,7 +618,7 @@ func (a *Adapter) Open() (chan string, error) {
}

go func() {
a.lines <- p
a.sendLine(p)

log.Debug().
Str("address", conn.RemoteAddr().String()).
Expand Down Expand Up @@ -726,7 +738,7 @@ func (a *Adapter) Open() (chan string, error) {
}()

go func() {
a.lines <- p
a.sendLine(p)

log.Debug().
Str("address", conn.RemoteAddr().String()).
Expand Down

0 comments on commit e5eedbb

Please sign in to comment.