From 909f47e4e2a1c074d103cc4e330f16d4edadcee8 Mon Sep 17 00:00:00 2001 From: Pascal Watteel Date: Tue, 17 Nov 2026 11:33:10 +0400 Subject: [PATCH] sip tests: pick the parked party's INVITE and ACK by sender and Call-ID; fail a BYE on an ended dialog TestCallPark failed in the unit tier of CI run 44008466828 (PR #398, a store-only change) at park_test.go:433: "timed out waiting for BYE to the retriever when the parked party hangs up". It did not reproduce locally in about 2,000 runs: TestCallPark alone with and without -race, with -test.cpu=1, with random 0-300 ms delays injected at each step of retrieve, answer, BYE handling and linked hang-up, and in three full internal/sip package runs. The product path in that step is in order before the retriever's 200 goes out. parkRetrieved sets the parked call's peer (park.go:539), and handleRetrieve sets the retriever's link before answerSelf (park.go:633-636). After that, the parked party's BYE goes handleBye -> hangup -> linkedHangup -> notifyPeer -> the retriever's linkedHangup -> byeCallerRaw on every interleaving tried. The test has a gap that produces exactly this failure without a product bug. establish took the next INVITE and ACK queued for b, without checking whose they were. byeFrom then called sipgo's DialogServerSession.Bye, which returns nil without sending anything when the dialog has already ended (sipgo v1.7.3 dialog_server.go:364-368). So if b's queue gave establish another call's INVITE, step 4 got the step-3 dialog, which had already ended. byeFrom "succeeded" without sending a BYE, the PBX was never told the party hung up, and the wait for the retriever's BYE timed out after 10 s. The CI timing fits that: 16.64 s in total, which is the usual ~6.6 s for the earlier steps plus the 10 s wait. - establish drains b's INVITE and ACK queues before dialling. It then takes the INVITE sent by the PBX and the ACK with that INVITE's Call-ID. - byeFrom fails at once if the callee's dialog has already ended, instead of passing without sending a BYE. After: go test -race -count=50 -run 'TestCallPark$' ./internal/sip/ passes 50/50, and -race -count=3 -run TestCallPark (the whole park family) passes. The boss and session-timer tests that share establish and byeFrom also pass. --- internal/sip/park_test.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/internal/sip/park_test.go b/internal/sip/park_test.go index dd74bc85..f0097fcc 100644 --- a/internal/sip/park_test.go +++ b/internal/sip/park_test.go @@ -276,12 +276,18 @@ type parkedCall struct { func establish(t *testing.T, a, b *phone) parkedCall { t.Helper() + // The INVITE and ACK that b gets for this call are picked by their + // sender and Call-ID, not by queue position: anything left over in + // the queues would otherwise give the test another call's dialog, + // and a BYE on that ended dialog is never sent (byeFrom). + drainReqs(b.invites) + drainReqs(b.acks) r := waitCall(t, dial(t.Context(), a, "200")) if r.err != nil { t.Fatalf("call: %v", r.err) } - invB := waitReq(t, b.invites, "INVITE to b") - waitReq(t, b.acks, "ACK to b") + invB := waitReqFrom(t, b.invites, b.pbx, "", "INVITE to b") + waitReqFrom(t, b.acks, b.pbx, invB.CallID().Value(), "ACK to b") pc := parkedCall{r: r, feedA: startRTPFeed(t, r.dcs.InviteResponse.Body()), feedB: startRTPFeed(t, invB.Body()), dssB: b.serverFor(invB)} pc.feedA.silence(0.1) @@ -435,9 +441,33 @@ func TestCallPark(t *testing.T) { pbx.nextCDR(t) } -// byeFrom hangs up a call from the phone that was called. +// waitReqFrom waits on ch for a request sent by src (the PBX) and, when +// callID is set, of that Call-ID; other requests are skipped. +func waitReqFrom(t *testing.T, ch chan *sip.Request, src, callID, what string) *sip.Request { + t.Helper() + deadline := time.After(10 * time.Second) + for { + select { + case r := <-ch: + if r.Source() == src && (callID == "" || r.CallID().Value() == callID) { + return r + } + case <-deadline: + t.Fatalf("timed out waiting for %s", what) + return nil + } + } +} + +// byeFrom hangs up a call from the phone that was called. sipgo's Bye +// returns nil without sending anything on a dialog that has already +// ended, which would leave the test waiting for a BYE the PBX was never +// asked to relay: that is a failure here, not a silent no-op. func byeFrom(t *testing.T, dss *sipgo.DialogServerSession) { t.Helper() + if dss.LoadState() == sip.DialogStateEnded { + t.Fatalf("BYE from the callee: its dialog %s had already ended", dss.InviteRequest.CallID().Value()) + } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := dss.Bye(ctx); err != nil {