Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

fix: TCP connections leaking after RTC disconnects#397

Merged
kylecarbs merged 8 commits intomasterfromleakyconn
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletionci/image/Dockerfile
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
FROM golang:1.16.3
FROM golang:1.16.5

ENV GOFLAGS="-mod=readonly"
ENV CI=true
Expand Down
2 changes: 2 additions & 0 deletionswsnet/conn.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -129,6 +129,8 @@ type dataChannelConn struct {
}

func (c *dataChannelConn) init() {
c.closedMutex.Lock()
defer c.closedMutex.Unlock()
c.sendMore = make(chan struct{}, 1)
c.dc.SetBufferedAmountLowThreshold(bufferedAmountLowThreshold)
c.dc.OnBufferedAmountLow(func() {
Expand Down
37 changes: 37 additions & 0 deletionswsnet/dial_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -266,6 +266,43 @@ func TestDial(t *testing.T) {
_ = conn.Close()
assert.Equal(t, 1, dialer.activeConnections())
})

t.Run("Close Listeners on Disconnect", func(t *testing.T) {
t.Parallel()

tcpListener, err := net.Listen("tcp", "0.0.0.0:0")
require.NoError(t, err)
go func() {
_, _ = tcpListener.Accept()
}()

connectAddr, listenAddr := createDumbBroker(t)
l, err := Listen(context.Background(), slogtest.Make(t, nil), listenAddr, "")
require.NoError(t, err)

turnAddr, closeTurn := createTURNServer(t, ice.SchemeTypeTURN)
dialer, err := DialWebsocket(context.Background(), connectAddr, &DialOptions{
ICEServers: []webrtc.ICEServer{{
URLs: []string{fmt.Sprintf("turn:%s", turnAddr)},
Username: "example",
Credential: testPass,
CredentialType: webrtc.ICECredentialTypePassword,
}},
}, nil)
require.NoError(t, err)

_, err = dialer.DialContext(context.Background(), "tcp", tcpListener.Addr().String())
require.NoError(t, err)

closeTurn()

list := l.(*listener)
assert.Eventually(t, func() bool {
list.connClosersMut.Lock()
defer list.connClosersMut.Unlock()
return len(list.connClosers) == 0
}, time.Second*15, time.Millisecond*100)
})
}

func BenchmarkThroughput(b *testing.B) {
Expand Down
31 changes: 22 additions & 9 deletionswsnet/listen.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,9 +159,11 @@ func (l *listener) dial(ctx context.Context) (<-chan error, error) {
// so the cognitive overload linter has been disabled.
// nolint:gocognit,nestif
func (l *listener) negotiate(ctx context.Context, conn net.Conn) {
id := atomic.AddInt64(&l.nextConnNumber, 1)
ctx = slog.With(ctx, slog.F("conn_id", id))

var (
err error
id = atomic.AddInt64(&l.nextConnNumber, 1)
decoder = json.NewDecoder(conn)
rtc *webrtc.PeerConnection
// If candidates are sent before an offer, we place them here.
Expand All@@ -171,7 +173,7 @@ func (l *listener) negotiate(ctx context.Context, conn net.Conn) {
// Sends the error provided then closes the connection.
// If RTC isn't connected, we'll close it.
closeError = func(err error) {
l.log.Warn(ctx, "negotiation error, closing connection", slog.Error(err))
//l.log.Warn(ctx, "negotiation error, closing connection", slog.Error(err))

d, _ := json.Marshal(&BrokerMessage{
Error: err.Error(),
Expand All@@ -187,7 +189,6 @@ func (l *listener) negotiate(ctx context.Context, conn net.Conn) {
}
)

ctx = slog.With(ctx, slog.F("conn_id", id))
l.log.Info(ctx, "accepted new session from broker connection, negotiating")

for {
Expand DownExpand Up@@ -255,17 +256,26 @@ func (l *listener) negotiate(ctx context.Context, conn net.Conn) {
return
}
rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
l.log.Debug(ctx, "connection state change", slog.F("state", pcs.String()))
if pcs == webrtc.PeerConnectionStateConnecting {
l.log.Info(ctx, "connection state change", slog.F("state", pcs.String()))
switch pcs {
case webrtc.PeerConnectionStateConnected:
return
case webrtc.PeerConnectionStateConnecting:
// Safe to close the negotiating WebSocket.
_ = conn.Close()
return
}

// Close connections opened when RTC was alive.
l.connClosersMut.Lock()
defer l.connClosersMut.Unlock()
for _, connCloser := range l.connClosers {
_ = connCloser.Close()
}
_ =conn.Close()
l.connClosers =make([]io.Closer, 0)
})

flushCandidates := proxyICECandidates(rtc, conn)
l.connClosersMut.Lock()
l.connClosers = append(l.connClosers, rtc)
l.connClosersMut.Unlock()
rtc.OnDataChannel(l.handle(ctx, msg))

l.log.Debug(ctx, "set remote description", slog.F("offer", *msg.Offer))
Expand DownExpand Up@@ -420,6 +430,9 @@ func (l *listener) handle(ctx context.Context, msg BrokerMessage) func(dc *webrt
dc: dc,
rw: rw,
}
l.connClosersMut.Lock()
l.connClosers = append(l.connClosers, co)
l.connClosersMut.Unlock()
co.init()
defer nc.Close()
defer co.Close()
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp