closeCommunicationHandle hangs on Windows if the child is already reading

Aberta
#389 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
48/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
haskell
Domínio
backend

Direção de pesquisa

Start by tracing closeCommunicationHandle in System.Process.CommunicationHandle and how it closes the inherited Windows handle; the issue identifies fdToHandle, mkFD, and fdStat as the path to inspect. Reproduce the hang with the supplied CloseRepro.hs program on Windows, then check that closing the parent's copy does not block while the child is reading and the program reaches done.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

On Windows, closeCommunicationHandle blocks for as long as the child process has a read pending on the
handle it inherited. The documentation recommends closing the parent's copy after spawning the child, and
readCreateProcessWithExitCodeCommunicationHandle does the same, so a child that starts reading quickly
can deadlock with its parent.

This prints done on Linux and hangs on Windows (default I/O manager; did not try WinIO):

-- ghc -threaded CloseRepro.hs
import Control.Concurrent
import qualified Data.ByteString as B
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 <- openCommunicationHandleRead (read ch)
    B.hGetSome h 1 >>= print
  _ -> do
    (theyRead, weWrite) <- createTheyReadWeWritePipe
    self <- getExecutablePath
    ph <- spawnProcess self ["child", show theyRead]
    threadDelay 1000000                -- let the child start its read, otherwise it's a race.
    closeCommunicationHandle theyRead  -- hangs on Windows
    B.hPut weWrite (B.pack [120])
    hClose weWrite
    _ <- waitForProcess ph
    putStrLn "done"

Without the threadDelay the child can still win the race on a busy enough machine, e.g. GHC's CI.

From what I can tell, using an LLM to trace through calls, closeCommunicationHandle turns the HANDLE into a Handle before closing it, and fdToHandle calls _fstati64 (via mkFD and fdStat). The parent's copy and the child's are the same synchronous pipe, and there _fstati64 waits for the child's ReadFile to finish. Trying the calls one at a time: _fstati64 and PeekNamedPipe block, while GetFileType and CloseHandle do not. The child is waiting for the parent to write, so we get a deadlock. Because fdStat imports _fstati64 as unsafe,
every other Haskell thread in the parent stops as well.

Calling CloseHandle on the raw HANDLE avoids this, and is how I'm working around it in commit 76c49a91c44801ad9e4c9f5f6f957f4e589c964c of ghc.

Linguagem predominante
Haskell
Estrelas
91
Forks
93
Métricas de merge de PRs
Nenhum PR com merge em 30d

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de haskell/process

Todas as issues de haskell/process

Issues semelhantes

Mais issues de Haskell

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.