summaryrefslogtreecommitdiff
path: root/dht
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-10-18 04:22:15 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:49:41 -0500
commit9273a01aac621f09b0c00292b55153d558b4a4e0 (patch)
tree7f762b56741b59b052878c29b02e42b9dbd33f36 /dht
parente02b3a26895565c492e96b75c7348f3d625b2ba8 (diff)
Use GHC's closeFdWith to un-block recvFrom.
Diffstat (limited to 'dht')
-rw-r--r--dht/examples/dhtd.hs9
-rw-r--r--dht/src/Network/QueryResponse.hs17
2 files changed, 8 insertions, 18 deletions
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs
index 287301d4..eb41c598 100644
--- a/dht/examples/dhtd.hs
+++ b/dht/examples/dhtd.hs
@@ -1850,13 +1850,6 @@ main = do
1850 quitBt 1850 quitBt
1851 quitTox 1851 quitTox
1852 1852
1853 -- dput XMisc "Raising sigINT"
1854 -- raiseSignal sigINT
1855 -- dput XMisc "Raising sigTERM"
1856 -- raiseSignal sigTERM -- This shouldn't cause a termination because the signal handler is still installed.
1857 -- -- However, it will interrupt any dangling calls to recvFrom so that those threads
1858 -- -- can clean up.
1859
1860 swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms) 1853 swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms)
1861 forM_ (Map.toList dhts) $ \(netname,dht) -> do 1854 forM_ (Map.toList dhts) $ \(netname,dht) -> do
1862 saveNodes netname dht 1855 saveNodes netname dht
@@ -1864,7 +1857,5 @@ main = do
1864 L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb 1857 L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb
1865 dput XMisc $ "Saved bt-peers.dat" 1858 dput XMisc $ "Saved bt-peers.dat"
1866 1859
1867 -- threadDelay 1000000
1868
1869 s <- threadReport False 1860 s <- threadReport False
1870 putStrLn s 1861 putStrLn s
diff --git a/dht/src/Network/QueryResponse.hs b/dht/src/Network/QueryResponse.hs
index 8f3c078d..4f956936 100644
--- a/dht/src/Network/QueryResponse.hs
+++ b/dht/src/Network/QueryResponse.hs
@@ -32,6 +32,7 @@ import qualified Data.Word64Map as W64Map
32 ;import Data.Word64Map (Word64Map) 32 ;import Data.Word64Map (Word64Map)
33import Data.Word 33import Data.Word
34import Data.Maybe 34import Data.Maybe
35import GHC.Conc (closeFdWith)
35import GHC.Event 36import GHC.Event
36import Network.Socket 37import Network.Socket
37import Network.Socket.ByteString as B 38import Network.Socket.ByteString as B
@@ -602,7 +603,6 @@ udpTransport' bind_address = do
602 awaitMessage = \kont -> do 603 awaitMessage = \kont -> do
603 r <- handle (ignoreEOF sock isClosed $ Right (B.empty, SockAddrInet 0 0)) $ do 604 r <- handle (ignoreEOF sock isClosed $ Right (B.empty, SockAddrInet 0 0)) $ do
604 Just . Right <$!> B.recvFrom sock udpBufferSize 605 Just . Right <$!> B.recvFrom sock udpBufferSize
605 dput XMisc $ "udp.awaitMessage.recvFrom: " ++ show r
606 kont $! r 606 kont $! r
607 , sendMessage = case family of 607 , sendMessage = case family of
608 AF_INET6 -> \case 608 AF_INET6 -> \case
@@ -621,15 +621,14 @@ udpTransport' bind_address = do
621 _ -> \addr bs -> saferSendTo sock bs addr 621 _ -> \addr bs -> saferSendTo sock bs addr
622 , closeTransport = do 622 , closeTransport = do
623 dput XMisc $ "closeTransport for udpTransport' called. " ++ show bind_address 623 dput XMisc $ "closeTransport for udpTransport' called. " ++ show bind_address
624 tryPutMVar isClosed () 624 tryPutMVar isClosed () -- signal awaitMessage that the transport is closed.
625 -- set O_NONBLOCK using fcntl 625#if !MIN_VERSION_network (3,1,0)
626 -- NOTE: setNonBlockIfNeeded is a no-op on windows 626 let withFdSocket sock f = f (fdSocket sock)
627#if MIN_VERSION_network (3,1,0)
628 withFdSocket sock $ \fd -> setNonBlockIfNeeded fd
629#else
630 setNonBlockIfNeeded (fdSocket sock) -- setSocketOption sock ??? 0
631#endif 627#endif
632 -- shutdown sock ShutdownBoth `catchIOError` \_ -> return () 628 withFdSocket sock $ \fd -> do
629 let sorryGHCButIAmNotFuckingClosingTheSocketYet fd = return ()
630 -- This call is necessary to interrupt the blocking recvFrom call in awaitMessage.
631 closeFdWith sorryGHCButIAmNotFuckingClosingTheSocketYet (fromIntegral fd)
633 } 632 }
634 return (tr, sock) 633 return (tr, sock)
635 634