This repository was archived by the owner on Aug 30, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork18
fix: TCP connections leaking after RTC disconnects#397
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
748ebf8
fix: TCP connections leaking after RTC disconnects
kylecarbsc89026e
Fix race condition
kylecarbsc8ec3e1
Add switch
kylecarbs6d888d2
Lower tick time
kylecarbs7637803
Fix data race
kylecarbs2adfb26
Update Go version
kylecarbs6e59ce2
Fix log err
kylecarbscd3e6a0
Fix race
kylecarbsFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletionci/image/Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.16.5 | ||
ENV GOFLAGS="-mod=readonly" | ||
ENV CI=true | ||
2 changes: 2 additions & 0 deletionswsnet/conn.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletionswsnet/dial_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
31 changes: 22 additions & 9 deletionswsnet/listen.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
decoder = json.NewDecoder(conn) | ||
rtc *webrtc.PeerConnection | ||
// If candidates are sent before an offer, we place them here. | ||
@@ -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)) | ||
d, _ := json.Marshal(&BrokerMessage{ | ||
Error: err.Error(), | ||
@@ -187,7 +189,6 @@ func (l *listener) negotiate(ctx context.Context, conn net.Conn) { | ||
} | ||
) | ||
l.log.Info(ctx, "accepted new session from broker connection, negotiating") | ||
for { | ||
@@ -255,17 +256,26 @@ func (l *listener) negotiate(ctx context.Context, conn net.Conn) { | ||
return | ||
} | ||
rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) { | ||
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() | ||
kylecarbs marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return | ||
} | ||
// Close connections opened when RTC was alive. | ||
l.connClosersMut.Lock() | ||
defer l.connClosersMut.Unlock() | ||
for _, connCloser := range l.connClosers { | ||
_ = connCloser.Close() | ||
} | ||
l.connClosers =make([]io.Closer, 0) | ||
}) | ||
flushCandidates := proxyICECandidates(rtc, conn) | ||
rtc.OnDataChannel(l.handle(ctx, msg)) | ||
l.log.Debug(ctx, "set remote description", slog.F("offer", *msg.Offer)) | ||
@@ -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() | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.