close channel when exception happens

Fix for the #1044 issue