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

Aperta
#390 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Haskell
Stelle
91
Fork
93
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di haskell/process

Tutte le issue di haskell/process

Issue simili

Altre issue su Haskell

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.