From 1c721cb20e44fdc7ec294792a9621d54361d344e Mon Sep 17 00:00:00 2001 From: kbleeke Date: Mon, 27 Mar 2023 13:39:41 +0200 Subject: [PATCH] cancel ioctl when future is dropped --- src/control.rs | 23 ++++++++++++++++++++--- src/ioctl.rs | 4 ++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/control.rs b/src/control.rs index 0dbf6d44..30d5d092 100644 --- a/src/control.rs +++ b/src/control.rs @@ -278,10 +278,27 @@ impl<'a> Control<'a> { } async fn ioctl(&mut self, kind: IoctlType, cmd: u32, iface: u32, buf: &mut [u8]) -> usize { - // TODO cancel ioctl on future drop. + struct CancelOnDrop<'a>(&'a IoctlState); + + impl CancelOnDrop<'_> { + fn defuse(self) { + core::mem::forget(self); + } + } + + impl Drop for CancelOnDrop<'_> { + fn drop(&mut self) { + self.0.cancel_ioctl(); + } + } + + let ioctl = CancelOnDrop(self.ioctl_state); + + ioctl.0.do_ioctl(kind, cmd, iface, buf).await; + let resp_len = ioctl.0.wait_complete().await; + + ioctl.defuse(); - self.ioctl_state.do_ioctl(kind, cmd, iface, buf).await; - let resp_len = self.ioctl_state.wait_complete().await; resp_len } } diff --git a/src/ioctl.rs b/src/ioctl.rs index 6a746559..f5ab410d 100644 --- a/src/ioctl.rs +++ b/src/ioctl.rs @@ -88,6 +88,10 @@ impl IoctlState { pending } + pub fn cancel_ioctl(&self) { + self.state.set(IoctlStateInner::Done { resp_len: 0 }); + } + pub async fn do_ioctl(&self, kind: IoctlType, cmd: u32, iface: u32, buf: &mut [u8]) -> usize { warn!("doing ioctl"); self.state