Repository navigation
Conversation
The CANCEL sent for a still-ringing outbound call was built by the transaction layer (tx.Cancel), which copies only the mandatory INVITE headers. sendCancel, which applies attributes_to_headers and caller headers, was unreachable in the ringing state because c.invite was only set after the final 200 OK. As a result there was no way to send an RFC 3326 Reason header (e.g. 'Call completed elsewhere') or any other mapped header on that CANCEL. See: livekit#846 - remember the INVITE as soon as its transaction is created, so the ringing state can build a header-aware CANCEL from it - give sipResponse an optional onCancel hook so context cancellation sends the header-aware CANCEL instead of the bare transaction one (c.mu is held by the same goroutine, so sendCancel is called directly) - clear the remembered INVITE once the transaction completes, and drop it in watchCancelledInvite so a racing 2xx never gets a stray CANCEL
| resp, err := sipResponse(ctx, tx, c.c.closing.Watch(), setState, func() { | ||
| // Cancel with attributes_to_headers applied (e.g. RFC 3326 Reason). | ||
| // NOTE: c.mu is held by this goroutine higher up the stack (Invite), | ||
| // so all other mutators are blocked on it; call sendCancel directly | ||
| // instead of re-locking (which would deadlock). | ||
| c.sendCancel(context.WithoutCancel(ctx), nil) |
There was a problem hiding this comment.
🟡 Ringing EndCall headers remain dropped
When EndCall closes a ringing call, onCancel invokes sendCancel without its headers. sendCancel clears c.invite, so the blocked Close cannot send them later.
Learn more
Invite holds the signaling mutex while waiting for the INVITE response. EndCall first closes the outer call, which cancels the INVITE context, then blocks in stopSIP waiting to call sipOutbound.Close under that mutex. The cancellation hook therefore runs first with nil headers and sendCancel clears the remembered INVITE. Once Invite releases the mutex, sipOutbound.Close sees no INVITE and cannot send the EndCall headers.
Example: An RPC ends a ringing call with Reason: SIP;cause=200. The hook sends a CANCEL containing mapped participant attributes but no Reason; the later close sends nothing.
Recommended fix: Stage the EndCall headers where the cancellation hook can consume them, or restructure cancellation so sipOutbound.Close builds the single CANCEL after receiving those headers. Add an end-to-end ringing lk.sip.EndCall test that asserts its Reason header on the wire.
Was this helpful? React with 👍 or 👎 to provide feedback.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #854 +/- ##
==========================================
+ Coverage 65.25% 68.58% +3.32%
==========================================
Files 51 43 -8
Lines 6588 8581 +1993
==========================================
+ Hits 4299 5885 +1586
- Misses 1915 2175 +260
- Partials 374 521 +147 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @hechen-eng, gentle review request on this small fix — it makes sure |
Summary
The CANCEL sent for a still-ringing outbound call carried none of the trunk's
attributes_to_headersmappings, and none of the headers passed tolk.sip.EndCall. There was no way to send an RFC 3326Reasonheader (e.g.Reason: SIP;cause=200;text="Call completed elsewhere"), the standard signal for "another endpoint answered" in a parallel ring.Closes #846.
Root cause
sendCancel(which appliesattributes_to_headersand caller headers) was unreachable in the ringing state. It is only called fromsipOutbound.Closewhenc.invite != nil && c.inviteOk == nil, butc.invitewas only set after the final 200 OK:So while ringing,
c.invite == nil,sipOutbound.Closetook thedrop()branch, and the actual CANCEL on the wire came fromtx.Cancel()insidesipResponse— built by the transaction layer, which copies only the mandatory INVITE headers (Via/Route/Max-Forwards/From/To/Call-ID/CSeq).What changed
pkg/sip/outbound.goattemptInvitenow setsc.invite = reqright afterTransactionRequestsucceeds, so the ringing state can build a header-aware CANCEL from it.sipResponsetakes an optionalonCancelhook. When set, context cancellation / service shutdown sends the header-aware CANCEL viasendCancelinstead of the baretx.Cancel().c.muis held by the same goroutine up the stack (theInvitemethod), sosendCancelis called directly without re-locking.c.inviteis set back tonilso nothing else can cancel a dead transaction.watchCancelledInvitedrops the call state. A racing 2xx that arrives after the cancel would previously leavec.inviteset; nowwatchCancelledInvitecallsdrop()on completion so a stray CANCEL can never be sent against an answered call.pkg/sip/protocol.gosipResponsecall sites (sendBye,sendRefer) to passnilfor the newonCancelparameter — their behavior is unchanged.Tests
Three new tests in
pkg/sip/outbound_test.go:TestSIPResponseCancelUsesHook— verifiessipResponseinvokes theonCancelhook instead oftx.Cancel()when one is provided.TestOutboundCloseWhileRingingSendsCancelWithHeaders— unit test: asipOutboundwith a remembered INVITE, when closed while ringing, sends a CANCEL carrying bothattributes_to_headersmappings and caller-supplied headers (e.g.Reason), with the correct CSeq and Via.TestOutboundCancelWhileRinging— end-to-end:CreateSIPParticipant(wait_until_answered=true)is cancelled while the remote is still sending 180 Ringing; the wire shows a CANCEL withUser-Agent: LiveKit.Existing tests updated for the new
sipResponse/watchCancelledInvitesignatures; all pass.Why this approach
The issue suggests two alternatives: (a) let the sipgo transaction cancel accept extra headers, or (b) set
c.inviteearly soClose()→sendCancelapplies. Option (b) is entirely withinlivekit/sip— no coordinatedsipgodependency bump needed — and reuses the existing header-awaresendCancelpath. The only subtlety is thatsipResponse, notClose, is what detects the ringing-state cancel, so theonCancelhook lets that path reachsendCancelwithout re-lockingc.mu(held by theInvitegoroutine).Verified locally with golangci-lint v2.13.1 (same as CI, 0 issues) and the full
go test ./pkg/...suite.