Skip to content

Commit a9f5c0c

Browse files
committed
AioCb::Drop will now panic for in-progress AioCb
Printing a warning message to stderr isn't really appropriate, because there's no way to guarantee that stderr is even valid. Nor is aio_suspend necessarily an appropriate action to take.
1 parent 318441d commit a9f5c0c

File tree

3 files changed

+14
-40
lines changed

3 files changed

+14
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Changed
99
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
10+
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#XXX](https://github.com/nix-rust/nix/pull/XXX))
1011

1112
## [0.9.0] 2017-07-23
1213

src/sys/aio.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use libc::{c_void, off_t, size_t};
44
use libc;
55
use std::fmt;
66
use std::fmt::Debug;
7-
use std::io::Write;
8-
use std::io::stderr;
97
use std::marker::PhantomData;
108
use std::mem;
119
use std::rc::Rc;
@@ -332,24 +330,8 @@ impl<'a> Debug for AioCb<'a> {
332330

333331
impl<'a> Drop for AioCb<'a> {
334332
/// If the `AioCb` has no remaining state in the kernel, just drop it.
335-
/// Otherwise, collect its error and return values, so as not to leak
336-
/// resources.
333+
/// Otherwise, dropping constitutes a resource leak, which is an error
337334
fn drop(&mut self) {
338-
if self.in_progress {
339-
// Well-written programs should never get here. They should always
340-
// wait for an AioCb to complete before dropping it
341-
let _ = write!(stderr(), "WARNING: dropped an in-progress AioCb");
342-
loop {
343-
let ret = aio_suspend(&[&self], None);
344-
match ret {
345-
Ok(()) => break,
346-
Err(Error::Sys(Errno::EINVAL)) => panic!(
347-
"Inconsistent AioCb.in_progress value"),
348-
Err(Error::Sys(Errno::EINTR)) => (), // Retry interrupted syscall
349-
_ => panic!("Unexpected aio_suspend return value {:?}", ret)
350-
};
351-
}
352-
let _ = self.aio_return();
353-
}
335+
assert!(!self.in_progress, "Dropped an in-progress AioCb");
354336
}
355337
}

test/sys/test_aio.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,27 +446,18 @@ fn test_lio_listio_read_immutable() {
446446
// Test dropping an AioCb that hasn't yet finished. Behind the scenes, the
447447
// library should wait for the AioCb's completion.
448448
#[test]
449+
#[should_panic(expected = "Dropped an in-progress AioCb")]
449450
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
450451
fn test_drop() {
451-
const INITIAL: &'static [u8] = b"abcdef123456";
452-
const WBUF: &'static [u8] = b"CDEF"; //"CDEF".to_string().into_bytes();
453-
let mut rbuf = Vec::new();
454-
const EXPECT: &'static [u8] = b"abCDEF123456";
455-
456-
let mut f = tempfile().unwrap();
457-
f.write(INITIAL).unwrap();
458-
{
459-
let mut aiocb = AioCb::from_slice( f.as_raw_fd(),
460-
2, //offset
461-
&WBUF,
462-
0, //priority
463-
SigevNotify::SigevNone,
464-
LioOpcode::LIO_NOP);
465-
aiocb.write().unwrap();
466-
}
452+
const WBUF: &'static [u8] = b"CDEF";
467453

468-
f.seek(SeekFrom::Start(0)).unwrap();
469-
let len = f.read_to_end(&mut rbuf).unwrap();
470-
assert!(len == EXPECT.len());
471-
assert!(rbuf == EXPECT);
454+
let f = tempfile().unwrap();
455+
f.set_len(6).unwrap();
456+
let mut aiocb = AioCb::from_slice( f.as_raw_fd(),
457+
2, //offset
458+
&WBUF,
459+
0, //priority
460+
SigevNotify::SigevNone,
461+
LioOpcode::LIO_NOP);
462+
aiocb.write().unwrap();
472463
}

0 commit comments

Comments
 (0)