fix: restore thread network namespaces and stabilize tunnel tests (#2683)

This commit is contained in:
KKRainbow authored and GitHub committed 2026-10-12 20:04:01 +08:00
1 parent 1649ec0013
commit 4b4f0c3cb3
5 files changed
+162 -53

No files matched your search

+55
View File
@@ -93,3 +93,58 @@ impl Drop for TestNetwork {
}
}
}
#[test]
fn nested_namespace_guard_restores_current_thread() {
let mut network = TestNetwork::new();
network.create_empty_namespace("net_a");
network.create_empty_namespace("net_b");
let net_a = NetNS::new(Some(network.namespace("net_a").into()));
let net_b = NetNS::new(Some(network.namespace("net_b").into()));
let current = || std::fs::read_link("/proc/thread-self/ns/net").unwrap();
let original = current();
net_a.run(|| {
let expected = current();
assert_ne!(expected, original);
net_b.run(|| assert_ne!(current(), expected));
assert_eq!(current(), expected);
});
assert_eq!(current(), original);
}
#[tokio::test]
async fn async_namespace_restores_between_polls() {
let mut network = TestNetwork::new();
network.create_empty_namespace("net_a");
network.create_empty_namespace("net_b");
let net_a = NetNS::new(Some(network.namespace("net_a").into()));
let net_b = NetNS::new(Some(network.namespace("net_b").into()));
let current = || std::fs::read_link("/proc/thread-self/ns/net").unwrap();
let original = current();
let expected = net_a.run(current);
let mut future = Box::pin(net_a.run_async(|| async {
assert_eq!(current(), expected);
tokio::task::yield_now().await;
assert_eq!(current(), expected);
}));
assert!(futures::poll!(future.as_mut()).is_pending());
assert_eq!(current(), original);
net_b.run(|| assert_ne!(current(), expected));
future.await;
assert_eq!(current(), original);
let dropped_in = std::cell::RefCell::new(None);
let mut cancelled = Box::pin(net_a.run_async(|| async {
let _cleanup = guarden::guard::ContextGuard::new((), |_| {
*dropped_in.borrow_mut() = Some(current());
});
std::future::pending::<()>().await;
}));
assert!(futures::poll!(cancelled.as_mut()).is_pending());
assert_eq!(current(), original);
drop(cancelled);
assert_eq!(current(), original);
assert_eq!(*dropped_in.borrow(), Some(expected));
}
+7 -4
View File
@@ -2836,13 +2836,16 @@ pub async fn port_forward_test(
let mut buf = vec![0; buf_size as usize];
rand::thread_rng().fill(&mut buf[..]);
// Allow the UDP connector's three-second handshake deadline to complete.
// Echo and shutdown retain their separate one-second deadlines.
let connect_timeout = Duration::from_secs(5);
_tunnel_pingpong_netns_with_timeout(
tcp_listener,
tcp_connector,
NetNS::new(Some(network.namespace("net_c").into())),
NetNS::new(Some(network.namespace("net_a").into())),
buf,
Duration::from_secs(1),
connect_timeout,
)
.await
.unwrap();
@@ -2859,7 +2862,7 @@ pub async fn port_forward_test(
NetNS::new(Some(network.namespace("net_d").into())),
NetNS::new(Some(network.namespace("net_a").into())),
buf,
Duration::from_secs(1),
connect_timeout,
)
.await
.unwrap();
@@ -2876,7 +2879,7 @@ pub async fn port_forward_test(
NetNS::new(Some(network.namespace("net_c").into())),
NetNS::new(Some(network.namespace("net_a").into())),
buf,
Duration::from_secs(1),
connect_timeout,
)
.await
.unwrap();
@@ -2893,7 +2896,7 @@ pub async fn port_forward_test(
NetNS::new(Some(network.namespace("net_d").into())),
NetNS::new(Some(network.namespace("net_a").into())),
buf,
Duration::from_secs(1),
connect_timeout,
)
.await
.unwrap();