From f3410db5032b9bdceff376f353697b16b3fcda9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20G=C3=B6m=C3=B6ri?= Date: Fri, 6 Oct 2023 07:02:39 +0200 Subject: [PATCH] Web-MQTT: don't call FHC when connection terminates early When the Web-MQTT connection terminates early because of no supported subprotocol, `terminate/3` called fhc release although no fhc obtain was called yet. This was the case even when `use_file_handle_cache` was false, because `#state.should_use_fhc` was not initialized. Fixing this avoids the below harmless warning ``` [debug] error updating ets counter <0.1224.0> in table #Ref<0.2797411137.1366163457.189876>: [{ets, update_counter, [#Ref<0.2797411137.1366163457.189876>, <0.1224.0>, {5, -1}], ... [warning] FHC: failed to update counter 'obtained_socket', client pid: <0.1224.0> ``` --- .../rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl b/deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl index ac430021bcd0..8420dd167876 100644 --- a/deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl +++ b/deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl @@ -75,12 +75,12 @@ init(Req, Opts) -> undefined -> no_supported_sub_protocol(undefined, Req); Protocol -> - WsOpts0 = proplists:get_value(ws_opts, Opts, #{}), - WsOpts = maps:merge(#{compress => true}, WsOpts0), case lists:member(<<"mqtt">>, Protocol) of false -> no_supported_sub_protocol(Protocol, Req); true -> + WsOpts0 = proplists:get_value(ws_opts, Opts, #{}), + WsOpts = maps:merge(#{compress => true}, WsOpts0), ShouldUseFHC = application:get_env(?APP, use_file_handle_cache, true), case ShouldUseFHC of true -> ?LOG_INFO("Web MQTT: file handle cache use is enabled"); @@ -278,7 +278,12 @@ terminate(_Reason, _Request, no_supported_sub_protocol(Protocol, Req) -> %% The client MUST include “mqtt” in the list of WebSocket Sub Protocols it offers [MQTT-6.0.0-3]. ?LOG_ERROR("Web MQTT: 'mqtt' not included in client offered subprotocols: ~tp", [Protocol]), - {ok, cowboy_req:reply(400, #{<<"connection">> => <<"close">>}, Req), #state{}}. + %% Set should_use_fhc to false, because at this early stage of init no fhc + %% obtain was called, so terminate/3 should not call fhc release + %% (even if use_file_handle_cache is true) + {ok, + cowboy_req:reply(400, #{<<"connection">> => <<"close">>}, Req), + #state{should_use_fhc = false}}. handle_data(Data, State0 = #state{}) -> case handle_data1(Data, State0) of