From e6223e76607d58fd45b5adb782dc26e92f5c8858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Thu, 12 Dec 2024 13:57:03 +0100 Subject: [PATCH] Fix issues in the REST API subscribing for messages - 'terminate' was not called because of missing 'trap_exit' flag - 'terminate' would crash because 'ok' was not stripped from Session - Any stanza other than 'message' would cause the process to crash Before the fix, rest_client_SUITE would end with ghost sessions. Now the state is clear after this suite. --- src/mongoose_client_api/mongoose_client_api_sse.erl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mongoose_client_api/mongoose_client_api_sse.erl b/src/mongoose_client_api/mongoose_client_api_sse.erl index 118c033d3f..f08b6e8378 100644 --- a/src/mongoose_client_api/mongoose_client_api_sse.erl +++ b/src/mongoose_client_api/mongoose_client_api_sse.erl @@ -18,6 +18,7 @@ routes() -> [{"/sse", lasse_handler, #{module => mongoose_client_api_sse}}]. init(_InitArgs, _LastEvtId, Req) -> + process_flag(trap_exit, true), % needed for 'terminate' to be called ?LOG_DEBUG(#{what => client_api_sse_init, req => Req}), {cowboy_rest, Req1, State0} = mongoose_client_api:init(Req, []), {Authorization, Req2, State} = mongoose_client_api:is_authorized(Req1, State0), @@ -28,7 +29,7 @@ init(_InitArgs, _LastEvtId, Req) -> maybe_init(Authorization, Req2, State#{id => 1}). maybe_init(true, Req, #{jid := JID} = State) -> - Session = mongoose_stanza_api:open_session(JID, false), + {ok, Session} = mongoose_stanza_api:open_session(JID, false), {ok, Req, State#{session => Session}}; maybe_init(true, Req, State) -> %% This is for OPTIONS method @@ -51,7 +52,9 @@ handle_info(Msg, State) -> handle_msg(<<"message">>, Acc, El, State) -> Timestamp = os:system_time(microsecond), Type = mongoose_acc:stanza_type(Acc), - maybe_send_message_event(Type, El, Timestamp, State). + maybe_send_message_event(Type, El, Timestamp, State); +handle_msg(_, _, _, State) -> + {nosend, State}. handle_error(Msg, Reason, State) -> ?LOG_WARNING(#{what => sse_handle_error, msg => Msg, reason => Reason}),