Skip to content

Commit 3144253

Browse files
committed
Switch to the new operations for obtaining OS handles
1 parent 98101f8 commit 3144253

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

System/Process/Common.hs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ import Data.String ( IsString(..) )
4545
import Foreign.Ptr
4646
import 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
4852
import System.Posix.Internals
4953
import GHC.IO.Exception
5054
import GHC.IO.Encoding
5155
import qualified GHC.IO.FD as FD
5256
import GHC.IO.Device
5357
#if defined(__IO_MANAGER_WINIO__)
54-
import GHC.IO.Handle.Windows
5558
import GHC.IO.Windows.Handle (fromHANDLE, Io(), NativeHandle())
5659
#endif
5760
import GHC.IO.Handle.FD
@@ -260,23 +263,29 @@ mbFd :: String -> FD -> StdStream -> IO FD
260263
mbFd _ _std CreatePipe = return (-1)
261264
mbFd _fun std Inherit = return std
262265
mbFd _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

281290
mbPipe :: StdStream -> Ptr FD -> IOMode -> IO (Maybe Handle)
282291
mbPipe CreatePipe pfd mode = fmap Just (pfdToHandle pfd mode)
@@ -317,7 +326,7 @@ mbHANDLE :: HANDLE -> StdStream -> IO HANDLE
317326
mbHANDLE _std CreatePipe = return $ intPtrToPtr (-1)
318327
mbHANDLE std Inherit = return std
319328
mbHANDLE _std NoStream = return $ intPtrToPtr (-2)
320-
mbHANDLE _std (UseHandle hdl) = handleToHANDLE hdl
329+
mbHANDLE _std (UseHandle hdl) = withWindowsHandleReadingBiasedRaw hdl $ return
321330

322331
mbPipeHANDLE :: StdStream -> Ptr HANDLE -> IOMode -> IO (Maybe Handle)
323332
mbPipeHANDLE CreatePipe pfd mode =

0 commit comments

Comments
 (0)