Repository navigation
Description
Summary
Server.ListenAndServe signals readiness before the transport's connection pool is populated. A caller woken by that signal that immediately sends an outbound request can therefore fail with bind: address already in use, because the request's Via carries the listener's own host:port and ClientRequestConnection tries to create a connection for that address instead of reusing the listener that already holds it.
Environment
github.com/emiago/sipgo v1.6.0github.com/emiago/diago v0.32.0(the consumer of the readiness signal, viaServeBackground)- Go 1.26, observed on Linux (GitHub-hosted
ubuntu-latest) and macOS, UDP transport
What happens
A provider that starts serving and then sends its first request (e.g. REGISTER) can fail with:
diago: register: fail to create transaction req="REGISTER sip:127.0.0.1:21003 SIP/2.0":
client transcation failed to request connection: listen udp 127.0.0.1:40684:
bind: address already in use
The address in the error is the transport's own listening port (here 127.0.0.1:40684, assigned from the ephemeral range because the listener was created on port 0).
Root cause
server.go (v1.6.0), UDP branch:
udpConn, err := net.ListenUDP(network, laddr) // line 117 — the port is now bound
if err != nil { ... }
go watchContext(udpConn)
listenReadyCtx(ctx, network, udpConn.LocalAddr().String()) // line 123 — signals "ready"
return srv.tp.ServeUDP(udpConn) // line 124 — pool.Add happens in hereTransportUDP.Serve is what adds the listener to the pool (t.pool.Add(c.PacketAddr, c)), i.e. the connection becomes discoverable after the readiness signal has already been delivered.
The sending side then does (sip/transport_layer.go, ClientRequestConnection):
laddr := req.Laddr
// "This is probably client forcing host:port"
if laddr.IP != nil && laddr.Port > 0 {
c = transport.GetConnection(laddr.String()) // misses: pool entry not added yet
} else if l.connectionReuse {
c = transport.GetConnection(raddr.String())
}
if c == nil {
c, err = transport.CreateConnection(ctx, laddr, raddr, l.handleMessage) // binds laddr
if err != nil { return nil, err } // EADDRINUSE
}Because the request's Via sent-by legitimately carries our listener's host:port (which is the point — overrideSentBy rewrites the Via from the connection afterwards), laddr.Port > 0 is true, GetConnection(laddr) finds nothing, and CreateConnection calls net.ListenPacket(ctx, "udp", "127.0.0.1:40684") on an address the listener already owns.
Adding a mutex or serialising serves does not help: the two paths are correctly synchronised, the ordering is simply wrong.
Why it is intermittent
The woken caller only wins the race if it is scheduled ahead of ServeUDP's pool insert right after the signal. listenReadyCtx sends on a buffered channel, so it does not yield — which is why this is rare in a quiet process and reproducible on loaded CI runners, where asynchronous preemption can land the serving goroutine right after the send. In our case it loses roughly 1 run in 3–6 on CI and has not reproduced locally in 20 consecutive -race -count=20 runs of the affected test.
Suggested fix (any one of these)
- Populate the pool before signalling readiness — e.g. do the
pool.AddinListenAndServe(orServeUDP) before callinglistenReadyCtx. - Have
TransportUDP.CreateConnection/addSingleflightreuse an existing listener whose local address equalsladdrrather than binding again. For UDP there can only be one socket perladdr, andTransportUDP.GetConnectionalready documents the intent ("will return same listener connection"). - Treat the readiness signal as "the socket is bound" and rename/split it so a caller is not invited to send before serving has started.
Reproduction sketch
ctx := context.WithValue(context.Background(), sipgo.ListenReadyCtxKey, readyCh)
go server.ListenAndServe(ctx, "udp", "127.0.0.1:0")
<-readyCh // "Network ready" — the pool may still be empty
client.Do(ctx, registerRequest) // Via carries 127.0.0.1:<listener port> → EADDRINUSEOur failing tests both create a provider, wait for readiness via diago's ServeBackground, and then send the first request immediately; both use an explicit LocalHost (127.0.0.1) so the Via carries a concrete address, which is what selects the laddr.Port > 0 branch.
Activity
emiago commentedon Oct 1, 2026
Pls share how you use diago here. Daigo has also some special listeners.
emiago commentedon Oct 12, 2026
Hi @s4idev . Can you share more on use case here. FYI some extra transport race stuff was added, but I have feeling use case of lib may be more of issue.