From 0ab17de8c9318be316b89b7d92faf402af416c3c Mon Sep 17 00:00:00 2001 From: Pascal Watteel Date: Tue, 17 Nov 2026 08:47:33 +0400 Subject: [PATCH] sip: TestOriginateHoldRelayed waits for the callee's answer before its re-INVITE The test took the destination phone's server dialog as soon as onInvite recorded it and sent a hold re-INVITE on it, while the callee function could still be writing the 200 (sipgo sets Dialog.InviteResponse in WriteResponse and reads it in buildReq): a data race under -race (CI run 44014233188). The test phone now closes a per-Call-ID channel when its callee function returns, and answeredDialog waits for it before handing the dialog out. --- internal/sip/helpers_test.go | 30 ++++++++++++++++++++++++++++-- internal/sip/originate_test.go | 4 +--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/sip/helpers_test.go b/internal/sip/helpers_test.go index 6e853458a..86b648ebb 100644 --- a/internal/sip/helpers_test.go +++ b/internal/sip/helpers_test.go @@ -530,7 +530,11 @@ type phone struct { challenged chan *sip.Request byeAt time.Time servers map[string]*sipgo.DialogServerSession - clients map[string]*sipgo.DialogClientSession + // handled is closed, per Call-ID, once the callee function has + // returned: its responses are written and the dialog is safe to use + // from the test. + handled map[string]chan struct{} + clients map[string]*sipgo.DialogClientSession // pending holds a dialing INVITE's Call-ID until its answer is // confirmed (closed then), so onBye never reads a session WaitAnswer // is still writing. @@ -612,7 +616,7 @@ func startPhone(t *testing.T, pbx *testPBX, user, pass string, conn net.PacketCo sdp: "v=0\r\no=" + user + " 1 1 IN IP4 127.0.0.1\r\ns=-\r\nc=IN IP4 127.0.0.1\r\nt=0 0\r\n" + "m=audio " + strconv.Itoa(rtpPort) + " RTP/AVP 0 101\r\na=rtpmap:101 telephone-event/8000\r\n", contact: sip.ContactHeader{Address: sip.Uri{Scheme: "sip", User: user, Host: host, Port: port}}, - servers: map[string]*sipgo.DialogServerSession{}, clients: map[string]*sipgo.DialogClientSession{}, pending: map[string]chan struct{}{}, + servers: map[string]*sipgo.DialogServerSession{}, handled: map[string]chan struct{}{}, clients: map[string]*sipgo.DialogClientSession{}, pending: map[string]chan struct{}{}, invites: make(chan *sip.Request, 16), reinvites: make(chan *sip.Request, 16), cancels: make(chan *sip.Request, 16), acks: make(chan *sip.Request, 16), byes: make(chan *sip.Request, 16), rings: make(chan int, 16), infos: make(chan *sip.Request, 16), messages: make(chan *sip.Request, 16), @@ -694,12 +698,34 @@ func (p *phone) onInvite(req *sip.Request, tx sip.ServerTransaction) { } p.mu.Lock() p.servers[req.CallID().Value()] = dss + handled := make(chan struct{}) + p.handled[req.CallID().Value()] = handled f := p.callee p.mu.Unlock() push(p.invites, req) + defer close(handled) f(p, req, tx, dss) } +// answeredDialog returns the server dialog of callID once the callee +// function has finished answering it, so the test can send in-dialog +// requests without racing the handler's writes to the dialog. +func (p *phone) answeredDialog(t *testing.T, callID string) *sipgo.DialogServerSession { + t.Helper() + p.mu.Lock() + dss, handled := p.servers[callID], p.handled[callID] + p.mu.Unlock() + if handled == nil { + t.Fatalf("%s: no INVITE for %s", p.user, callID) + } + select { + case <-handled: + case <-time.After(5 * time.Second): + t.Fatalf("%s: the answer to %s did not finish", p.user, callID) + } + return dss +} + // challengeInvite answers an INVITE without the phone's credentials with a // 401 when the phone authenticates incoming INVITEs; true when it did. func (p *phone) challengeInvite(req *sip.Request, tx sip.ServerTransaction) bool { diff --git a/internal/sip/originate_test.go b/internal/sip/originate_test.go index 8b15466a7..99a509b4a 100644 --- a/internal/sip/originate_test.go +++ b/internal/sip/originate_test.go @@ -396,9 +396,7 @@ func TestOriginateHoldRelayed(t *testing.T) { binv := waitReq(t, e.b.invites, "destination INVITE") e.orig.waitState(t, "o1", livestate.OriginateConnected) waitReq(t, e.a.acks, "phone ACK") - e.b.mu.Lock() - dss := e.b.servers[binv.CallID().Value()] - e.b.mu.Unlock() + dss := e.b.answeredDialog(t, binv.CallID().Value()) hold := strings.Replace(e.b.sdp, "m=audio", "a=sendonly\r\nm=audio", 1) req := sip.NewRequest(sip.INVITE, *binv.Contact().Address.Clone()) req.AppendHeader(sip.NewHeader("Content-Type", "application/sdp"))