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

fix: race that causes LocalParticipant to show up as RemoteParticipant #619

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type RTCEngine struct {
OnResuming func()
OnResumed func()
OnTranscription func(*livekit.Transcription)
OnSignalClientConnected func(*livekit.JoinResponse)

// callbacks to get data
CbGetLocalParticipantSID func() string
Expand Down Expand Up @@ -160,6 +161,10 @@ func (e *RTCEngine) JoinContext(ctx context.Context, url string, token string, p
return nil, err
}

if e.OnSignalClientConnected != nil {
e.OnSignalClientConnected(res)
}

e.client.Start()

// send offer
Expand Down
9 changes: 7 additions & 2 deletions regionurlprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ func (r *regionURLProvider) RefreshRegionSettings(cloudHostname, token string) e
return errors.New("refreshRegionSettings failed to decode region settings: " + err.Error())
}

r.mutex.Lock()
r.hostnameSettingsCache[cloudHostname] = &hostnameSettingsCacheItem{
item := &hostnameSettingsCacheItem{
regionSettings: regions,
updatedAt: time.Now(),
regionURLAttempts: map[string]int{},
}
r.mutex.Lock()
r.hostnameSettingsCache[cloudHostname] = item
r.mutex.Unlock()

if len(item.regionSettings.Regions) == 0 {
logger.Warnw("no regions returned", nil, "cloudHostname", cloudHostname)
}

return nil
}

Expand Down
44 changes: 24 additions & 20 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
r.LocalParticipant = newLocalParticipant(engine, r.callback)

// callbacks from engine
engine.OnSignalClientConnected = r.handleSignalClientConnected
engine.OnMediaTrack = r.handleMediaTrack
engine.OnDisconnected = r.handleDisconnect
engine.OnParticipantUpdate = r.handleParticipantUpdate
Expand Down Expand Up @@ -328,6 +329,7 @@
d := time.Duration(1<<min(tries, 6)) * time.Second // max 64 seconds
logger.Errorw("failed to join room", err,
"retrying in", d,
"url", bestURL,
)
time.Sleep(d)
continue
Expand All @@ -338,32 +340,12 @@

if joinRes == nil {
var err error
joinRes, err = r.engine.JoinContext(ctx, url, token, params)

Check failure on line 343 in room.go

View workflow job for this annotation

GitHub Actions / test

this value of joinRes is never used (SA4006)
if err != nil {
return err
}
}

r.lock.Lock()
r.name = joinRes.Room.Name
r.metadata = joinRes.Room.Metadata
r.serverInfo = joinRes.ServerInfo
r.connectionState = ConnectionStateConnected
r.sifTrailer = make([]byte, len(joinRes.SifTrailer))
copy(r.sifTrailer, joinRes.SifTrailer)
r.lock.Unlock()

r.setSid(joinRes.Room.Sid, false)

r.LocalParticipant.updateInfo(joinRes.Participant)
r.LocalParticipant.updateSubscriptionPermission()

for _, pi := range joinRes.OtherParticipants {
rp := r.addRemoteParticipant(pi, true)
r.clearParticipantDefers(livekit.ParticipantID(pi.Sid), pi)
r.runParticipantDefers(livekit.ParticipantID(pi.Sid), rp)
}

return nil
}

Expand Down Expand Up @@ -552,6 +534,28 @@
r.runParticipantDefers(livekit.ParticipantID(participantID), rp)
}

func (r *Room) handleSignalClientConnected(joinRes *livekit.JoinResponse) {
r.lock.Lock()
r.name = joinRes.Room.Name
r.metadata = joinRes.Room.Metadata
r.serverInfo = joinRes.ServerInfo
r.connectionState = ConnectionStateConnected
r.sifTrailer = make([]byte, len(joinRes.SifTrailer))
copy(r.sifTrailer, joinRes.SifTrailer)
r.lock.Unlock()

r.setSid(joinRes.Room.Sid, false)

r.LocalParticipant.updateInfo(joinRes.Participant)
r.LocalParticipant.updateSubscriptionPermission()

for _, pi := range joinRes.OtherParticipants {
r.addRemoteParticipant(pi, true)
r.clearParticipantDefers(livekit.ParticipantID(pi.Sid), pi)
// no need to run participant defers here, since we are connected for the first time
}
}

func (r *Room) handleDisconnect(reason DisconnectionReason) {
r.callback.OnDisconnected()
r.callback.OnDisconnectedWithReason(reason)
Expand Down
Loading