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

Ouverte
#390 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
57/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Domaine
backend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Haskell
Étoiles
91
Forks
93
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de haskell/process

Toutes les issues de haskell/process

Issues similaires

Plus d'issues Haskell

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.