Skip to content

Commit 8b1caac

Browse files
committed
fix(io): handle interrupt error
1 parent d2c73db commit 8b1caac

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

compio-io/src/read/ext.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "allocator_api")]
22
use std::alloc::Allocator;
33

4-
use compio_buf::{buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut};
4+
use compio_buf::{vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut};
55

66
use crate::{util::Take, AsyncRead, AsyncReadAt, IoResult};
77

@@ -41,20 +41,25 @@ macro_rules! loop_read_exact {
4141
let len = $len;
4242

4343
while $tracker < len {
44-
($tracker, $buf) = buf_try!($read_expr.await.into_inner().and_then(|n, b| {
45-
if n == 0 {
46-
use ::std::io::{Error, ErrorKind};
47-
(
48-
Err(Error::new(
49-
ErrorKind::UnexpectedEof,
44+
match $read_expr.await.into_inner() {
45+
BufResult(Ok(0), buf) => {
46+
return BufResult(
47+
Err(::std::io::Error::new(
48+
::std::io::ErrorKind::UnexpectedEof,
5049
"failed to fill whole buffer",
5150
)),
52-
b,
53-
)
54-
} else {
55-
(Ok($tracker + n), b)
51+
buf,
52+
);
5653
}
57-
}));
54+
BufResult(Ok(n), buf) => {
55+
$tracker += n;
56+
$buf = buf;
57+
}
58+
BufResult(Err(ref e), buf) if e.kind() == ::std::io::ErrorKind::Interrupted => {
59+
$buf = buf;
60+
}
61+
res => return res,
62+
}
5863
}
5964
return BufResult(Ok($tracker), $buf)
6065
};
@@ -114,16 +119,23 @@ macro_rules! loop_read_vectored {
114119
macro_rules! loop_read_to_end {
115120
($buf:ident, $tracker:ident : $tracker_ty:ty,loop $read_expr:expr) => {{
116121
let mut $tracker: $tracker_ty = 0;
117-
let mut read;
118122
loop {
119123
if $buf.len() == $buf.capacity() {
120124
$buf.reserve(32);
121125
}
122-
(read, $buf) = buf_try!($read_expr.await.into_inner());
123-
if read == 0 {
124-
break;
125-
} else {
126-
$tracker += read as $tracker_ty;
126+
match $read_expr.await.into_inner() {
127+
BufResult(Ok(0), buf) => {
128+
$buf = buf;
129+
break;
130+
}
131+
BufResult(Ok(read), buf) => {
132+
$tracker += read as $tracker_ty;
133+
$buf = buf;
134+
}
135+
BufResult(Err(ref e), buf) if e.kind() == ::std::io::ErrorKind::Interrupted => {
136+
$buf = buf
137+
}
138+
res => return res,
127139
}
128140
}
129141
BufResult(Ok($tracker as usize), $buf)

compio-io/src/write/ext.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use compio_buf::{buf_try, BufResult, IntoInner, IoBuf, IoVectoredBuf};
1+
use compio_buf::{BufResult, IntoInner, IoBuf, IoVectoredBuf};
22

33
use crate::{AsyncWrite, AsyncWriteAt, IoResult};
44

@@ -40,18 +40,25 @@ macro_rules! loop_write_all {
4040
let mut $needle = 0;
4141

4242
while $needle < len {
43-
let n;
44-
(n, $buf) = buf_try!($expr_expr.await.into_inner());
45-
if n == 0 {
46-
return BufResult(
47-
Err(::std::io::Error::new(
48-
::std::io::ErrorKind::WriteZero,
49-
"failed to write whole buffer",
50-
)),
51-
$buf,
52-
);
43+
match $expr_expr.await.into_inner() {
44+
BufResult(Ok(0), buf) => {
45+
return BufResult(
46+
Err(::std::io::Error::new(
47+
::std::io::ErrorKind::WriteZero,
48+
"failed to write whole buffer",
49+
)),
50+
buf,
51+
);
52+
}
53+
BufResult(Ok(n), buf) => {
54+
$needle += n;
55+
$buf = buf;
56+
}
57+
BufResult(Err(ref e), buf) if e.kind() == ::std::io::ErrorKind::Interrupted => {
58+
$buf = buf;
59+
}
60+
res => return res,
5361
}
54-
$needle += n;
5562
}
5663

5764
return BufResult(Ok($needle), $buf);

0 commit comments

Comments
 (0)