hreinhardt/amqp

closeChannel results in an unhandled exception message

Closed this issue · 1 comments

Latest version of AMQP added printing of unhandled exceptions, which is a suboptiomal approach in the first place for custom applications that rely on custom structured logging, nevertheless this ticket is not about the approach itself.

This ticket is about the fact that calling closeChannel, which is a normal operation that can be called by a user, results in this scary error message printed on stderr:

unhandled AMQP channel exception (chanId=1): thread killed

Repro:

main :: IO ()
main = do
  conn <- openConnection'' defaultConnectionOpts
  chan <- openChannel conn
  putStrLn "Using channel ..."
  threadDelay 500000
  putStrLn "Closing channel ..."
  closeChannel chan
  putStrLn "Waiting ..."
  threadDelay 500000
  putStrLn "Exiting ..."

Results in:

Using channel ...
Closing channel ...
Waiting ...
unhandled AMQP channel exception (chanId=1): thread killed
Exiting ...

I understand that adding an exception handler to a channel removes this error being printed, but it seems like a bad default.

I suspect the proper solution to this problem is to throw a custom exception instead of ThreadKilled:

Just (_, thrID) -> killThread thrID
Nothing -> return ()

        case IM.lookup (fromIntegral $ channelID c) chans of
            Just (_, thrID) -> throwTo thrID $ ChannelClosedException Normal "closeChannel was called"

I wasn't sure that it would the desired solution, so I leave it here as a suggestion instead of an actual PR.

Argh, that's what I get for not thoroughly testing the change, since it looked so innocent.

Your proposal looks exactly correct. I've released a new version. Thanks for the report!