Permalink
Browse files

Fix resource handling in udpflow sample

Thanks to comments by Felipe @meteficha Lessa in the pull-request
review. Now strace does show the expected close calls.

See: #69 (comment)
  • Loading branch information...
1 parent 51d36c9 commit 9ec973431f9f821015ff72ff0b23100e8d692992 @NicolasT NicolasT committed Aug 29, 2012
Showing with 15 additions and 12 deletions.
  1. +15 −12 network-conduit/udpflow.hs
View
@@ -6,7 +6,8 @@ import Data.Conduit
import Data.Conduit.Network.UDP
import qualified Data.Conduit.List as CL
-import Control.Concurrent (forkIO, threadDelay)
+import Control.Concurrent (forkIO, killThread, threadDelay)
+import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Char8 ()
import Network.Socket (addrAddress, connect, sClose)
@@ -24,19 +25,21 @@ receiver = runResourceT $ src $$ CL.mapM_ (\_ -> return ())
(\sock -> sourceSocket sock 4096)
sender :: IO ()
-sender = do
- (sock, addr) <- getSocket localhost port
- connect sock (addrAddress addr)
-
- let sink = addCleanup
- (const $ sClose sock)
- (sinkSocket sock)
-
- CL.sourceList (repeat "abc") $$ sink
+sender = runResourceT $ CL.sourceList (repeat "abc") $$ sink
+ where
+ sink = bracketP
+ (getSocket localhost port)
+ (sClose . fst)
+ (\(sock, addr) -> do
+ liftIO $ connect sock (addrAddress addr)
+ sinkSocket sock)
main :: IO ()
main = do
- forkIO receiver
- forkIO sender
+ rt <- forkIO receiver
+ st <- forkIO sender
threadDelay $ 1000 * 1000 * 5
+
+ killThread st
+ killThread rt

0 comments on commit 9ec9734

Please sign in to comment.