Skip to content

Commit

Permalink
locking is hard
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 19, 2024
1 parent e4b7f4d commit 16d1e39
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions net-tools/netwatch/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,17 @@ impl UdpSocket {
/// Rebind the underlying socket.
pub fn rebind(&self) -> Result<()> {
// Remove old socket
let mut guard = self.socket.write().unwrap();
{
let mut guard = self.socket.write().unwrap();
let socket = guard.take().expect("not yet dropped");

drop(socket);
}

// Prepare new socket
let new_socket = inner_bind(self.addr)?;

// Insert new socket
self.socket.write().unwrap().replace(new_socket);
guard.replace(new_socket);

// Clear errors
self.is_broken
Expand All @@ -102,10 +101,8 @@ impl UdpSocket {
fn bind_raw(addr: impl Into<SocketAddr>) -> Result<Self> {
let mut addr = addr.into();
let socket = inner_bind(addr)?;
if addr.port() == 0 {
// update to use selected port
addr.set_port(socket.local_addr()?.port());
}
// update to use selected port
addr.set_port(socket.local_addr()?.port());

Ok(UdpSocket {
socket: Arc::new(RwLock::new(Some(socket))),
Expand Down

0 comments on commit 16d1e39

Please sign in to comment.