Skip to content

Commit

Permalink
io: Do not panic in Drop impls
Browse files Browse the repository at this point in the history
We do the memory unmapping in the Mmap Drop impl itself now, so we
can get rid of the Arena Drop impl completely.

In the Stream Drop impl, we ignore any errors returned by stop().

Signed-off-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
raymanfx committed May 9, 2024
1 parent a55ac7c commit a3ea986
Showing 1 changed file with 2 additions and 41 deletions.
43 changes: 2 additions & 41 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use crate::io::traits::{CaptureStream, OutputStream, Stream as StreamTrait};
use crate::memory::{Memory, Mmap, UserPtr};
use crate::v4l2;

/// Manage mapped buffers
///
/// All buffers are unmapped in the Drop impl.
/// In case of errors during unmapping, we panic because there is memory corruption going on.
/// Manage memory buffers
pub(crate) struct Arena<T> {
handle: Arc<Handle>,
pub bufs: Vec<Buffer<T>>,
Expand Down Expand Up @@ -49,30 +46,6 @@ impl<T> Arena<T> {
}
}

impl<T> Drop for Arena<T> {
fn drop(&mut self) {
if self.bufs.is_empty() {
// nothing to do
return;
}

// free all buffers by requesting 0
if let Err(e) = self.request(0) {
if let Some(code) = e.raw_os_error() {
// ENODEV means the file descriptor wrapped in the handle became invalid, most
// likely because the device was unplugged or the connection (USB, PCI, ..)
// broke down. Handle this case gracefully by ignoring it.
if code == 19 {
/* ignore */
return;
}
}

panic!("{:?}", e)
}
}
}

impl<'a> Arena<Mmap<'a>> {
/// Returns a new buffer manager instance
///
Expand Down Expand Up @@ -300,19 +273,7 @@ impl<T> Stream<T> {

impl<T> Drop for Stream<T> {
fn drop(&mut self) {
if let Err(e) = self.stop() {
if let Some(code) = e.raw_os_error() {
// ENODEV means the file descriptor wrapped in the handle became invalid, most
// likely because the device was unplugged or the connection (USB, PCI, ..)
// broke down. Handle this case gracefully by ignoring it.
if code == 19 {
/* ignore */
return;
}
}

panic!("{:?}", e)
}
let _ = self.stop();
}
}

Expand Down

0 comments on commit a3ea986

Please sign in to comment.