Skip to content

Commit

Permalink
Prevent ICETransport start/stop deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniels committed Sep 4, 2024
1 parent 4ef00e6 commit 9a71f69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ var (
errInvalidICEServer = errors.New("invalid ICEServer")

errICETransportNotInNew = errors.New("ICETransport can only be called in ICETransportStateNew")
errICETransportClosed = errors.New("ICETransport closed")

errCertificatePEMFormatError = errors.New("bad Certificate PEM format")

Expand Down
23 changes: 15 additions & 8 deletions icetransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (t *ICETransport) Start(gatherer *ICEGatherer, params ICEParameters, role *
return err
}

if t.State() == ICETransportStateClosed {
return errICETransportClosed
}

t.conn = iceConn

config := mux.Config{
Expand Down Expand Up @@ -200,28 +204,31 @@ func (t *ICETransport) GracefulStop() error {

func (t *ICETransport) stop(shouldGracefullyClose bool) error {
t.lock.Lock()
defer t.lock.Unlock()

t.setState(ICETransportStateClosed)

if t.ctxCancel != nil {
t.ctxCancel()
}

// mux and gatherer can only be set when ICETransport.State != Closed.
mux := t.mux
gatherer := t.gatherer
t.lock.Unlock()

if t.mux != nil {
var closeErrs []error
if shouldGracefullyClose && t.gatherer != nil {
if shouldGracefullyClose && gatherer != nil {
// we can't access icegatherer/icetransport.Close via
// mux's net.Conn Close so we call it earlier here.
closeErrs = append(closeErrs, t.gatherer.GracefulClose())
closeErrs = append(closeErrs, gatherer.GracefulClose())
}
closeErrs = append(closeErrs, t.mux.Close())
closeErrs = append(closeErrs, mux.Close())
return util.FlattenErrs(closeErrs)
} else if t.gatherer != nil {
} else if gatherer != nil {
if shouldGracefullyClose {
return t.gatherer.GracefulClose()
return gatherer.GracefulClose()
}
return t.gatherer.Close()
return gatherer.Close()
}
return nil
}
Expand Down

0 comments on commit 9a71f69

Please sign in to comment.