Killing a thread waiting for hGetLine from a socket (Windows)

If I run the following Java program on a Windows (10) machine (I am posting the program here for completeness):

public class IdleServer {
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket serverSock = new ServerSocket(9090);

        Socket sock = serverSock.accept();

        while (true) Thread.sleep(1000);
    }
}

and then connect to the Java server from Haskell:

readWithinNSecs :: IO ()
readWithinNSecs = withSocketsDo $ do
  h <- connectTo "localhost" (PortNumber 9090)
  hSetBuffering h NoBuffering
  readerTid <- forkIO $ reader h
  threadDelay $ 2 * 10^6
  putStrLn "Killing the reader"
  killThread readerTid
  putStrLn "Reader thread killed"
  where
    reader h = do
      line <- strip <$> hGetLine h
      putStrLn $ "Got " ++ line

then the Haskell program will freeze when trying to kill a process that it is trying to read from the handle. Killing a Java server will cause the Haskell program to terminate, which will also result in an error:

<socket: 380>: hGetLine: failed (No error)

If I run the same two programs on Linux, the Haskell program exits.

This seems to indicate an error in the baseHaskell libraries , so I reported this , but in the meantime I need to find a way around this. Any ideas?

. , : , .

import           Control.Concurrent.Async

-- ...

readerAsync h = do
  a <- async $ strip <$> hGetLine h
  line <- wait a
  putStrLn $ "Got " ++ line
+4

Source: https://habr.com/ru/post/1689884/


All Articles