Skip to content

Commit becaf6c

Browse files
committed
add sock timeout check
1 parent 03d0223 commit becaf6c

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

core/src/syscall/unix/connect.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,22 @@ impl<I: ConnectSyscall> ConnectSyscall for NioConnectSyscall<I> {
6363
set_non_blocking(fd);
6464
}
6565
let start_time = now();
66+
let mut left_time = send_time_limit(fd);
6667
let mut r = self.inner.connect(fn_ptr, fd, address, len);
67-
while start_time
68-
.saturating_add(send_time_limit(fd))
69-
.saturating_sub(now()) > 0
70-
{
68+
while left_time > 0 {
7169
if r == 0 {
7270
reset_errno();
7371
break;
7472
}
7573
let errno = Error::last_os_error().raw_os_error();
7674
if errno == Some(libc::EINPROGRESS) || errno == Some(libc::EALREADY) || errno == Some(libc::EWOULDBLOCK) {
7775
//阻塞,直到写事件发生
78-
if EventLoops::wait_write_event(fd, Some(crate::common::constants::SLICE)).is_err()
76+
left_time = start_time
77+
.saturating_add(send_time_limit(fd))
78+
.saturating_sub(now());
79+
let wait_time = std::time::Duration::from_nanos(left_time)
80+
.min(crate::common::constants::SLICE);
81+
if EventLoops::wait_write_event(fd, Some(wait_time)).is_err()
7982
{
8083
break;
8184
}

core/src/syscall/unix/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ macro_rules! impl_facade {
3333
$crate::error!("{} change to running state failed !", co.name());
3434
}
3535
}
36-
$crate::info!("exit syscall {} {:?}", syscall, r);
36+
$crate::info!("exit syscall {} {:?} {}", syscall, r, std::io::Error::last_os_error());
3737
r
3838
}
3939
}
@@ -259,7 +259,13 @@ macro_rules! impl_nio_read_iovec {
259259
}
260260
let start_time = $crate::common::now();
261261
let mut left_time = $crate::syscall::common::recv_time_limit($fd);
262-
let vec = unsafe { Vec::from_raw_parts($iov.cast_mut(), $iovcnt as usize, $iovcnt as usize) };
262+
let vec = unsafe {
263+
Vec::from_raw_parts(
264+
$iov.cast_mut(),
265+
$iovcnt as usize,
266+
$iovcnt as usize
267+
)
268+
};
263269
let mut length = 0;
264270
let mut received = 0usize;
265271
let mut r = 0;
@@ -441,7 +447,13 @@ macro_rules! impl_nio_write_iovec {
441447
}
442448
let start_time = $crate::common::now();
443449
let mut left_time = $crate::syscall::common::send_time_limit($fd);
444-
let vec = unsafe { Vec::from_raw_parts($iov.cast_mut(), $iovcnt as usize, $iovcnt as usize) };
450+
let vec = unsafe {
451+
Vec::from_raw_parts(
452+
$iov.cast_mut(),
453+
$iovcnt as usize,
454+
$iovcnt as usize
455+
)
456+
};
445457
let mut length = 0;
446458
let mut sent = 0usize;
447459
let mut r = 0;

core/src/syscall/windows/connect.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use crate::common::now;
12
use crate::net::EventLoops;
2-
use crate::syscall::common::{is_blocking, reset_errno, set_blocking, set_errno, set_non_blocking};
3+
use crate::syscall::common::{is_blocking, reset_errno, set_blocking, set_errno, set_non_blocking, send_time_limit};
34
use once_cell::sync::Lazy;
45
use std::ffi::c_int;
56
use std::io::Error;
@@ -49,18 +50,25 @@ impl<I: ConnectSyscall> ConnectSyscall for NioConnectSyscall<I> {
4950
if blocking {
5051
set_non_blocking(fd);
5152
}
53+
let start_time = now();
54+
let mut left_time = send_time_limit(fd);
5255
let mut r = self.inner.connect(fn_ptr, fd, address, len);
53-
loop {
56+
while left_time > 0 {
5457
if r == 0 {
5558
reset_errno();
5659
break;
5760
}
5861
let errno = Error::last_os_error().raw_os_error();
5962
if errno == Some(WSAEINPROGRESS) || errno == Some(WSAEALREADY) || errno == Some(WSAEWOULDBLOCK) {
6063
//阻塞,直到写事件发生
64+
left_time = start_time
65+
.saturating_add(send_time_limit(fd))
66+
.saturating_sub(now());
67+
let wait_time = std::time::Duration::from_nanos(left_time)
68+
.min(crate::common::constants::SLICE);
6169
if EventLoops::wait_write_event(
6270
fd as _,
63-
Some(crate::common::constants::SLICE)
71+
Some(wait_time)
6472
).is_err() {
6573
break;
6674
}

core/src/syscall/windows/mod.rs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ macro_rules! impl_facade {
3636
$crate::error!("{} change to running state failed !", co.name());
3737
}
3838
}
39-
$crate::info!("exit syscall {} {:?}", syscall, r);
39+
$crate::info!("exit syscall {} {:?} {}", syscall, r, std::io::Error::last_os_error());
4040
r
4141
}
4242
}
@@ -63,8 +63,9 @@ macro_rules! impl_nio_read {
6363
$crate::syscall::common::set_non_blocking($fd);
6464
}
6565
let start_time = $crate::common::now();
66-
let mut r;
67-
loop {
66+
let mut left_time = $crate::syscall::common::recv_time_limit($fd);
67+
let mut r = 0;
68+
while left_time > 0 {
6869
r = self.inner.$syscall(fn_ptr, $fd, $($arg, )*);
6970
if r != -1 as _ {
7071
$crate::syscall::common::reset_errno();
@@ -73,9 +74,10 @@ macro_rules! impl_nio_read {
7374
let error_kind = std::io::Error::last_os_error().kind();
7475
if error_kind == std::io::ErrorKind::WouldBlock {
7576
//wait read event
76-
let wait_time = std::time::Duration::from_nanos(start_time
77+
left_time = start_time
7778
.saturating_add($crate::syscall::common::recv_time_limit($fd))
78-
.saturating_sub($crate::common::now()))
79+
.saturating_sub($crate::common::now());
80+
let wait_time = std::time::Duration::from_nanos(left_time)
7981
.min($crate::common::constants::SLICE);
8082
if $crate::net::EventLoops::wait_read_event(
8183
$fd as _,
@@ -119,9 +121,10 @@ macro_rules! impl_nio_read_buf {
119121
$crate::syscall::common::set_non_blocking($fd);
120122
}
121123
let start_time = $crate::common::now();
124+
let mut left_time = $crate::syscall::common::recv_time_limit($fd);
122125
let mut received = 0;
123126
let mut r = 0;
124-
while received < $len {
127+
while received < $len && left_time > 0 {
125128
r = self.inner.$syscall(
126129
fn_ptr,
127130
$fd,
@@ -140,9 +143,10 @@ macro_rules! impl_nio_read_buf {
140143
let error_kind = std::io::Error::last_os_error().kind();
141144
if error_kind == std::io::ErrorKind::WouldBlock {
142145
//wait read event
143-
let wait_time = std::time::Duration::from_nanos(start_time
146+
left_time = start_time
144147
.saturating_add($crate::syscall::common::recv_time_limit($fd))
145-
.saturating_sub($crate::common::now()))
148+
.saturating_sub($crate::common::now());
149+
let wait_time = std::time::Duration::from_nanos(left_time)
146150
.min($crate::common::constants::SLICE);
147151
if $crate::net::EventLoops::wait_read_event(
148152
$fd as _,
@@ -187,8 +191,15 @@ macro_rules! impl_nio_read_iovec {
187191
if blocking {
188192
$crate::syscall::common::set_non_blocking($fd);
189193
}
190-
let vec = unsafe { Vec::from_raw_parts($iov.cast_mut(), $iovcnt as usize, $iovcnt as usize) };
191194
let start_time = $crate::common::now();
195+
let mut left_time = $crate::syscall::common::recv_time_limit($fd);
196+
let vec = unsafe {
197+
Vec::from_raw_parts(
198+
$iov.cast_mut(),
199+
$iovcnt as usize,
200+
$iovcnt as usize
201+
)
202+
};
192203
let mut length = 0;
193204
let mut received = 0usize;
194205
let mut r = 0;
@@ -204,7 +215,7 @@ macro_rules! impl_nio_read_iovec {
204215
for i in vec.iter().skip(index) {
205216
arg.push(*i);
206217
}
207-
while received < length {
218+
while received < length && left_time > 0 {
208219
if 0 != offset {
209220
arg[0] = windows_sys::Win32::Networking::WinSock::WSABUF {
210221
buf: (arg[0].buf as usize + offset) as windows_sys::core::PSTR,
@@ -238,9 +249,10 @@ macro_rules! impl_nio_read_iovec {
238249
let error_kind = std::io::Error::last_os_error().kind();
239250
if error_kind == std::io::ErrorKind::WouldBlock {
240251
//wait read event
241-
let wait_time = std::time::Duration::from_nanos(start_time
252+
left_time = start_time
242253
.saturating_add($crate::syscall::common::recv_time_limit($fd))
243-
.saturating_sub($crate::common::now()))
254+
.saturating_sub($crate::common::now());
255+
let wait_time = std::time::Duration::from_nanos(left_time)
244256
.min($crate::common::constants::SLICE);
245257
if $crate::net::EventLoops::wait_read_event(
246258
$fd as _,
@@ -297,9 +309,10 @@ macro_rules! impl_nio_write_buf {
297309
$crate::syscall::common::set_non_blocking($fd);
298310
}
299311
let start_time = $crate::common::now();
312+
let mut left_time = $crate::syscall::common::send_time_limit($fd);
300313
let mut sent = 0;
301314
let mut r = 0;
302-
while sent < $len {
315+
while sent < $len && left_time > 0 {
303316
r = self.inner.$syscall(
304317
fn_ptr,
305318
$fd,
@@ -318,9 +331,10 @@ macro_rules! impl_nio_write_buf {
318331
let error_kind = std::io::Error::last_os_error().kind();
319332
if error_kind == std::io::ErrorKind::WouldBlock {
320333
//wait write event
321-
let wait_time = std::time::Duration::from_nanos(start_time
334+
left_time = start_time
322335
.saturating_add($crate::syscall::common::send_time_limit($fd))
323-
.saturating_sub($crate::common::now()))
336+
.saturating_sub($crate::common::now());
337+
let wait_time = std::time::Duration::from_nanos(left_time)
324338
.min($crate::common::constants::SLICE);
325339
if $crate::net::EventLoops::wait_write_event(
326340
$fd as _,
@@ -365,8 +379,15 @@ macro_rules! impl_nio_write_iovec {
365379
if blocking {
366380
$crate::syscall::common::set_non_blocking($fd);
367381
}
368-
let vec = unsafe { Vec::from_raw_parts($iov.cast_mut(), $iovcnt as usize, $iovcnt as usize) };
369382
let start_time = $crate::common::now();
383+
let mut left_time = $crate::syscall::common::send_time_limit($fd);
384+
let vec = unsafe {
385+
Vec::from_raw_parts(
386+
$iov.cast_mut(),
387+
$iovcnt as usize,
388+
$iovcnt as usize
389+
)
390+
};
370391
let mut length = 0;
371392
let mut sent = 0usize;
372393
let mut r = 0;
@@ -382,7 +403,7 @@ macro_rules! impl_nio_write_iovec {
382403
for i in vec.iter().skip(index) {
383404
arg.push(*i);
384405
}
385-
while sent < length {
406+
while sent < length && left_time > 0 {
386407
if 0 != offset {
387408
arg[0] = windows_sys::Win32::Networking::WinSock::WSABUF {
388409
buf: (arg[0].buf as usize + offset) as windows_sys::core::PSTR,
@@ -410,9 +431,10 @@ macro_rules! impl_nio_write_iovec {
410431
let error_kind = std::io::Error::last_os_error().kind();
411432
if error_kind == std::io::ErrorKind::WouldBlock {
412433
//wait write event
413-
let wait_time = std::time::Duration::from_nanos(start_time
434+
left_time = start_time
414435
.saturating_add($crate::syscall::common::send_time_limit($fd))
415-
.saturating_sub($crate::common::now()))
436+
.saturating_sub($crate::common::now());
437+
let wait_time = std::time::Duration::from_nanos(left_time)
416438
.min($crate::common::constants::SLICE);
417439
if $crate::net::EventLoops::wait_write_event(
418440
$fd as _,

0 commit comments

Comments
 (0)