Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in handleToFd, fixes issue #4 #222

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions System/Posix/IO/Common.hsc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE Trustworthy #-}

-----------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -73,7 +73,10 @@ import GHC.IO.Handle.Types
import qualified GHC.IO.FD as FD
import qualified GHC.IO.Handle.FD as FD
import GHC.IO.Exception
import GHC.IO.Buffer
import GHC.IORef
import Data.Typeable (cast)
import System.IO.Unsafe (unsafePerformIO)

#if !defined(HAVE_PIPE)
import System.IO.Error ( ioeSetLocation )
Expand Down Expand Up @@ -301,10 +304,26 @@ handleToFd' h h_@Handle__{haType=_,..} = do
-- converting a Handle into an Fd effectively means
-- letting go of the Handle; it is put into a closed
-- state as a result.
flushWriteBuffer h_

-- free the spare buffers
writeIORef haBuffers BufferListNil
writeIORef haCharBuffer noCharBuffer
writeIORef haByteBuffer noByteBuffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change lose any unwritten data in the write buffer? It used to be flushed, but now is just dropped?

With hClose this happens before the call to hClose_handle_ handle_ (after which the above code is modelled):

hClose_help :: Handle__ -> IO (Handle__, Maybe SomeException)
hClose_help handle_ =
  case haType handle_ of
      ClosedHandle -> return (handle_,Nothing)
      _ -> do mb_exc1 <- trymaybe $ flushWriteBuffer handle_ -- interruptible
                    -- it is important that hClose doesn't fail and
                    -- leave the Handle open (#3128), so we catch
                    -- exceptions when flushing the buffer.
              (h_, mb_exc2) <- hClose_handle_ handle_
              return (h_, if isJust mb_exc1 then mb_exc1 else mb_exc2)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not too sure about this change. I think it's not necessary for 2.8.0.0, since people have lived with it for over a decade.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the memory leak can be safely avoided, I'm inclined to fix anyway, but replacing flush with just dropping the buffer is not correct. Otherwise, I don't see any issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hs-viktor I'm not too familiar with this part of the code. Feel free to adjust this PR.


-- release our encoder/decoder
closeTextCodecs h_

FD.release fd
return (Handle__{haType=ClosedHandle,..}, Fd (FD.fdFD fd))

{-# NOINLINE noCharBuffer #-}
noCharBuffer :: CharBuffer
noCharBuffer = unsafePerformIO $ newCharBuffer 1 ReadBuffer

{-# NOINLINE noByteBuffer #-}
noByteBuffer :: Buffer Word8
noByteBuffer = unsafePerformIO $ newByteBuffer 1 ReadBuffer


-- -----------------------------------------------------------------------------
-- Fd options
Expand Down
2 changes: 2 additions & 0 deletions cabal.project.wasm32-wasi
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ package unix
ghc-options: -Wno-unused-imports

write-ghc-environment-files: always

allow-newer: all:base