From 0482f06b953b3b850b413a3187abf237874efc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 25 Mar 2025 22:12:57 +0100 Subject: [PATCH 1/2] Fix the exception logged by Cowboy caused by double reply (#13612) Issue introduced in 383ddb16341. Authored-by: @lhoguin. (cherry picked from commit 0a7c86b4807619b1ab52c18f091752d4f711d5b1) (cherry picked from commit dfe484be9e9254fbdd1696eebccf461aa5235154) # Conflicts: # deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl # deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl # deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl --- .../src/rabbit_mgmt_util.erl | 17 +++++++++++++++++ .../src/rabbit_mgmt_wm_exchange_publish.erl | 16 +++++++++++----- .../src/rabbit_mgmt_wm_queue_actions.erl | 16 +++++++++++----- .../src/rabbit_mgmt_wm_queue_get.erl | 16 +++++++++++----- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_util.erl b/deps/rabbitmq_management/src/rabbit_mgmt_util.erl index 557ac0433835..88946e6943f8 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_util.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_util.erl @@ -51,6 +51,8 @@ -export([disable_stats/1, enable_queue_totals/1]). +-export([set_resp_not_found/2]). + -import(rabbit_misc, [pget/2]). -include("rabbit_mgmt.hrl"). @@ -1178,3 +1180,18 @@ catch_no_such_user_or_vhost(Fun, Replacement) -> %% error is thrown when the request is out of range sublist(List, S, L) when is_integer(L), L >= 0 -> lists:sublist(lists:nthtail(S-1, List), L). + +-spec set_resp_not_found(binary(), cowboy_req:req()) -> cowboy_req:req(). +set_resp_not_found(NotFoundBin, ReqData) -> + ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of + not_found -> + <<"vhost_not_found">>; + _ -> + NotFoundBin + end, + ReqData1 = cowboy_req:set_resp_header( + <<"content-type">>, <<"application/json">>, ReqData), + cowboy_req:set_resp_body(rabbit_json:encode(#{ + <<"error">> => <<"not_found">>, + <<"reason">> => ErrorMessage + }), ReqData1). diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl index 9d2d6d711dde..15779566c758 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl @@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) -> content_types_provided(ReqData, Context) -> {rabbit_mgmt_util:responder_map(to_json), ReqData, Context}. -resource_exists(ReqData, Context) -> - {case rabbit_mgmt_wm_exchange:exchange(ReqData) of - not_found -> raise_not_found(ReqData, Context); - _ -> true - end, ReqData, Context}. +resource_exists(ReqData0, Context) -> + case rabbit_mgmt_wm_exchange:exchange(ReqData0) of + not_found -> + ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"exchange_not_found">>, ReqData0), + {false, ReqData1, Context}; + _ -> + {true, ReqData0, Context} + end. allow_missing_post(ReqData, Context) -> {false, ReqData, Context}. @@ -104,6 +107,7 @@ bad({{coordinator_unavailable, _}, _}, ReqData, Context) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). +<<<<<<< HEAD raise_not_found(ReqData, Context) -> ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of not_found -> @@ -116,6 +120,8 @@ raise_not_found(ReqData, Context) -> ReqData, Context). +======= +>>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- decode(Payload, <<"string">>) -> Payload; diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl index b749c6820305..ec802b69a5b7 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl @@ -25,11 +25,14 @@ variances(Req, Context) -> allowed_methods(ReqData, Context) -> {[<<"POST">>, <<"OPTIONS">>], ReqData, Context}. -resource_exists(ReqData, Context) -> - {case rabbit_mgmt_wm_queue:queue(ReqData) of - not_found -> raise_not_found(ReqData, Context); - _ -> true - end, ReqData, Context}. +resource_exists(ReqData0, Context) -> + case rabbit_mgmt_wm_queue:queue(ReqData0) of + not_found -> + ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0), + {false, ReqData1, Context}; + _ -> + {true, ReqData0, Context} + end. allow_missing_post(ReqData, Context) -> {false, ReqData, Context}. @@ -54,6 +57,7 @@ do_it(ReqData0, Context) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_admin(ReqData, Context). +<<<<<<< HEAD raise_not_found(ReqData, Context) -> ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of not_found -> @@ -65,6 +69,8 @@ raise_not_found(ReqData, Context) -> rabbit_data_coercion:to_binary(ErrorMessage), ReqData, Context). +======= +>>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- action(Else, _Q, ReqData, Context) -> diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl index db82f5a85348..93e4f97f4046 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl @@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) -> content_types_provided(ReqData, Context) -> {rabbit_mgmt_util:responder_map(to_json), ReqData, Context}. -resource_exists(ReqData, Context) -> - {case rabbit_mgmt_wm_queue:queue(ReqData) of - not_found -> raise_not_found(ReqData, Context); - _ -> true - end, ReqData, Context}. +resource_exists(ReqData0, Context) -> + case rabbit_mgmt_wm_queue:queue(ReqData0) of + not_found -> + ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0), + {false, ReqData1, Context}; + _ -> + {true, ReqData0, Context} + end. allow_missing_post(ReqData, Context) -> {false, ReqData, Context}. @@ -152,6 +155,7 @@ basic_get(Ch, Q, AckMode, Enc, Trunc) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). +<<<<<<< HEAD raise_not_found(ReqData, Context) -> ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of not_found -> @@ -163,6 +167,8 @@ raise_not_found(ReqData, Context) -> rabbit_data_coercion:to_binary(ErrorMessage), ReqData, Context). +======= +>>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- maybe_truncate(Payload, none) -> Payload; From fa5737cebc63d0a2340045e42cb60ef000f7f879 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Tue, 25 Mar 2025 21:36:35 -0400 Subject: [PATCH 2/2] Resolve a conflict #13615 #13612 #13619 --- .../src/rabbit_mgmt_wm_exchange_publish.erl | 15 --------------- .../src/rabbit_mgmt_wm_queue_actions.erl | 14 -------------- .../src/rabbit_mgmt_wm_queue_get.erl | 14 -------------- 3 files changed, 43 deletions(-) diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl index 15779566c758..efd4500d9e45 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl @@ -107,21 +107,6 @@ bad({{coordinator_unavailable, _}, _}, ReqData, Context) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). -<<<<<<< HEAD -raise_not_found(ReqData, Context) -> - ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of - not_found -> - "vhost_not_found"; - _ -> - "exchange_not_found" - end, - rabbit_mgmt_util:not_found( - rabbit_data_coercion:to_binary(ErrorMessage), - ReqData, - Context). - -======= ->>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- decode(Payload, <<"string">>) -> Payload; diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl index ec802b69a5b7..68bf00406f59 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl @@ -57,20 +57,6 @@ do_it(ReqData0, Context) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_admin(ReqData, Context). -<<<<<<< HEAD -raise_not_found(ReqData, Context) -> - ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of - not_found -> - "vhost_not_found"; - _ -> - "queue_not_found" - end, - rabbit_mgmt_util:not_found( - rabbit_data_coercion:to_binary(ErrorMessage), - ReqData, - Context). -======= ->>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- action(Else, _Q, ReqData, Context) -> diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl index 93e4f97f4046..baffbc731833 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl @@ -155,20 +155,6 @@ basic_get(Ch, Q, AckMode, Enc, Trunc) -> is_authorized(ReqData, Context) -> rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). -<<<<<<< HEAD -raise_not_found(ReqData, Context) -> - ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of - not_found -> - "vhost_not_found"; - _ -> - "queue_not_found" - end, - rabbit_mgmt_util:not_found( - rabbit_data_coercion:to_binary(ErrorMessage), - ReqData, - Context). -======= ->>>>>>> dfe484be9 (Fix the exception logged by Cowboy caused by double reply (#13612)) %%-------------------------------------------------------------------- maybe_truncate(Payload, none) -> Payload;