CommunicationHandle pipes block every thread in the non-threaded RTS on Windows

未关闭
#390 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
57/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
backend

调研方向

Start in createCommunicationPipe, where the issue says Windows builds always use rawHANDLEToHandle; compare its runtime selection with createProcess and createPipe. Reproduce the delay with the supplied non-threaded program on Windows, then check the result with the default I/O manager and +RTS --io-manager=native. Done means the default non-threaded RTS lets threadDelay return before the child writes.

由索引模型根据 Issue 内容生成。

描述

GHC defines __IO_MANAGER_WINIO__ for every Windows build, so createCommunicationPipe always gives the
parent a native Handle made by rawHANDLEToHandle, even when the program runs with the default I/O
manager. The comment there says this is fine without WinIO at runtime. That holds with -threaded. In
the non-threaded RTS, though, reading from that Handle is a blocking ReadFile on a synchronous
HANDLE. That RTS runs every Haskell thread on one OS thread, so they all stop until data arrives. An fd
Handle does not have this problem, because in the non-threaded RTS its reads go through asyncRead#
and only the reading thread waits.

Here the child writes after 3s. On Linux, and on Windows with +RTS --io-manager=native, the
threadDelay returns after about 1s. On Windows with the default I/O manager it returns only after the
child has written:

-- ghc NonThreadedRepro.hs   (without -threaded)
import Control.Concurrent
import qualified Data.ByteString as B
import GHC.Clock (getMonotonicTime)
import System.Environment
import System.IO
import System.Process
import System.Process.CommunicationHandle

main :: IO ()
main = getArgs >>= \args -> case args of
  ["child", ch] -> do
    h <- openCommunicationHandleWrite (read ch)
    threadDelay 3000000
    B.hPut h (B.pack [120])
    hClose h
  _ -> do
    (weRead, theyWrite) <- createWeReadTheyWritePipe
    self <- getExecutablePath
    ph <- spawnProcess self ["child", show theyWrite]
    closeCommunicationHandle theyWrite
    t0 <- getMonotonicTime
    _ <- forkIO (B.hGetSome weRead 1 >>= print)
    yield
    threadDelay 1000000
    t1 <- getMonotonicTime
    putStrLn ("threadDelay 1000000 returned after " ++ show (t1 - t0) ++ "s")
    _ <- waitForProcess ph
    return ()

createProcess and createPipe already pick an fd Handle or a native one at runtime with <!>. Doing
the same in createCommunicationPipe should fix this:

ourHandle <- ((\fd -> rawFdToHandle fd ourMode) =<< openHANDLE ourHANDLE)
               <!> rawHANDLEToHandle ourHANDLE ourMode

Worked around this by swapping in an fd Handle here.

主要语言
Haskell
星标
91
派生
93
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

haskell/process 的其他 Issue

查看 haskell/process 的全部 Issue

相似的 Issue

更多 Haskell Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。