Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Ensure rtc conn is closed on error #438

Merged
merged 1 commit into from
Sep 10, 2021
Merged
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
13 changes: 11 additions & 2 deletions wsnet/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pion/datachannel"
"github.com/pion/webrtc/v3"
"golang.org/x/net/proxy"
"golang.org/x/xerrors"
"nhooyr.io/websocket"

"cdr.dev/slog"
Expand Down Expand Up @@ -112,6 +113,11 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
iceServers: rtc.GetConfiguration().ICEServers,
rtc: rtc.ConnectionState(),
}

closeErr := rtc.Close()
if closeErr != nil {
log.Warn(context.Background(), "close rtc connection on dial failure", slog.Error(closeErr))
}
}
}()

Expand Down Expand Up @@ -170,9 +176,12 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
connClosers: []io.Closer{ctrl},
}

// This is on a separate line so the defer above catches it.
err = dialer.negotiate(ctx)
return dialer, err
if err != nil {
return nil, xerrors.Errorf("negotiate rtc connection: %w", err)
}

return dialer, nil
}

// Dialer enables arbitrary dialing to any network and address
Expand Down