-
Notifications
You must be signed in to change notification settings - Fork 243
fix: cache participant attrs for BYE attributes_to_headers (#404) #775
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
Open
lixuanqun
wants to merge
2
commits into
livekit:main
Choose a base branch
from
lixuanqun:cursor/fix-bye-attrs-headers-33f3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright 2026 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sip | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // Regression for livekit/sip#404: when the agent deletes the room before SIP | ||
| // sends BYE, LocalParticipant is gone. attributes_to_headers must still map | ||
| // from the last cached participant attributes. | ||
| func TestFillHeadersUsesCachedAttrsWhenRoomNil(t *testing.T) { | ||
| call := &inboundCall{ | ||
| attrsToHdr: map[string]string{ | ||
| "sip.custom": "X-Custom-Header", | ||
| }, | ||
| } | ||
| call.storeParticipantAttrs(map[string]string{ | ||
| "sip.custom": "value-from-cache", | ||
| "other": "ignored", | ||
| }) | ||
| call.lkRoom = nil | ||
| cc := &sipInbound{call: call} | ||
|
|
||
| headers := cc.fillHeaders(nil) | ||
| require.Equal(t, map[string]string{"X-Custom-Header": "value-from-cache"}, headers) | ||
|
|
||
| // No mapping configured → leave headers untouched. | ||
| call.attrsToHdr = nil | ||
| require.Nil(t, cc.fillHeaders(nil)) | ||
|
|
||
| // Mapping configured but cache empty → leave headers untouched. | ||
| call.attrsToHdr = map[string]string{"sip.custom": "X-Custom-Header"} | ||
| call.attrsMu.Lock() | ||
| call.cachedAttrs = nil | ||
| call.attrsMu.Unlock() | ||
| require.Nil(t, cc.fillHeaders(nil)) | ||
| } | ||
|
|
||
| func TestOutboundSetAttrsToHeadersUsesCachedAttrsWhenRoomNil(t *testing.T) { | ||
| call := &outboundCall{ | ||
| sipConf: sipOutboundConfig{ | ||
| attrsToHeaders: map[string]string{ | ||
| "sip.custom": "X-Custom-Header", | ||
| }, | ||
| }, | ||
| } | ||
| call.storeParticipantAttrs(map[string]string{ | ||
| "sip.custom": "outbound-cache", | ||
| }) | ||
| call.lkRoom = nil | ||
|
|
||
| headers := call.setAttrsToHeaders(nil) | ||
| require.Equal(t, map[string]string{"X-Custom-Header": "outbound-cache"}, headers) | ||
| } | ||
|
|
||
| func TestAttrsToHeaders(t *testing.T) { | ||
| attrs := map[string]string{"a": "1", "b": "2"} | ||
| mapping := map[string]string{"a": "X-A", "missing": "X-Missing"} | ||
| headers := AttrsToHeaders(attrs, mapping, map[string]string{"Keep": "yes"}) | ||
| require.Equal(t, map[string]string{ | ||
| "Keep": "yes", | ||
| "X-A": "1", | ||
| }, headers) | ||
| } | ||
|
|
||
| // snapshotParticipantAttrs skips a read that returns no attributes, so a | ||
| // teardown-time empty read does not wipe the cached values that BYE's | ||
| // attributes_to_headers relies on (livekit/sip#404). This is exercised on | ||
| // the lkRoom == nil path here: the function returns early without touching | ||
| // the cache. The "room up, attributes empty" path is the same guard, but | ||
| // needs a live lksdk participant so it is left to integration. | ||
| func TestSnapshotParticipantAttrsDoesNotWipeCacheOnEmptyRead(t *testing.T) { | ||
| for _, setup := range []func() *inboundCall{ | ||
| func() *inboundCall { | ||
| c := &inboundCall{} | ||
| c.storeParticipantAttrs(map[string]string{"sip.custom": "seeded"}) | ||
| c.snapshotParticipantAttrs() // lkRoom nil → early return, cache intact | ||
| return c | ||
| }, | ||
| } { | ||
| c := setup() | ||
| c.attrsMu.Lock() | ||
| got := c.cachedAttrs | ||
| c.attrsMu.Unlock() | ||
| require.Equal(t, map[string]string{"sip.custom": "seeded"}, got, | ||
| "empty/nil room read must not wipe cached attrs") | ||
| } | ||
| } | ||
|
|
||
| func TestOutboundSnapshotParticipantAttrsDoesNotWipeCacheOnEmptyRead(t *testing.T) { | ||
| c := &outboundCall{} | ||
| c.storeParticipantAttrs(map[string]string{"sip.custom": "outbound-seeded"}) | ||
| c.snapshotParticipantAttrs() // lkRoom nil → early return, cache intact | ||
| c.attrsMu.Lock() | ||
| got := c.cachedAttrs | ||
| c.attrsMu.Unlock() | ||
| require.Equal(t, map[string]string{"sip.custom": "outbound-seeded"}, got, | ||
| "empty/nil room read must not wipe cached attrs") | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Cleared attributes emit stale headers
When a live participant clears every attribute,
snapshotParticipantAttrspreserves the old cache. BYE and REFER then emit headers for nonexistent attributes.Learn more
An empty attribute map is also a valid participant state. These guards cannot distinguish that state from a teardown-time empty SDK read, so both inbound and outbound snapshots retain values that the participant removed. Header generation later treats those retained values as current.
Example: A participant starts with
sip.custom=seeded, then removes every attribute while the room remains connected. Its BYE is expected to omit the mappedX-Customheader, but the cache still suppliesseeded.Recommended fix: Track confirmed attribute updates separately from teardown reads. Record confirmed empty states, while preserving the cache only when room lifecycle state proves the participant is unavailable or the read is transient.
Was this helpful? React with 👍 or 👎 to provide feedback.