Skip to content

Commit 9b00a97

Browse files
committed
fix: reorder struct fields to ensure proper cleanup order
Rust drops struct fields in declaration order (top to bottom). Reordered LinuxJail fields so cleanup happens in reverse order of creation: 1. DNS server stopped (explicit in Drop::drop) 2. netns_resolv cleaned (unmount bind-mount, remove /etc/netns dir) 3. nftables cleaned (remove firewall rules) 4. veth_pair cleaned (delete veth pair) 5. namespace cleaned (delete network namespace) This ensures the bind-mount unmount happens BEFORE the namespace is deleted, making cleanup more robust and predictable. All 23 tests pass on ml-1 ✓
1 parent e06724b commit 9b00a97

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

src/jail/linux/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,17 @@ pub fn format_ip(ip: [u8; 4]) -> String {
6161
/// Provides complete network isolation without persistent system state
6262
pub struct LinuxJail {
6363
config: JailConfig,
64-
namespace: Option<ManagedResource<NetworkNamespace>>,
65-
veth_pair: Option<ManagedResource<VethPair>>,
66-
nftables: Option<ManagedResource<NFTable>>,
64+
// IMPORTANT: Field order matters! Rust drops fields in declaration order (top to bottom).
65+
// We want cleanup to happen in reverse order of creation:
66+
// 1. DNS server stopped (explicit in Drop::drop)
67+
// 2. netns_resolv cleaned (unmount bind-mount, remove /etc/netns dir)
68+
// 3. nftables cleaned (remove firewall rules)
69+
// 4. veth_pair cleaned (delete veth pair)
70+
// 5. namespace cleaned (delete network namespace)
6771
netns_resolv: Option<ManagedResource<NetnsResolv>>,
72+
nftables: Option<ManagedResource<NFTable>>,
73+
veth_pair: Option<ManagedResource<VethPair>>,
74+
namespace: Option<ManagedResource<NetworkNamespace>>,
6875
// Host-side DNS server for DNAT redirection
6976
host_dns_server: Option<dns::DummyDnsServer>,
7077
// Per-jail computed networking (unique /30 inside 10.99/16)
@@ -80,10 +87,10 @@ impl LinuxJail {
8087
Self::compute_subnet_for_jail(&config.jail_id);
8188
Ok(Self {
8289
config,
83-
namespace: None,
84-
veth_pair: None,
85-
nftables: None,
8690
netns_resolv: None,
91+
nftables: None,
92+
veth_pair: None,
93+
namespace: None,
8794
host_dns_server: None,
8895
host_ip,
8996
host_cidr,
@@ -602,10 +609,10 @@ impl Clone for LinuxJail {
602609
// system resources that shouldn't be duplicated
603610
Self {
604611
config: self.config.clone(),
605-
namespace: None,
606-
veth_pair: None,
607-
nftables: None,
608612
netns_resolv: None,
613+
nftables: None,
614+
veth_pair: None,
615+
namespace: None,
609616
host_dns_server: None,
610617
host_ip: self.host_ip,
611618
host_cidr: self.host_cidr.clone(),

0 commit comments

Comments
 (0)