fix(firezone-tunnel(windows)): don't panic if the sending ring buffer is full (#3544)

I never saw this replicate, but in theory it could happen. This PR just
drops packets while the ring buffer is full.

Closes #3518
This commit is contained in:
Reactor Scram
2024-02-02 15:49:05 -06:00
committed by GitHub
parent 6bb302b5d4
commit 079b4e7f9a

View File

@@ -222,14 +222,19 @@ impl Tun {
}
fn write(&self, bytes: &[u8]) -> io::Result<usize> {
// TODO: If the ring buffer is full, don't panic, just return Ok(None) or an error or whatever the Unix impls do.
// <https://github.com/firezone/firezone/issues/3518>
// Make sure this doesn't block.
let mut pkt = self
.session
.allocate_send_packet(bytes.len().try_into().unwrap())
.unwrap();
let len = bytes
.len()
.try_into()
.expect("Packet length should fit into u16");
let Ok(mut pkt) = self.session.allocate_send_packet(len) else {
// Ring buffer is full, just drop the packet since we're at the IP layer
return Ok(0);
};
pkt.bytes_mut().copy_from_slice(bytes);
// `send_packet` cannot fail to enqueue the packet, since we already allocated
// space in the ring buffer.
self.session.send_packet(pkt);
Ok(bytes.len())
}