diff --git a/monoio-rustls/src/safe_io.rs b/monoio-rustls/src/safe_io.rs index 11b196a..b6b33bf 100644 --- a/monoio-rustls/src/safe_io.rs +++ b/monoio-rustls/src/safe_io.rs @@ -104,6 +104,11 @@ impl Default for SafeRead { impl SafeRead { pub(crate) async fn do_io(&mut self, mut io: IO) -> io::Result { + if self.buffer.is_none() { + // if called do_io after the time async read/write had been cancelled, + // buffer will be none, so we return Other error. + return Err(io::ErrorKind::Other.into()); + } // if there are some data inside the buffer, just return. let buffer = self.buffer.as_ref().expect("buffer ref expected"); if !buffer.is_empty() { @@ -134,6 +139,11 @@ impl SafeRead { impl io::Read for SafeRead { fn read(&mut self, buf: &mut [u8]) -> io::Result { + if self.buffer.is_none() { + // if called read after the time async read/write had been cancelled, + // buffer will be none, so we return Other error. + return Err(io::ErrorKind::Other.into()); + } // if buffer is empty, return WoundBlock. let buffer = self.buffer.as_mut().expect("buffer mut expected"); if buffer.is_empty() { @@ -187,6 +197,11 @@ impl Default for SafeWrite { impl SafeWrite { pub(crate) async fn do_io(&mut self, mut io: IO) -> io::Result { + if self.buffer.is_none() { + // if called do_io after the time async read/write had been cancelled, + // buffer will be none, so we return Other error. + return Err(io::ErrorKind::Other.into()); + } // if the buffer is empty, just return. let buffer = self.buffer.as_ref().expect("buffer ref expected"); if buffer.is_empty() { @@ -213,6 +228,11 @@ impl SafeWrite { impl io::Write for SafeWrite { fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buffer.is_none() { + // if called write after the time async read/write had been cancelled, + // buffer will be none, so we return Other error. + return Err(io::ErrorKind::Other.into()); + } // if there is too much data inside the buffer, return WoundBlock let buffer = self.buffer.as_mut().expect("buffer mut expected"); if !matches!(self.status, WriteStatus::Ok) { @@ -233,6 +253,11 @@ impl io::Write for SafeWrite { } fn flush(&mut self) -> io::Result<()> { + if self.buffer.is_none() { + // if called flush after the time async read/write had been cancelled, + // buffer will be none, so we return Other error. + return Err(io::ErrorKind::Other.into()); + } let buffer = self.buffer.as_mut().expect("buffer mut expected"); if !matches!(self.status, WriteStatus::Ok) { match std::mem::replace(&mut self.status, WriteStatus::Ok) {