@@ -45,13 +45,16 @@ import Data.String ( IsString(..) )
4545import Foreign.Ptr
4646import Foreign.Storable ( Storable (peek ) )
4747
48+ import System.IO.OS (withFileDescriptorReadingBiasedRaw )
49+ #if defined(__IO_MANAGER_WINIO__)
50+ import System.IO.OS (withWindowsHandleReadingBiasedRaw )
51+ #endif
4852import System.Posix.Internals
4953import GHC.IO.Exception
5054import GHC.IO.Encoding
5155import qualified GHC.IO.FD as FD
5256import GHC.IO.Device
5357#if defined(__IO_MANAGER_WINIO__)
54- import GHC.IO.Handle.Windows
5558import GHC.IO.Windows.Handle (fromHANDLE , Io (), NativeHandle ())
5659#endif
5760import GHC.IO.Handle.FD
@@ -260,23 +263,29 @@ mbFd :: String -> FD -> StdStream -> IO FD
260263mbFd _ _std CreatePipe = return (- 1 )
261264mbFd _fun std Inherit = return std
262265mbFd _fn _std NoStream = return (- 2 )
263- mbFd fun _std (UseHandle hdl) =
264- withHandle fun hdl $ \ Handle__ {haDevice= dev,.. } -> do
265- case cast dev of
266- Just fd -> do
266+ mbFd fun _std (UseHandle hdl) = do
267+ setToBlockingIfPossible
268+ withFileDescriptorReadingBiasedRaw hdl $ return
269+ where
270+ setToBlockingIfPossible =
267271#if !defined(javascript_HOST_ARCH)
268- -- clear the O_NONBLOCK flag on this FD, if it is set, since
269- -- we're exposing it externally (see #3316)
270- fd' <- FD. setNonBlockingMode fd False
272+ -- clear the O_NONBLOCK flag on this FD, if it is set, since we're
273+ -- exposing it externally (see GHC issue #3316)
274+ withAllHandles__ fun hdl $ \ Handle__ {haDevice= dev,.. } -> do
275+ case cast dev of
276+ Just fd -> do
277+ fd' <- FD. setNonBlockingMode fd False
278+ return (Handle__ {haDevice= fd',.. })
279+ Nothing ->
280+ ioError (mkIOError illegalOperationErrorType
281+ " createProcess"
282+ (Just hdl)
283+ Nothing
284+ `ioeSetErrorString` " handle is not a file descriptor" )
271285#else
272- -- on the JavaScript platform we cannot change the FD flags
273- fd' <- pure fd
286+ -- on the JavaScript platform we cannot change the FD flags
287+ return ()
274288#endif
275- return (Handle__ {haDevice= fd',.. }, FD. fdFD fd')
276- Nothing ->
277- ioError (mkIOError illegalOperationErrorType
278- " createProcess" (Just hdl) Nothing
279- `ioeSetErrorString` " handle is not a file descriptor" )
280289
281290mbPipe :: StdStream -> Ptr FD -> IOMode -> IO (Maybe Handle )
282291mbPipe CreatePipe pfd mode = fmap Just (pfdToHandle pfd mode)
@@ -317,7 +326,7 @@ mbHANDLE :: HANDLE -> StdStream -> IO HANDLE
317326mbHANDLE _std CreatePipe = return $ intPtrToPtr (- 1 )
318327mbHANDLE std Inherit = return std
319328mbHANDLE _std NoStream = return $ intPtrToPtr (- 2 )
320- mbHANDLE _std (UseHandle hdl) = handleToHANDLE hdl
329+ mbHANDLE _std (UseHandle hdl) = withWindowsHandleReadingBiasedRaw hdl $ return
321330
322331mbPipeHANDLE :: StdStream -> Ptr HANDLE -> IOMode -> IO (Maybe Handle )
323332mbPipeHANDLE CreatePipe pfd mode =
0 commit comments