Skip to content

Commit

Permalink
Avoid Go crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Feb 17, 2025
1 parent ad02f7e commit 3cea785
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
20 changes: 1 addition & 19 deletions talpid-wireguard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl WireguardMonitor {
let should_negotiate_ephemeral_peer = config.quantum_resistant || config.daita;

let (cancel_token, cancel_receiver) = connectivity::CancelToken::new();
let mut connectivity_check = connectivity::Check::new(
let connectivity_check = connectivity::Check::new(
config.ipv4_gateway,
args.retry_attempt,
cancel_receiver.clone(),
Expand Down Expand Up @@ -500,24 +500,6 @@ impl WireguardMonitor {
.await;
}

match connectivity_check
.establish_connectivity(&tunnel.lock().await.as_ref().unwrap())
.await
{
Ok(true) => Ok(()),
Ok(false) => {
log::warn!("Timeout while checking tunnel connection");
Err(CloseMsg::PingErr)
}
Err(error) => {
log::error!(
"{}",
error.display_chain_with_msg("Failed to check tunnel connection")
);
Err(CloseMsg::PingErr)
}
}?;

let metadata = Self::tunnel_metadata(&iface_name, &config);
event_hook.on_event(TunnelEvent::Up(metadata)).await;

Expand Down
29 changes: 29 additions & 0 deletions talpid-wireguard/src/wireguard_go/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ impl WgGoTunnel {
tunnel.wait_for_routes().await?;
}

// This seemingly fixes the GO crash we see
tunnel.ensure_tunnel_is_running().await?;
log::debug!("DEBUG: get_tunnel 4");

Ok(tunnel)
}

Expand Down Expand Up @@ -551,6 +554,9 @@ impl WgGoTunnel {
tunnel.wait_for_routes().await?;
}

// This seemingly fixes the GO crash we see
tunnel.ensure_tunnel_is_running().await?;

Ok(tunnel)
}

Expand Down Expand Up @@ -585,6 +591,29 @@ impl WgGoTunnel {
.map_err(CloseMsg::SetupError)
.unwrap();

Ok(())
}
async fn ensure_tunnel_is_running(&self) -> Result<()> {
let state = self.as_state();
let addr = state.config.ipv4_gateway;
let cancel_receiver = state.cancel_receiver.clone();
let mut check = connectivity::Check::new(addr, 0, cancel_receiver)
.map_err(|err| TunnelError::RecoverableStartWireguardError(Box::new(err)))?;

// TODO: retry attempt?

let connection_established = check
.establish_connectivity(self)
.await
.map_err(|e| TunnelError::RecoverableStartWireguardError(Box::new(e)))?;

// Timed out
if !connection_established {
return Err(TunnelError::RecoverableStartWireguardError(Box::new(
super::Error::TimeoutError,
)));
}

Ok(())
}
}
Expand Down

0 comments on commit 3cea785

Please sign in to comment.