Search code examples
erlanggen-server

gen_server not getting messages after httpc call


I have one process which sends a pause message to a gen_server like so:

Results = [gen_server:cast(Child, pause) || 
      {Id, Child, _Type, _Modules} <- supervisor:which_children(?SERVER),
      ?IGNORE(Id) == false],

In my gen_server, I catch these messages in my handle_cast as follows:

handle_cast(pause, #state{task=#task{server=Serv, 
                 service=Srv, 
                 description=Desc}}=State) ->
    lager:info("Suspending ~s, ~s, ~s.",[Serv, Srv, Desc]),
    {noreply, State#state{suspended=true}};

handle_cast(Msg, State) ->
    lager:error("Url Poller received unexpected cast message: ~p",[Msg]),
    {noreply, State}.

What's really strange is that fairly frequently one of my gen_servers doesn't seem to receive the pause message -- I get no lager message and the process in question will not respond to subsequent attempts to pause (or resume).

Any ideas about what might be going on?

The gen_server is very simple, it uses erlang:send_after/3 to send itself a "poll" message. Upon receiving this poll message, if not paused, it hits a url and saves the response to an ETS and fires off another erlang:send_after/3 to poll again after an appropriate interval. If its paused, it simply fires off another erlang:send_after?3

All pause does is set the state to paused = true

Using observer, the stuck process shows that the current function is httpc:handle_answer and that the message queue is backing up

Sate Tab: Information "Timed out" Tip "system messages are probably not treated by this process"

the top of the stack trace shows httpc:handle_answer httpc.erl:636


Solution

  • I picked the code of httpc:handle_answer from github erlang otp inets http client:

    (Note: it is not the same version as yours since the function goes from line 616 to 631)

    handle_answer(RequestId, false, _) ->
        {ok, RequestId};
    handle_answer(RequestId, true, Options) ->
        receive
            {http, {RequestId, saved_to_file}} ->
                ?hcrt("received saved-to-file", [{request_id, RequestId}]),
                {ok, saved_to_file};
            {http, {RequestId, {_,_,_} = Result}} ->
                ?hcrt("received answer", [{request_id, RequestId},
                                          {result, Result}]),
                return_answer(Options, Result);
            {http, {RequestId, {error, Reason}}} ->
                ?hcrt("received error", [{request_id, RequestId},
                                         {reason, Reason}]),
                {error, Reason}
        end.
    

    So the process is waiting for a message (coming after a call to httpc_manager:request(Request, profile_name(Profile) which has returned {ok, RequestId}), and this message does not come or it has a wrong format. Can you check the values of the parameters and the message queue?