work around ghc segfault

hSetEncoding of a closed handle segfaults.
https://ghc.haskell.org/trac/ghc/ticket/7161

8484c0c197 introduced the crash.
In particular, stdin may get closed (by eg, getContents) and then trying
to set its encoding will crash. We didn't need to adjust stdin's
encoding anyway, but only stderr, to work around
https://github.com/yesodweb/persistent/issues/474

Thanks to Mesar Hameed for assistance related to reproducing this bug.
This commit is contained in:
Joey Hess 2016-12-30 18:14:19 -04:00
parent 1c744b9512
commit 23d71423e1
No known key found for this signature in database
GPG key ID: C910D9222512E3C7
2 changed files with 9 additions and 1 deletions

View file

@ -69,7 +69,7 @@ openDb db tablename = do
worker <- async (workerThread (T.pack db) tablename jobs)
-- work around https://github.com/yesodweb/persistent/issues/474
liftIO useFileSystemEncoding
liftIO $ fileEncoding stderr
return $ DbHandle worker jobs

View file

@ -10,6 +10,7 @@
module Utility.FileSystemEncoding (
useFileSystemEncoding,
fileEncoding,
withFilePath,
md5FilePath,
decodeBS,
@ -63,6 +64,13 @@ useFileSystemEncoding = do
hSetEncoding stderr e
Encoding.setLocaleEncoding e
fileEncoding :: Handle -> IO ()
#ifndef mingw32_HOST_OS
fileEncoding h = hSetEncoding h =<< Encoding.getFileSystemEncoding
#else
fileEncoding h = hSetEncoding h Encoding.utf8
#endif
{- Marshal a Haskell FilePath into a NUL terminated C string using temporary
- storage. The FilePath is encoded using the filesystem encoding,
- reversing the decoding that should have been done when the FilePath