Skip to content

Commit 650e68d

Browse files
authored
fix(driver): avoid leak if not consumed (#809)
* fix(driver): avoid leak if not consumed * docs(driver): comment on the changed layout
1 parent ccaebf7 commit 650e68d

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

compio-driver/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ rustdoc-args = ["--cfg", "docsrs"]
1818
compio-buf = { workspace = true }
1919
compio-log = { workspace = true }
2020

21+
compio-send-wrapper = { workspace = true }
22+
2123
# Utils
2224
cfg-if = { workspace = true }
2325
flume = { workspace = true, default-features = false }

compio-driver/src/key.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
};
1111

1212
use compio_buf::{BufResult, IntoInner};
13+
use compio_send_wrapper::SendWrapper;
1314
use thin_cell::unsync::{Inner, Ref, ThinCell};
1415

1516
use crate::{Carry, DriverType, Extra, OpCode, PushEntry, control::Carrier};
@@ -348,6 +349,7 @@ impl ErasedKey {
348349
pub(crate) unsafe fn freeze(self) -> FrozenKey {
349350
FrozenKey {
350351
inner: ManuallyDrop::new(self),
352+
thread_id: SendWrapper::new(()),
351353
}
352354
}
353355
}
@@ -360,10 +362,11 @@ impl Debug for ErasedKey {
360362

361363
/// A frozen view into a [`Key`].
362364
///
363-
/// It's guaranteed to have the same layout as [`ErasedKey`].
364-
#[repr(transparent)]
365+
/// It's guaranteed to have [`ErasedKey`] as the first field.
366+
#[repr(C)]
365367
pub(crate) struct FrozenKey {
366368
inner: ManuallyDrop<ErasedKey>,
369+
thread_id: SendWrapper<()>,
367370
}
368371

369372
impl FrozenKey {
@@ -372,7 +375,16 @@ impl FrozenKey {
372375
}
373376

374377
pub fn into_inner(self) -> ErasedKey {
375-
ManuallyDrop::into_inner(self.inner)
378+
let mut this = ManuallyDrop::new(self);
379+
unsafe { ManuallyDrop::take(&mut this.inner) }
380+
}
381+
}
382+
383+
impl Drop for FrozenKey {
384+
fn drop(&mut self) {
385+
if self.thread_id.valid() {
386+
unsafe { ManuallyDrop::drop(&mut self.inner) }
387+
}
376388
}
377389
}
378390

0 commit comments

Comments
 (0)