From: Matt Mullins Date: Sat, 4 May 2013 06:37:19 +0000 (-0700) Subject: Close Channel on irc_amqp_listener termination. X-Git-Tag: v10~6 X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=bbe3789804b100542b1df0b248bcf16b111e71d5;p=erlbot.git Close Channel on irc_amqp_listener termination. This fixes a bug wherein application:stop(irc) would leave a dangling Channel that was consuming messages on a queue -- long after the process to which it would deliver is dead. That would cause the AMQP channel to exit with reason unexpected_delivery_and_no_default_consumer, killing all of the rest of the AMQP client and, by the transitive property of death, the irc application as well. The irc_amqp_listener must trap exits in order to receive a termination notice from its supervisor. I'm not actually sure if this has any implications for it monitoring its AMQP Channel or not. --- diff --git a/irc/irc_amqp_listener.erl b/irc/irc_amqp_listener.erl index f615dc3..fe4fc7f 100644 --- a/irc/irc_amqp_listener.erl +++ b/irc/irc_amqp_listener.erl @@ -16,7 +16,8 @@ -export([ init/1, handle_info/2, - code_change/3 + code_change/3, + terminate/2 ]). -include("irc_util.hrl"). @@ -26,6 +27,7 @@ start_link(ConnectionPid, RoutingKey) -> gen_server:start_link(?MODULE, {ConnectionPid, RoutingKey}, []). init({ConnectionPid, RoutingKey}) -> + process_flag(trap_exit, true), {ok, _, ListeningChannel} = amqp_bot_listener:listen_for_events(RoutingKey), {ok, {ConnectionPid, ListeningChannel}}. @@ -59,6 +61,10 @@ code_change(2, {ConnectionPid}, _) -> code_change({down, 2}, {ConnectionPid, _ListeningChannel}, _) -> {ok, {ConnectionPid}}. +terminate(Reason, {ConnectionPid, ListeningChannel}) -> + error_logger:warning_msg("Tearing down channel ~p for reason ~p", [ListeningChannel, Reason]), + amqp_channel:close(ListeningChannel). + %% @doc Sends a message with the given routing key and body to the proper %% exchange send_message(RoutingKey, ReplyTo, Body) -> diff --git a/irc/irc_object_sup.erl b/irc/irc_object_sup.erl index 055de24..dd4b44a 100644 --- a/irc/irc_object_sup.erl +++ b/irc/irc_object_sup.erl @@ -22,7 +22,7 @@ add_amqp_listener(ObjectSup, ConnectionPid, RoutingKey) -> Child = {"amqp_" ++ RoutingKey, {irc_amqp_listener, start_link, [ConnectionPid, RoutingKey]}, transient, - brutal_kill, + 200, worker, [irc_amqp_listener] },