Skip to content

Commit e6223e7

Browse files
committed
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.
1 parent dea3bfd commit e6223e7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/mongoose_client_api/mongoose_client_api_sse.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ routes() ->
1818
[{"/sse", lasse_handler, #{module => mongoose_client_api_sse}}].
1919

2020
init(_InitArgs, _LastEvtId, Req) ->
21+
process_flag(trap_exit, true), % needed for 'terminate' to be called
2122
?LOG_DEBUG(#{what => client_api_sse_init, req => Req}),
2223
{cowboy_rest, Req1, State0} = mongoose_client_api:init(Req, []),
2324
{Authorization, Req2, State} = mongoose_client_api:is_authorized(Req1, State0),
@@ -28,7 +29,7 @@ init(_InitArgs, _LastEvtId, Req) ->
2829
maybe_init(Authorization, Req2, State#{id => 1}).
2930

3031
maybe_init(true, Req, #{jid := JID} = State) ->
31-
Session = mongoose_stanza_api:open_session(JID, false),
32+
{ok, Session} = mongoose_stanza_api:open_session(JID, false),
3233
{ok, Req, State#{session => Session}};
3334
maybe_init(true, Req, State) ->
3435
%% This is for OPTIONS method
@@ -51,7 +52,9 @@ handle_info(Msg, State) ->
5152
handle_msg(<<"message">>, Acc, El, State) ->
5253
Timestamp = os:system_time(microsecond),
5354
Type = mongoose_acc:stanza_type(Acc),
54-
maybe_send_message_event(Type, El, Timestamp, State).
55+
maybe_send_message_event(Type, El, Timestamp, State);
56+
handle_msg(_, _, _, State) ->
57+
{nosend, State}.
5558

5659
handle_error(Msg, Reason, State) ->
5760
?LOG_WARNING(#{what => sse_handle_error, msg => Msg, reason => Reason}),

0 commit comments

Comments
 (0)