From b0798ac9d8accae6e3d228c0fbbe8feb3fe1de2e Mon Sep 17 00:00:00 2001 From: Pascal Watteel Date: Tue, 17 Nov 2026 12:05:14 +0400 Subject: [PATCH] sip: a retrieve right after the parker's BYE keeps its audio and gets no hold music The parker's phone gets its BYE before parkRun's byeA (or w.bye) returns, and a retrieve can land in that window. parkRetrieved attaches the retriever to the parker's relay leg (attachParty -> handOverLeg). Then parkRun went on as if nothing had happened: - relay.SetTarget(parkerLeg, nil) cleared the leg the retriever had just been attached to. The parked party's audio was dropped until the retriever's own RTP latched the leg again. A phone that sends nothing first, or sends from its SDP address only after hearing something, stayed one-way. - parkHold started the music on hold on the parked party's leg and stored it in pk.music. parkRetrieved had already passed its stopParkMusic, so the music played over the retrieved call until parkHold's loop saw the halt. parkRun now releases the parker's leg only while the call is still parked. It checks c.park and pk.phase under c.mu, which is the lock parkRetrieved takes to leave the parked phase, and returns without entering parkHold once a retrieve has won. parkHold starts the music under the same lock, and only for a call that is still parked and not ended. So a retrieve either comes after the clear and attaches over it, or comes before and the leg and the party are left alone. A retrieve that comes after the music started stops it, as before. TestCallParkRetrieveAsParkerLeaves holds the park goroutine right after the parker's BYE (parkerGoneHook, tests only) and retrieves in that window. It then checks two things: the parked party gets no RTP while nobody talks, and the parked party's first packet reaches the retriever's SDP address before the retriever has sent anything. Before the fix both checks fail ("received 1 RTP packets ... (music on hold)", "the retriever's SDP address did not get the parked party's audio"). After the fix, -race -count=50 passes 50/50. The park, pickup and session-timer tests pass with -race -count=3, and the internal/sip package passes. --- internal/sip/park.go | 31 ++++++++- internal/sip/park_retrieve_test.go | 101 +++++++++++++++++++++++++++++ internal/sip/server.go | 5 ++ 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 internal/sip/park_retrieve_test.go diff --git a/internal/sip/park.go b/internal/sip/park.go index afb66ebd..c637fb7f 100644 --- a/internal/sip/park.go +++ b/internal/sip/park.go @@ -397,8 +397,26 @@ func (c *call) parkRun(pk *parkState, relay *media.Relay, partyLeg, parkerLeg st } else { w.bye() } - relay.SetTarget(parkerLeg, nil) + if h := s.parkerGoneHook.Load(); h != nil { + (*h)() + } cancel() + // The parker's BYE is out, but a retrieve may already have taken the + // call: the retriever is attached to the parker's relay leg as soon as + // the parker's phone has the BYE, which is before byeA returns. The + // parker's leg is released only while the call is still parked, under + // the lock parkRetrieved takes to leave that phase, so a retrieve + // either finds the leg released and attaches over it, or has already + // attached and the leg is left alone. + c.mu.Lock() + parked := c.park == pk && pk.phase == phaseParked + if parked { + relay.SetTarget(parkerLeg, nil) + } + c.mu.Unlock() + if !parked { + return + } c.parkHold(pk, relay, partyLeg, parkerLeg, pk.returnAt) } @@ -425,9 +443,16 @@ func (c *call) parkHold(pk *parkState, relay *media.Relay, partyLeg, parkerLeg s if snap := s.deps.Snapshots.Current(); snap != nil { classID = c.holdClassID(snap, parkerLeg == legCaller) } - player := c.runMOH(relay, partyLeg, "", classID) + // The music starts only while the call is still parked, decided under + // the lock parkRetrieved takes to leave that phase: a retrieve that won + // before this point must not get the music played over the retrieved + // call, and one that comes after stops the music it finds. c.mu.Lock() - pk.music = player + if c.park != pk || pk.phase != phaseParked || c.ended || c.hungUp { + c.mu.Unlock() + return + } + pk.music = c.runMOH(relay, partyLeg, "", classID) c.mu.Unlock() defer c.stopParkMusic(pk) diff --git a/internal/sip/park_retrieve_test.go b/internal/sip/park_retrieve_test.go new file mode 100644 index 00000000..36959e51 --- /dev/null +++ b/internal/sip/park_retrieve_test.go @@ -0,0 +1,101 @@ +package sip + +import ( + "net" + "strconv" + "testing" + "time" + + "github.com/azrtydxb/hello/internal/media" +) + +// TestCallParkRetrieveAsParkerLeaves: a retrieve can land while the park +// goroutine is still finishing with the parker, between the parker's BYE +// and the release of the parker's relay leg. That leg is the one the +// retriever is attached to. It fails if the park goroutine then clears the +// retriever's media target (the retriever stops hearing the parked party) or +// starts the music on hold over the retrieved call. +func TestCallParkRetrieveAsParkerLeaves(t *testing.T) { + st := parkStore(t) + pbx := startPBX(t, threeDevices(), withMedia(false), withPark(st, "sip-a", testLot)) + a, b, c := newPhone(t, pbx, "a1", "pa"), newPhone(t, pbx, "b1", "pb1"), newPhone(t, pbx, "c1", "pc") + for _, p := range []*phone{a, b, c} { + p.register(t) + p.setCallee(answerAfter(nil)) + } + reached, release := make(chan struct{}), make(chan struct{}) + hold := func() { + close(reached) + select { + case <-release: + case <-time.After(10 * time.Second): + } + } + pbx.srv.parkerGoneHook.Store(&hold) + + pc := establish(t, a, b) + parkBy(t, a, pc, "*74702") + occupied(t, st, "702", "100") + waitReq(t, a.byes, "BYE to the parker") + select { + case <-reached: + case <-time.After(10 * time.Second): + t.Fatal("the park goroutine did not finish the parker's BYE") + } + + // The retrieve lands while the park goroutine is held right after the + // parker's BYE. + r := waitCall(t, dial(t.Context(), c, "*75702")) + if r.err != nil { + t.Fatalf("retrieve: %v", r.err) + } + feedC := startRTPFeed(t, r.dcs.InviteResponse.Body()) + // The retriever's media address is the one its SDP names. Listening + // there sees what the relay sends before any packet from the retriever + // has re-latched the leg to the feed's socket. + offer, err := media.ParseAudioSDP([]byte(c.sdpText())) + if err != nil { + t.Fatal(err) + } + atC, err := net.ListenPacket("udp", net.JoinHostPort(offer.Address, strconv.Itoa(offer.Port))) + if err != nil { + t.Fatalf("listen on the retriever's SDP address: %v", err) + } + t.Cleanup(func() { _ = atC.Close() }) + stop := make(chan struct{}) + toB := countRTP(pc.feedB.conn, stop) + close(release) + + // Nobody talks for a while: the parked party must hear nothing, which + // rules out music on hold started over the retrieved call. + time.Sleep(800 * time.Millisecond) + close(stop) + time.Sleep(150 * time.Millisecond) // the counter's last read returns + if n := toB.Load(); n != 0 { + t.Errorf("the retrieved party received %d RTP packets while nobody talked, want 0 (music on hold)", n) + } + + // The parked party talks first: the retriever's leg must still point + // at the retriever's SDP address, which the retrieve set, not have been + // cleared as the parker's. + pc.feedB.packet(0, []byte("from-b-to-the-retriever....."), false) + if !hears(atC, "from-b-to-the-retriever.....", 3*time.Second) { + t.Fatal("the retriever's SDP address did not get the parked party's audio (its relay leg was cleared)") + } + // Both ways once the retriever talks too. + feedC.packet(0, make([]byte, 160), false) + time.Sleep(100 * time.Millisecond) + pc.feedB.packet(0, []byte("from-b-to-the-retriever-2..."), false) + if !hears(feedC.conn, "from-b-to-the-retriever-2...", 3*time.Second) { + t.Fatal("the retriever did not hear the parked party") + } + feedC.packet(0, []byte("from-c-to-the-parked-party.."), false) + if !hears(pc.feedB.conn, "from-c-to-the-parked-party..", 3*time.Second) { + t.Fatal("the parked party did not hear the retriever") + } + + hangup(t, r.dcs) + waitReq(t, b.byes, "BYE to the parked party when the retriever hangs up") + pbx.nextCDR(t) + pbx.nextCDR(t) +} diff --git a/internal/sip/server.go b/internal/sip/server.go index 568b9331..dfad7da6 100644 --- a/internal/sip/server.go +++ b/internal/sip/server.go @@ -367,6 +367,11 @@ type Server struct { // WriteResponse returns without an ACK, before the dialog is checked // for the caller's BYE, so a test can land a late ACK in between. unackedHook atomic.Pointer[func()] + // parkerGoneHook, when set (tests only), runs in a parked call's park + // goroutine once the parker's dialog has ended, before the parker's + // relay leg is released and the music on hold starts, so a test can + // land a retrieve in between. + parkerGoneHook atomic.Pointer[func()] // abortHook, when set (tests only), runs inside abort after it has // taken the setup's ownership before it signals the setup goroutine, so // a test can inject the fork failure that races the abort.