Skip to content

Commit

Permalink
fix: Ensure channels are only closed once (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 12, 2023
1 parent cbfdbcd commit ce26f51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ type Application struct {
messageMu sync.Mutex
// Relay messages received so users can add custom logic to
// events.
messageChan chan *pb.CastMessage
messageChan chan *pb.CastMessage
closeChanOnce sync.Once
// Functions that will receive messages from 'messageChan'
messageFuncs []CastMessageFunc

Expand Down Expand Up @@ -424,9 +425,9 @@ func (a *Application) Close(stopMedia bool) error {
a.sendMediaConn(&cast.CloseHeader)
a.sendDefaultConn(&cast.CloseHeader)
}
defer func() {
defer a.closeChanOnce.Do(func() {
close(a.messageChan)
}()
})
return a.conn.Close()
}

Expand Down
8 changes: 5 additions & 3 deletions cast/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net"
"sync"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -36,7 +37,8 @@ type Conn interface {
type Connection struct {
conn *tls.Conn

recvMsgChan chan *pb.CastMessage
recvMsgChan chan *pb.CastMessage
closeChanOnce sync.Once

debug bool
connected bool
Expand Down Expand Up @@ -74,9 +76,9 @@ func (c *Connection) Close() error {
if c.cancel != nil {
c.cancel()
}
defer func() {
defer c.closeChanOnce.Do(func() {
close(c.recvMsgChan)
}()
})
return c.conn.Close()
}

Expand Down

0 comments on commit ce26f51

Please sign in to comment.