mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-06 06:31:31 +00:00
hypha: protect from panics in the network stack (due to interface going away)
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::panic::UnwindSafe;
|
||||
use std::str::FromStr;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::{panic, thread};
|
||||
|
||||
use advmac::MacAddr6;
|
||||
use anyhow::{anyhow, Result};
|
||||
@ -21,6 +22,7 @@ pub struct NetworkBackend {
|
||||
}
|
||||
|
||||
unsafe impl Send for NetworkBackend {}
|
||||
impl UnwindSafe for NetworkBackend {}
|
||||
|
||||
pub struct NetworkService {
|
||||
pub network: String,
|
||||
@ -66,10 +68,25 @@ impl NetworkBackend {
|
||||
}
|
||||
let link = link.unwrap();
|
||||
handle.link().set(link.header.index).up().execute().await?;
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<()> {
|
||||
pub fn run(mut self) -> Result<()> {
|
||||
let interface = self.interface.clone();
|
||||
let result = panic::catch_unwind(move || self.run_maybe_panic());
|
||||
|
||||
if result.is_err() {
|
||||
return Err(anyhow!(
|
||||
"network backend for interface {} encountered an error and is now shutdown",
|
||||
interface
|
||||
));
|
||||
}
|
||||
|
||||
result.unwrap()
|
||||
}
|
||||
|
||||
fn run_maybe_panic(&mut self) -> Result<()> {
|
||||
let mac = MacAddr6::random();
|
||||
let mac = HardwareAddress::Ethernet(EthernetAddress(mac.to_array()));
|
||||
let config = Config::new(mac);
|
||||
@ -128,14 +145,16 @@ impl NetworkService {
|
||||
spawned.push(name);
|
||||
}
|
||||
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_network_backend(&mut self, interface: &str) -> Result<()> {
|
||||
let interface = interface.to_string();
|
||||
let mut network = NetworkBackend::new(&interface, &[&self.network])?;
|
||||
info!("initializing network backend for interface {}", interface);
|
||||
network.init().await?;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
info!("spawning network backend for interface {}", interface);
|
||||
thread::spawn(move || {
|
||||
if let Err(error) = network.run() {
|
||||
|
Reference in New Issue
Block a user