diff options
-rw-r--r-- | dht/examples/dhtd.hs | 15 | ||||
-rw-r--r-- | dht/src/Network/QueryResponse.hs | 15 |
2 files changed, 22 insertions, 8 deletions
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs index 6bbb3388..287301d4 100644 --- a/dht/examples/dhtd.hs +++ b/dht/examples/dhtd.hs | |||
@@ -1773,8 +1773,10 @@ main = do | |||
1773 | (waitForSignal, checkQuit) <- do | 1773 | (waitForSignal, checkQuit) <- do |
1774 | signalQuit <- atomically $ newTVar False | 1774 | signalQuit <- atomically $ newTVar False |
1775 | let quitCommand = atomically $ writeTVar signalQuit True | 1775 | let quitCommand = atomically $ writeTVar signalQuit True |
1776 | installHandler sigTERM (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing | 1776 | installHandler sigTERM (CatchOnce (do dput XMisc "sigTERM!" |
1777 | installHandler sigINT (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing | 1777 | atomically $ writeTVar signalQuit True)) Nothing |
1778 | installHandler sigINT (CatchOnce (do dput XMisc "sigINT!" | ||
1779 | atomically $ writeTVar signalQuit True)) Nothing | ||
1778 | let defaultToxData = do | 1780 | let defaultToxData = do |
1779 | rster <- Tox.newContactInfo | 1781 | rster <- Tox.newContactInfo |
1780 | crypto <- newCrypto | 1782 | crypto <- newCrypto |
@@ -1848,6 +1850,13 @@ main = do | |||
1848 | quitBt | 1850 | quitBt |
1849 | quitTox | 1851 | quitTox |
1850 | 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 | |||
1851 | swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms) | 1860 | swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms) |
1852 | forM_ (Map.toList dhts) $ \(netname,dht) -> do | 1861 | forM_ (Map.toList dhts) $ \(netname,dht) -> do |
1853 | saveNodes netname dht | 1862 | saveNodes netname dht |
@@ -1855,5 +1864,7 @@ main = do | |||
1855 | L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb | 1864 | L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb |
1856 | dput XMisc $ "Saved bt-peers.dat" | 1865 | dput XMisc $ "Saved bt-peers.dat" |
1857 | 1866 | ||
1867 | -- threadDelay 1000000 | ||
1868 | |||
1858 | s <- threadReport False | 1869 | s <- threadReport False |
1859 | putStrLn s | 1870 | putStrLn s |
diff --git a/dht/src/Network/QueryResponse.hs b/dht/src/Network/QueryResponse.hs index 27134470..8f3c078d 100644 --- a/dht/src/Network/QueryResponse.hs +++ b/dht/src/Network/QueryResponse.hs | |||
@@ -200,11 +200,11 @@ forkListener :: String -> Transport err addr x -> IO (IO ()) | |||
200 | forkListener name client = do | 200 | forkListener name client = do |
201 | thread_id <- forkIO $ do | 201 | thread_id <- forkIO $ do |
202 | myThreadId >>= flip labelThread ("listener."++name) | 202 | myThreadId >>= flip labelThread ("listener."++name) |
203 | fix $ awaitMessage client . const | 203 | fix $ \loop -> awaitMessage client $ maybe (return ()) (const loop) |
204 | dput XMisc $ "Listener died: " ++ name | 204 | dput XMisc $ "Listener died: " ++ name |
205 | return $ do | 205 | return $ do |
206 | closeTransport client | 206 | closeTransport client |
207 | killThread thread_id | 207 | -- killThread thread_id |
208 | 208 | ||
209 | asyncQuery_ :: Client err meth tid addr x | 209 | asyncQuery_ :: Client err meth tid addr x |
210 | -> MethodSerializer tid addr x meth a b | 210 | -> MethodSerializer tid addr x meth a b |
@@ -560,6 +560,7 @@ ignoreEOF sock isClosed def e = do | |||
560 | done <- tryReadMVar isClosed | 560 | done <- tryReadMVar isClosed |
561 | case done of | 561 | case done of |
562 | Just () -> do close sock | 562 | Just () -> do close sock |
563 | dput XMisc "Closing UDP socket." | ||
563 | pure Nothing | 564 | pure Nothing |
564 | _ -> if isEOFError e then pure $ Just def | 565 | _ -> if isEOFError e then pure $ Just def |
565 | else throwIO e | 566 | else throwIO e |
@@ -584,11 +585,11 @@ saferSendTo sock bs saddr = void (B.sendTo sock bs saddr) | |||
584 | -- argument is the listen-address for incoming packets. This is a useful | 585 | -- argument is the listen-address for incoming packets. This is a useful |
585 | -- low-level 'Transport' that can be transformed for higher-level protocols | 586 | -- low-level 'Transport' that can be transformed for higher-level protocols |
586 | -- using 'layerTransport'. | 587 | -- using 'layerTransport'. |
587 | udpTransport :: SockAddr -> IO (Transport err SockAddr ByteString) | 588 | udpTransport :: Show err => SockAddr -> IO (Transport err SockAddr ByteString) |
588 | udpTransport bind_address = fst <$> udpTransport' bind_address | 589 | udpTransport bind_address = fst <$> udpTransport' bind_address |
589 | 590 | ||
590 | -- | Like 'udpTransport' except also returns the raw socket (for broadcast use). | 591 | -- | Like 'udpTransport' except also returns the raw socket (for broadcast use). |
591 | udpTransport' :: SockAddr -> IO (Transport err SockAddr ByteString, Socket) | 592 | udpTransport' :: Show err => SockAddr -> IO (Transport err SockAddr ByteString, Socket) |
592 | udpTransport' bind_address = do | 593 | udpTransport' bind_address = do |
593 | let family = sockAddrFamily bind_address | 594 | let family = sockAddrFamily bind_address |
594 | sock <- socket family Datagram defaultProtocol | 595 | sock <- socket family Datagram defaultProtocol |
@@ -601,6 +602,7 @@ udpTransport' bind_address = do | |||
601 | awaitMessage = \kont -> do | 602 | awaitMessage = \kont -> do |
602 | r <- handle (ignoreEOF sock isClosed $ Right (B.empty, SockAddrInet 0 0)) $ do | 603 | r <- handle (ignoreEOF sock isClosed $ Right (B.empty, SockAddrInet 0 0)) $ do |
603 | Just . Right <$!> B.recvFrom sock udpBufferSize | 604 | Just . Right <$!> B.recvFrom sock udpBufferSize |
605 | dput XMisc $ "udp.awaitMessage.recvFrom: " ++ show r | ||
604 | kont $! r | 606 | kont $! r |
605 | , sendMessage = case family of | 607 | , sendMessage = case family of |
606 | AF_INET6 -> \case | 608 | AF_INET6 -> \case |
@@ -618,8 +620,8 @@ udpTransport' bind_address = do | |||
618 | addr4 -> \bs -> saferSendTo sock bs addr4 | 620 | addr4 -> \bs -> saferSendTo sock bs addr4 |
619 | _ -> \addr bs -> saferSendTo sock bs addr | 621 | _ -> \addr bs -> saferSendTo sock bs addr |
620 | , closeTransport = do | 622 | , closeTransport = do |
621 | -- close sock | 623 | dput XMisc $ "closeTransport for udpTransport' called. " ++ show bind_address |
622 | tryTakeMVar isClosed >> putMVar isClosed () | 624 | tryPutMVar isClosed () |
623 | -- set O_NONBLOCK using fcntl | 625 | -- set O_NONBLOCK using fcntl |
624 | -- NOTE: setNonBlockIfNeeded is a no-op on windows | 626 | -- NOTE: setNonBlockIfNeeded is a no-op on windows |
625 | #if MIN_VERSION_network (3,1,0) | 627 | #if MIN_VERSION_network (3,1,0) |
@@ -627,6 +629,7 @@ udpTransport' bind_address = do | |||
627 | #else | 629 | #else |
628 | setNonBlockIfNeeded (fdSocket sock) -- setSocketOption sock ??? 0 | 630 | setNonBlockIfNeeded (fdSocket sock) -- setSocketOption sock ??? 0 |
629 | #endif | 631 | #endif |
632 | -- shutdown sock ShutdownBoth `catchIOError` \_ -> return () | ||
630 | } | 633 | } |
631 | return (tr, sock) | 634 | return (tr, sock) |
632 | 635 | ||