diff --git a/codex-rs/linux-sandbox/src/dns_routing.rs b/codex-rs/linux-sandbox/src/dns_routing.rs index 1470734f87..b3cfa0dcef 100644 --- a/codex-rs/linux-sandbox/src/dns_routing.rs +++ b/codex-rs/linux-sandbox/src/dns_routing.rs @@ -108,7 +108,9 @@ fn netns_udp_loop(socket: UdpSocket, endpoint: SocketAddr) -> io::Result<()> { let mut query = vec![0; MAX_DNS_MESSAGE_BYTES]; loop { let (len, peer) = socket.recv_from(&mut query)?; - let response = resolve_through_proxy(endpoint, &query[..len])?; + let Ok(response) = resolve_through_proxy(endpoint, &query[..len]) else { + continue; + }; socket.send_to(&response, peer)?; } } diff --git a/codex-rs/linux-sandbox/src/dns_routing_tests.rs b/codex-rs/linux-sandbox/src/dns_routing_tests.rs index 3a68f7bc9a..7cbdc1ffd7 100644 --- a/codex-rs/linux-sandbox/src/dns_routing_tests.rs +++ b/codex-rs/linux-sandbox/src/dns_routing_tests.rs @@ -1,7 +1,9 @@ use super::*; use pretty_assertions::assert_eq; use std::net::TcpListener; +use std::net::UdpSocket; use std::sync::mpsc; +use std::time::Duration; #[test] fn parses_loopback_dns_proxy_endpoint() { @@ -43,3 +45,58 @@ fn forwards_dns_query_to_private_proxy_session() { assert_eq!(received_rx.recv().expect("query"), b"dns-query"); assert_eq!(response, expected_response); } + +#[test] +fn udp_loop_continues_after_proxy_error() { + let dns_socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).expect("bind UDP DNS stub"); + dns_socket + .set_read_timeout(Some(Duration::from_millis(500))) + .expect("set DNS stub idle timeout"); + let dns_addr = dns_socket.local_addr().expect("DNS stub address"); + + let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).expect("bind DNS proxy fixture"); + let endpoint = listener.local_addr().expect("fixture local addr"); + let (received_tx, received_rx) = mpsc::channel(); + let expected_response = b"dns-response".to_vec(); + let response = expected_response.clone(); + std::thread::spawn(move || { + let _ = listener.accept().expect("accept failing DNS proxy session"); + + let (mut stream, _) = listener.accept().expect("accept DNS proxy session"); + let mut preface = [0_u8; DNS_PROXY_SESSION_PREFACE.len()]; + stream.read_exact(&mut preface).expect("read preface"); + assert_eq!(&preface, DNS_PROXY_SESSION_PREFACE); + stream + .write_all(DNS_PROXY_SESSION_PREFACE) + .expect("write preface ack"); + let query = read_frame(&mut stream).expect("read query"); + received_tx.send(query).expect("send query"); + write_frame(&mut stream, &response).expect("write response"); + }); + std::thread::spawn(move || { + let _ = netns_udp_loop(dns_socket, endpoint); + }); + + let client = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).expect("bind UDP client"); + client + .set_read_timeout(Some(Duration::from_millis(500))) + .expect("set UDP client timeout"); + client + .send_to(b"first-query", dns_addr) + .expect("send first query"); + let mut buffer = [0; 64]; + let _ = client.recv_from(&mut buffer); + + client + .send_to(b"second-query", dns_addr) + .expect("send second query"); + let (len, _) = client.recv_from(&mut buffer).expect("receive DNS response"); + + assert_eq!( + received_rx + .recv_timeout(Duration::from_secs(1)) + .expect("query"), + b"second-query" + ); + assert_eq!(&buffer[..len], expected_response); +}