
Folks, I have a thread that launches socket reader and writer threads. My intent is to notify the main thread when an exception is caught by the reader/writer. Is this code proper? --- withException :: (Event a -> IO ()) -> IO () -> IO Bool withException post action = handle (\e -> do case e of AsyncException ThreadKilled -> return () _ -> post $ NetworkError e return False ) $ do { action; return True } writeLoop :: (Event a -> IO ()) -> Handle -> (SSL, BIO, BIO) -> IO () writeLoop post h ssl = do go <- withException post $ do cmd <- read h ssl post $! Cmd $! cmd when go $ writeLoop post h ssl --- Thanks, Joel -- http://wagerlabs.com/

Hello Joel, your code is unreadable, inefficent and don't handle async exceptions between calls to `handle`. as i already sayed you'd better to use: handle (...) repeat_forever do cmd <- read h ssl post $! Cmd $! cmd Monday, December 12, 2005, 12:30:27 PM, you wrote: JR> Folks, JR> I have a thread that launches socket reader and writer threads. My JR> intent is to notify the main thread when an exception is caught by JR> the reader/writer. Is this code proper? JR> --- JR> withException :: (Event a -> IO ()) JR> -> IO () JR> -> IO Bool JR> withException post action = JR> handle (\e -> do case e of JR> AsyncException ThreadKilled -> return () JR> _ -> post $ NetworkError e JR> return False JR> ) $ do { action; return True } JR> writeLoop :: (Event a -> IO ()) -> Handle -> (SSL, BIO, BIO) -> IO () JR> writeLoop post h ssl = JR> do go <- withException post $ JR> do cmd <- read h ssl JR> post $! Cmd $! cmd JR> when go $ writeLoop post h ssl JR> --- JR> Thanks, Joel JR> -- JR> http://wagerlabs.com/ JR> _______________________________________________ JR> Haskell-Cafe mailing list JR> Haskell-Cafe@haskell.org JR> http://www.haskell.org/mailman/listinfo/haskell-cafe -- Best regards, Bulat mailto:bulatz@HotPOP.com

Bulat, I would welcome suggestions for improvements. Please let me know how my code could be made more efficient and more readable, keeping in mind what I'm trying to accomplish. Notice that my logic is different from the one that you are proposing. What I want is to handle exceptions in the forever loop _and_ terminate the forever loop when an exception is posted. The only reason I have the exception handler is that I want to post a message when the exception is received, unless it's a KillThread exception. Unless I'm mistaken, the code above will run forever and will not exit on exception. I use the code like this: tid1 <- forkIO $ writeLoop post h ssl `finally` finalize sock_lock ssl_lock but indeed no message will be posted if an exception happens between the calls to handle and things will just be silently finalized. I'm not sure how to have it both ways here. On Dec 12, 2005, at 11:29 AM, Bulat Ziganshin wrote:
Hello Joel,
your code is unreadable, inefficent and don't handle async exceptions between calls to `handle`. as i already sayed you'd better to use:
handle (...) repeat_forever do cmd <- read h ssl post $! Cmd $! cmd

Hello Joel, Monday, December 12, 2005, 7:26:23 PM, you wrote: JR> Unless I'm mistaken, the code above will run forever and will not JR> exit on exception. yes, you are muistaken! :) this code will repeat permanently until exception arrived. at this time it will process exception handler and then exit the whole function. you musr reread docs. hmm, actually this is the way exception handling works in ANY language
handle (...) repeat_forever do cmd <- read h ssl post $! Cmd $! cmd
JR> -- JR> http://wagerlabs.com/ -- Best regards, Bulat mailto:bulatz@HotPOP.com

Yes, you are right. I will make the change. On Dec 13, 2005, at 9:28 AM, Bulat Ziganshin wrote:
yes, you are muistaken! :) this code will repeat permanently until exception arrived. at this time it will process exception handler and then exit the whole function. you musr reread docs. hmm, actually this is the way exception handling works in ANY language
handle (...) repeat_forever do cmd <- read h ssl post $! Cmd $! cmd
participants (2)
-
Bulat Ziganshin
-
Joel Reymont