From f520c38b6014a577dc9011097037b9eb9d0fd437 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 27 Mar 2022 13:08:43 -0600 Subject: [PATCH] Stream::close() should close file handles (#1374) --- src/machine/streams.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 8808313a..f305ae04 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -988,18 +988,35 @@ impl Stream { #[inline] pub(crate) fn close(&mut self) -> Result<(), std::io::Error> { - let result = match self { + let mut stream = std::mem::replace(self, Stream::Null(StreamOptions::default())); + + match stream { Stream::NamedTcp(ref mut tcp_stream) => { tcp_stream.inner_mut().tcp_stream.shutdown(Shutdown::Both) }, Stream::NamedTls(ref mut tls_stream) => { tls_stream.inner_mut().tls_stream.shutdown() } - _ => Ok(()) - }; + Stream::InputFile(mut file_stream) => { + // close the stream by dropping the inner File. + unsafe { + file_stream.set_tag(ArenaHeaderTag::Dropped); + std::ptr::drop_in_place(&mut file_stream.inner_mut().file as *mut _); + } - *self = Stream::Null(StreamOptions::default()); - result + Ok(()) + } + Stream::OutputFile(mut file_stream) => { + // close the stream by dropping the inner File. + unsafe { + file_stream.set_tag(ArenaHeaderTag::Dropped); + std::ptr::drop_in_place(&mut file_stream.file as *mut _); + } + + Ok(()) + } + _ => Ok(()) + } } #[inline] -- 2.54.0