Try to close the socket first

This commit is contained in:
Owen
2026-01-17 17:35:00 -08:00
parent 6fd0984b13
commit ed4775bd26

View File

@@ -797,17 +797,19 @@ func (o *Olm) RebindSocket() error {
return fmt.Errorf("shared bind is not initialized")
}
// Get the current port so we can try to reuse it
currentPort := o.sharedBind.GetPort()
// Close the old socket first to release the port, then try to rebind to the same port
currentPort, err := o.sharedBind.CloseSocket()
if err != nil {
return fmt.Errorf("failed to close old socket: %w", err)
}
logger.Info("Rebinding UDP socket (current port: %d)", currentPort)
logger.Info("Rebinding UDP socket (released port: %d)", currentPort)
// Create a new UDP socket
var newConn *net.UDPConn
var newPort uint16
var err error
// First try to bind to the same port
// First try to bind to the same port (now available since we closed the old socket)
localAddr := &net.UDPAddr{
Port: int(currentPort),
IP: net.IPv4zero,