为什么我的Erlang应用程序崩溃了?

时间:2014-07-29 07:23:54

标签: erlang thrift otp rebar erl

我正在构建一个简单的erlang应用程序,并且对环境很新,所以我无法理解我遇到的一些错误。我不确定问题的根源在哪里,所以如果有任何相关内容我不发布,请告诉我:

我正在运行的模块的来源:

  -module(basilisk_server).
  -author("Oak").

  -export([start_link/2,start/0,start/1,start/2, stop/1]).
  -behaviour(application).
  -record(options,{
    port = 6890,
    server_opts = []
  }).
  -define(LocalIP,"192.168.64.128").
  start()->
    start([]).
  start(Args)->
    #options{port = Port, server_opts = ServerOpts}  = parse_args(Args),
    spawn(
      fun() ->
        start_link(Port, ServerOpts),
        receive after infinity -> ok end
      end)
  .
  start(_StartType, Args) ->
    start(Args).

  parse_args(Args) -> parse_args(Args, #options{}).
  parse_args([], Opts) -> Opts;
  parse_args([Head | Rest], Opts) ->
    NewOpts =
      case catch list_to_integer(Head) of
        Port when is_integer(Port) ->
          Opts#options{port = Port};
        _Else ->
          case Head of
            "framed" ->
              Opts#options{server_opts = [{framed, true} | Opts#options.server_opts]};
            "" ->
              Opts;
            _Else ->
              erlang:error({bad_arg, Head})
          end
      end,
    parse_args(Rest, NewOpts)
  .
  stop(_State) ->
    ok.

  start_link(Port, ServerOpts) ->
    io:format("Starting server on port ~p with args: ~p~n",[Port,ServerOpts]),

    Services =
      [
        {"AuthenticationService",authenticationService_thrift},
        {"UserRegistrationService",userRegistrationService_thrift}
      ]
    ,
    {ok, _} = thrift_socket_server:start([
      {ip, ?LocalIP},

      {port, Port},
      {name, ?MODULE},
      {service, Services},
      {handler,[
        {"error_handler",  thrift_error_handler},
        {"AuthenticationService",authentication_service},
        {"UserRegistrationService",user_registration_service}
      ]},
      {socket_opts, [{recv_timeout, infinity}]}
    ]++
    ServerOpts).

我运行application:start(basilisk_server).并按此顺序获取这些消息:

   {error,{bad_return,{{basilisk_server,start,[normal,[]]},
                      <0.38.0>}}}

  =INFO REPORT==== 29-Jul-2014::03:11:06 ===
      application: basilisk_server
      exited: {bad_return,{{basilisk_server,start,[normal,[]]},<0.38.0>}}
      type: temporary

  =ERROR REPORT==== 29-Jul-2014::03:11:06 ===
  Error in process <0.38.0> with exit value: {terminated,[{io,format,
 [<0.36.0>,"Starting server on port ~p with args: ~p~n",[6890,[]]],[]},
 {basilisk_server,start_link,2,[{file,"src/basilisk_server.erl"},{line,55}]}, 
 {basilisk_server,'-start/1-fun-0-',2,[{file,"src/basilisk_serve... 

我遇到的主要问题是确定错误的实际来源。我的印象是io:format在短时间内出现问题,但我认为这是一个“红鲱鱼”,而且bad_result是我问题的根源。我以几乎相同的状态运行程序,它正在工作,突然开始出现此错误。我回滚了大部分更改并且没有停止。我也尝试重新启动,以防后台进程出现问题。

1 个答案:

答案 0 :(得分:5)

您正在使用application:start,希望您的模块遵守application行为。

即:它将调用foo:start/2,告诉您应用程序正在启动。您应该返回{ok, Pid}。这在OTP设计原则的“应用程序”chapter中有记录。

但是,您的start函数会立即调用spawn,并使用其结果。由于spawn返回Pid,而不是{ok, Pid}application:start抱怨预期结果不匹配。

这就是您看到bad_return

的原因
{error,{bad_return,{{basilisk_server,start,[normal,[]]},
                  <0.38.0>}}}

这告诉您,您有error类型bad_return。它在调用basilisk_server:start(normal, [])时发生,并且返回值为<0.38.0>(pid)。

哦,另一个错误是因为您使用了start_link,这意味着您的两个进程已链接。当一个人死亡时,另一个人将被杀死。这就是您在terminated看到的内容。在这种情况下,它碰巧在io:format中间被杀死;它可能会在其他运行中被杀之前进一步发展。

此外,假定应用程序启动根管理程序(这是返回的pid的用途)。

此时,您不需要application行为。事实上,我并不相信你需要大部分代码。只需致电thrift_socket_server:start即可完成。它将继续在后台运行。

相关问题