Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent ICETransport start/stop deadlock #2897

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on the line but it looks like you're Locking the t.lock above and never unlocking; is that on purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah there's a defer unlock above. not sure how i feel about that pattern!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it haha, but since it was pre-existing and I have basically no context here will approve.

}

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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at these closes and they should be safe to call concurrently. If they aren't, we'll see some other problems crop up quickly. But I haven't found those to happen in any concurrent PC close test runs.

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
Loading