为什么“mask_”会抵消“timeout”?
我终于能够跟踪一个奇怪的错误,我遇到了(至少对我来说)mask
和 timeout
之间令人惊讶的交互:
import System.Timeout
import Control.Exception
ack :: Int -> Int -> Int
ack m n | m == 0, n >= 0 = n + 1
| m > 0, n == 0 = ack (m - 1) 1
| m > 0, n > 0 = ack (m - 1) (ack m (n - 1))
tryack :: Int -> Int -> IO (Maybe Int)
tryack m n = timeout 100000 {- uS -} $ evaluate $ ack m n
main :: IO ()
main = do
a <- tryack 3 11
print a -- Nothing
b <- mask_ $ tryack 3 11
print b -- Just 16381 after a few seconds
这让我觉得这是一个相当“非-组合”交互,因为这意味着如果库内部使用超时
,则调用链上某处外部应用的掩码
可能会导致库发生故障。
那么这是超时实现中的(已知)缺陷还是故意的?
I finally was able to track a weird bug I was having down to the (at least to me) surprising interaction between mask
and timeout
:
import System.Timeout
import Control.Exception
ack :: Int -> Int -> Int
ack m n | m == 0, n >= 0 = n + 1
| m > 0, n == 0 = ack (m - 1) 1
| m > 0, n > 0 = ack (m - 1) (ack m (n - 1))
tryack :: Int -> Int -> IO (Maybe Int)
tryack m n = timeout 100000 {- uS -} $ evaluate $ ack m n
main :: IO ()
main = do
a <- tryack 3 11
print a -- Nothing
b <- mask_ $ tryack 3 11
print b -- Just 16381 after a few seconds
This strikes me as a rather "non-compositional" interaction, as it means that if a library internally uses timeout
, an externally applied mask
somewhere up the call-chain may cause the library to malfunction.
So is this a (known) deficiency in the implementation of timeout
or is it intentional?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mask_
做什么?超时
做什么?
所以...
mask_
正在阻止timeout
传递异常。就是这样。您只是不能使用
mask
并让timeout
起作用。也许更好的方法是使用处理程序来捕获除
timeout
使用的异常之外的任何内容?What does
mask_
do?And what does
timeout
do?So...
mask_
is preventingtimeout
from delivering its exceptions. That's just how it is.You just can't use
mask
and havetimeout
work.Perhaps a better approach would be to use a handler to catch anything but the exception that
timeout
uses?