From: Adrián Arroyo Calle Date: Wed, 29 Sep 2021 22:10:03 +0000 (+0200) Subject: use shutdown error values to throw exceptions in close stream X-Git-Tag: v0.9.0~40^2~3 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=47d811c25099906d3dcd89ea8004dc3df06ad894;p=scryer-prolog.git use shutdown error values to throw exceptions in close stream --- diff --git a/src/machine/streams.rs b/src/machine/streams.rs index d765d881..e1773685 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -160,20 +160,6 @@ impl StreamInstance { } } -impl Drop for StreamInstance { - fn drop(&mut self) { - match self { - StreamInstance::TcpStream(_, ref mut tcp_stream) => { - tcp_stream.shutdown(Shutdown::Both).unwrap_or(()) - } - StreamInstance::TlsStream(_, ref mut tls_stream) => { - tls_stream.shutdown().unwrap_or(()); - } - _ => {} - } - } -} - impl fmt::Debug for StreamInstance { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self { @@ -596,8 +582,18 @@ impl Stream { } #[inline] - pub(crate) fn close(&mut self) { + pub(crate) fn close(&mut self) -> Result<(), std::io::Error> { + let result = match self.stream_inst.0.borrow_mut().stream_inst { + StreamInstance::TcpStream(_, ref mut tcp_stream) => { + tcp_stream.shutdown(Shutdown::Both) + }, + StreamInstance::TlsStream(_, ref mut tls_stream) => { + tls_stream.shutdown() + } + _ => Ok(()) + }; self.stream_inst.0.borrow_mut().stream_inst = StreamInstance::Null; + result } #[inline] @@ -636,20 +632,6 @@ impl Stream { } } - pub(crate) fn is_closed(&self) -> bool { - match self.stream_inst.0.borrow_mut().stream_inst { - StreamInstance::Null => true, - StreamInstance::TcpStream(_, ref mut tcp_stream) => { - let mut buf = [0;8]; - match tcp_stream.peek(&mut buf) { - Ok(n_bytes) => n_bytes == 0, - Err(_) => true - } - }, - _ => false - } - } - fn unpause_stream(&mut self) { let stream_inst = match self.stream_inst.0.borrow_mut().stream_inst { StreamInstance::PausedPrologStream(ref put_back, ref mut stream_inst) diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index e14a530f..cd02c750 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -2704,7 +2704,12 @@ impl MachineState { } if !stream.is_stdin() && !stream.is_stdout() && !stream.is_stderr() { - if stream.is_closed() { + let close_result = stream.close(); + + if let Some(ref alias) = stream.options().alias { + indices.stream_aliases.remove(alias); + } + if let Err(_) = close_result { let stub = MachineError::functor_stub( clause_name!("close"), 1, @@ -2721,12 +2726,6 @@ impl MachineState { ), stub, )); - } else { - stream.close(); - - if let Some(ref alias) = stream.options().alias { - indices.stream_aliases.remove(alias); - } } } }