【问题标题】:Can a Haskell or Haskell OS thread waiting on Network.Socket.accept not be killed on Windows?在 Windows 上等待 Network.Socket.accept 的 Haskell 或 Haskell OS 线程能否被杀死?
【发布时间】:2012-05-23 10:17:58
【问题描述】:
-- thread A
t <- forkIO $ do
   _ <- accept listener -- blocks

-- thread B
killThread t

适用于 Linux(可能也适用于 OS X 和 FreeBSD),但不适用于 Windows(尝试使用 +RTS -N4 -RTS 等进行线程化)。

  • 在这种情况下终止线程A 的正确方法是什么?
  • 有没有办法以特殊模式分叉线程 A,允许在 accept 上阻塞时终止?
  • 如果A 与forkOS 而不是forkIO 分叉会有帮助吗?

只有在bug report 发出警报时,我才注意到这种异常的 Windows 行为。

【问题讨论】:

标签: multithreading exception networking haskell asynchronous


【解决方案1】:

有趣的问题!

你不能中断阻塞的外部调用,所以我有点惊讶你能够中断 Linux 上的线程。此外,forkOS 也无济于事——它只是让外部代码分配线程本地存储,但与阻塞行为无关。但请记住,accept 可以设置为非阻塞:

如果队列中没有挂起的连接,并且套接字是 未标记为非阻塞,accept() 阻塞调用者,直到 存在连接。如果套接字被标记为非阻塞并且没有 队列中存在挂起的连接,accept() 失败并显示 EAGAIN 或 EWOULDBLOCK 错误。

in the Network library for Posix systems. 是做什么的,这会允许accept 被中断。

关于 Windows 的有趣说明:

-- On Windows, our sockets are not put in non-blocking mode (non-blocking
-- is not supported for regular file descriptors on Windows, and it would
-- be a pain to support it only for sockets).  So there are two cases:
--
--  - the threaded RTS uses safe calls for socket operations to get
--    non-blocking I/O, just like the rest of the I/O library
--
--  - with the non-threaded RTS, only some operations on sockets will be
--    non-blocking.  Reads and writes go through the normal async I/O
--    system.  accept() uses asyncDoProc so is non-blocking.  A handful
--    of others (recvFrom, sendFd, recvFd) will block all threads - if this
--    is a problem, -threaded is the workaround.

现在,在 Windows 上接受 -thread 运行时,使用 accept_safe(它允许其他线程取得进展)——但它不会将套接字置于非阻塞模式:

accept sock@(MkSocket s family stype protocol status) = do
 currentStatus <- readMVar status
 okay <- sIsAcceptable sock
 if not okay
   then
     ioError (userError ("accept: can't perform accept on socket (" ++ (show (family,stype,protocol)) ++") in status " ++
     show currentStatus))
   else do
     let sz = sizeOfSockAddrByFamily family
     allocaBytes sz $ \ sockaddr -> do

#if defined(mingw32_HOST_OS) && defined(__GLASGOW_HASKELL__)
     new_sock <-
    if threaded
       then with (fromIntegral sz) $ \ ptr_len ->
          throwErrnoIfMinus1Retry "Network.Socket.accept" $
            c_accept_safe s sockaddr ptr_len
       else do
            paramData <- c_newAcceptParams s (fromIntegral sz) sockaddr
            rc        <- asyncDoProc c_acceptDoProc paramData
            new_sock  <- c_acceptNewSock    paramData
            c_free paramData
            when (rc /= 0)
                 (ioError (errnoToIOError "Network.Socket.accept" (Errno (fromIntegral rc)) Nothing Nothing))
        return new_sock

自 2005 年以来,network 包的版本在带有 -thread 的 Windows 上显式使用标记为 safe 的接受调用,允许其他线程取得进展,但不将套接字本身设置为非阻塞模式(所以调用线程阻塞)。

要解决它,我看到两个选项:

  • 找出如何在 Windows 上进行非阻塞接受调用,并修补网络库——看看例如什么snap 或 yesod 在这里做,看看他们是否已经解决了。
  • 使用某种监督线程来伪造 epoll,监控被阻塞的子线程的进度。

【讨论】:

  • 哇!! 感谢您的代码,但我找到了一个很好的快速修复。我看到使用非线程运行时,他们正在执行asyncDoProc 调用,这些调用可能会被中断:) 所以在 Windows 上,我将默认使用该运行时,直到找到更好的解决方案。
  • 是的,我们需要在 Windows 上默认开启非阻塞接受。
  • 我还将确认并发布在这种特殊情况下我是否真的可以在 Linux 上中断accept。
  • 我刚刚检查并确认accept 在 Linux 上是可中断的(OS X 和 FreeBSD 可能与 Linux 相同)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 2015-04-22
相关资源
最近更新 更多