-
Notifications
You must be signed in to change notification settings - Fork 233
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
1
commit 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.
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,79 @@ | ||
| // 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) | ||
| } |
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
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.
🟡 Saved caller attributes can be wiped out by an empty refresh, so custom hangup headers are lost again
The stored copy of the caller's attributes is unconditionally replaced with whatever the live room reports (
c.cachedAttrs = attrsatpkg/sip/inbound.go:1817-1820), even when that live report is empty, so the values kept for the hangup message can be erased and the custom headers go missing again.Impact: In the exact teardown situation this change is meant to fix, the outgoing hangup can still be sent without the configured custom headers.
How an empty live read overwrites the seeded cache
snapshotParticipantAttrs(inboundpkg/sip/inbound.go:1809-1821, outboundpkg/sip/outbound.go:183-195) writesc.cachedAttrs = attrswith no length check, unlikestoreParticipantAttrswhich deliberately ignores empty input (pkg/sip/inbound.go:1824). Right after seeding the cache from the join config (pkg/sip/inbound.go:1556-1557andpkg/sip/outbound.go:508-509) a snapshot is taken immediately; ifLocalParticipant.Attributes()has not yet been populated it returns an empty map and the seed is discarded. The same holds forparticipantAttributes()(pkg/sip/inbound.go:1832-1836), which snapshots first: if the room object still exists during teardown but the local participant's attribute map has already been cleared, the good cache is replaced with an empty one andfillHeadersthen returns the headers untouched (pkg/sip/inbound.go:1799-1801).Guarding the write with
if len(attrs) == 0 { return }makes the snapshot strictly additive/refreshing and keeps the fallback usable.Was this helpful? React with 👍 or 👎 to provide feedback.