wgengine/magicsock: close old UDP sockets when TS_DEBUG_ALWAYS_USE_DERP is set

bindSocket's TS_DEBUG_ALWAYS_USE_DERP branch swapped in an empty socket
group without closing the existing one. If the knob was turned on after
the Conn was bound, a Rebind left the old socket open with no reference
to it, so its port stayed bound even after Conn.Close. Close the current
sockets first, as the normal bind path does.

Fixes #22524

Signed-off-by: Dean Chen <[email protected]>
This commit is contained in:
Dean Chen authored and Brad Fitzpatrick committed 2026-12-06 07:12:03 -08:00
1 parent 07fad02078
commit 1daa21f553
2 files changed
+23

No files matched your search

+3
View File
@@ -4028,6 +4028,9 @@ func (c *Conn) bindSocket(ruc *RebindingUDPConn, network string, curPortFate cur
if debugAlwaysDERP() {
c.logf("disabled %v per TS_DEBUG_ALWAYS_USE_DERP", network)
if err := ruc.closeLocked(); err != nil {
c.logf("magicsock: bindSocket %v close failed: %v", network, err)
}
ruc.setConnLocked(nil, c.udpSockets, "", c.controlKnobs)
return nil
}
+20
View File
@@ -5465,3 +5465,23 @@ func TestSetNetworkMapAndFilterAfterClose(t *testing.T) {
t.Error("relay client enabled on a closed Conn")
}
}
func TestBindSocketAlwaysDERPClosesOldSocket(t *testing.T) {
c, _ := newTestConnAndRegistry(t)
port := c.LocalPort()
if port == 0 {
t.Fatal("conn not bound to a UDP port")
}
envknob.SetenvForTest(t, "TS_DEBUG_ALWAYS_USE_DERP", "true")
c.Rebind()
if got := c.LocalPort(); got != 0 {
t.Fatalf("LocalPort after rebind = %d, want 0", got)
}
pc, err := net.ListenUDP("udp4", &net.UDPAddr{Port: int(port)})
if err != nil {
t.Fatalf("old port %d still in use after rebind: %v", port, err)
}
pc.Close()
}