Skip to content

fix: ensure num_participants is accurate in webhook events (#4265)#4422

Open
MavenRain wants to merge 2 commits intolivekit:masterfrom
MavenRain:fix/webhook-num-participants
Open

fix: ensure num_participants is accurate in webhook events (#4265)#4422
MavenRain wants to merge 2 commits intolivekit:masterfrom
MavenRain:fix/webhook-num-participants

Conversation

@MavenRain
Copy link
Copy Markdown

Three fixes for stale/incorrect num_participants in webhook payloads:

  1. Move participant map insertion before MarkDirty in join path so updateProto() counts the new participant.
  2. Use fresh room.ToProto() for participant_joined webhook instead of a stale snapshot captured at session start.
  3. Remove direct NumParticipants-- in leave path (inconsistent with updateProto's IsDependent check), force immediate proto update, and wait for completion before triggering onClose callbacks.

…4265)

  Three fixes for stale/incorrect num_participants in webhook payloads:

  1. Move participant map insertion before MarkDirty in join path so
     updateProto() counts the new participant.
  2. Use fresh room.ToProto() for participant_joined webhook instead of
     a stale snapshot captured at session start.
  3. Remove direct NumParticipants-- in leave path (inconsistent with
     updateProto's IsDependent check), force immediate proto update,
     and wait for completion before triggering onClose callbacks.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 2, 2026

CLA assistant check
All committers have signed the CLA.

pkg/rtc/room.go Outdated
}
// Note: NumParticipants is NOT decremented directly here.
// Instead, updateProto() recalculates it from the participants map.
// This avoids stale counts in webhook events.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good.

pkg/rtc/room.go Outdated
} else {
r.protoProxy.MarkDirty(false)
// Mark dirty after adding participant so updateProto() counts them
r.protoProxy.MarkDirty(true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Setting it to false was by design. Room updates are batched. When set to true, for large rooms (live streaming kind of applications), the update rate could be quite high.

Maybe a separate method to get consistent view ican be invoked when sending webhook events.

Requesting @cnderrauber for more thoughts as he is thinking about this too.

pkg/rtc/room.go Outdated
// (e.g., in webhook callbacks triggered by p.Close()) reflect the
// correct participant count.
done := r.protoProxy.MarkDirty(true)
<-done
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants