Fix "panic: send on closed channel"
This commit is contained in:
+10
-2
@@ -3,7 +3,6 @@ package core
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,6 +72,7 @@ type Sender struct {
|
|||||||
|
|
||||||
buf chan *Packet
|
buf chan *Packet
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSender(media *Media, codec *Codec) *Sender {
|
func NewSender(media *Media, codec *Codec) *Sender {
|
||||||
@@ -99,6 +99,11 @@ func NewSender(media *Media, codec *Codec) *Sender {
|
|||||||
s.Input = func(packet *Packet) {
|
s.Input = func(packet *Packet) {
|
||||||
// writing to nil chan - OK, writing to closed chan - panic
|
// writing to nil chan - OK, writing to closed chan - panic
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
|
if s.isClosed {
|
||||||
|
s.Drops++
|
||||||
|
s.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case s.buf <- packet:
|
case s.buf <- packet:
|
||||||
s.Bytes += len(packet.Payload)
|
s.Bytes += len(packet.Payload)
|
||||||
@@ -165,10 +170,13 @@ func (s *Sender) State() string {
|
|||||||
|
|
||||||
func (s *Sender) Close() {
|
func (s *Sender) Close() {
|
||||||
// close buffer if exists
|
// close buffer if exists
|
||||||
if buf := s.buf; buf != nil {
|
s.mu.Lock()
|
||||||
|
if buf := s.buf; buf != nil && !s.isClosed {
|
||||||
|
s.isClosed = true
|
||||||
s.buf = nil
|
s.buf = nil
|
||||||
defer close(buf)
|
defer close(buf)
|
||||||
}
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
s.Node.Close()
|
s.Node.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user