Skip to content

Commit

Permalink
Fix issues in the REST API subscribing for messages
Browse files Browse the repository at this point in the history
- '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.
  • Loading branch information
chrzaszcz committed Dec 12, 2024
1 parent dea3bfd commit e6223e7
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_sse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 21 in src/mongoose_client_api/mongoose_client_api_sse.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_client_api/mongoose_client_api_sse.erl#L21

Added line #L21 was not covered by tests
?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),
Expand All @@ -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),

Check warning on line 32 in src/mongoose_client_api/mongoose_client_api_sse.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_client_api/mongoose_client_api_sse.erl#L32

Added line #L32 was not covered by tests
{ok, Req, State#{session => Session}};
maybe_init(true, Req, State) ->
%% This is for OPTIONS method
Expand All @@ -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);

Check warning on line 55 in src/mongoose_client_api/mongoose_client_api_sse.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_client_api/mongoose_client_api_sse.erl#L55

Added line #L55 was not covered by tests
handle_msg(_, _, _, State) ->
{nosend, State}.

Check warning on line 57 in src/mongoose_client_api/mongoose_client_api_sse.erl

View check run for this annotation

Codecov / codecov/patch

src/mongoose_client_api/mongoose_client_api_sse.erl#L57

Added line #L57 was not covered by tests

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

0 comments on commit e6223e7

Please sign in to comment.