Friday, August 15, 2014

erlang init restart issue

Create file reltool.config:
{sys, [{erts, [{mod_cond, derived}, {app_file, strip}]},
   {app_file, strip},
   {rel, "test", "1", []},
   {boot_rel, "test"},
   {profile, embedded},
   {incl_cond, exclude},
   {app, kernel, [{incl_cond, include}]},
   {app, stdlib, [{incl_cond, include}]},
   {app, crypto, [{incl_cond, include}]}
  ]}.
Create an Erlang release named ‘test’ from ‘reltool.conig’ above:
BASH> erl -eval '{ok, [Config]} = file:consult("reltool.config"),{ok, Spec} = reltool:get_target_spec([Config]), _ = file:make_dir("test"), reltool:eval_target_spec(Spec, code:root_dir(), "test").' -s init stop -noshell
Now bring up the node and crash it:
BASH> export BINDIR=$(pwd)/test/erts-6.0.1/bin && $BINDIR/erlexec -boot test/releases/1/test -mode embedded
Erlang/OTP 17 [erts-6.0.1] [source-57e57ad] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.0.1  (abort with ^G)
1> init:restart().
ok
2> {"init terminating in do_boot",{'cannot load',crypto,get_files}}

Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Problematic code in init.erl
load_mod_code(Mod, BinCode, FullName) ->
    case erlang:module_loaded(Mod) of
        false ->
            case erlang:load_module(Mod, BinCode) of
                {module,Mod} -> {ok,FullName};
                {error,on_load} ->
                    ?ON_LOAD_HANDLER ! {loaded,Mod},
                    {ok,FullName};
                Other ->
                    exit({'cannot load',Mod,Other})
            end; 
        _ -> % Already loaded.
            {ok,FullName}
    end. 
?ON_LOAD_HANDLER ! {loaded,Mod}, crashed with ‘badarg’ because ?ON_LOAD_HANDLER was not alive 
?ON_LOAD_HANDLER is not started for init:restart, bug ? 
If boot the node with -init_dbug flag, below log can be observed, but not during init:restart
{running_on_load_handler,crypto}
{on_load_handler_returned_ok,crypto}
In fact, if any of the -on_load(…) modules gets loaded when booting, init:restart won’t work!
bootstrap/lib/asn1/src/asn1rt_nif.erl:-on_load(load_nif/0).
lib/runtime_tools/src/dyntrace.erl:-on_load(on_load/0).
lib/crypto/src/crypto.erl:-on_load(on_load/0).
lib/asn1/src/asn1rt_nif.erl:-on_load(load_nif/0).
erts/example/matrix_nif.erl:-on_load(on_load/0).
Try asn1 for example:
BASH> export BINDIR=$(pwd)/asn1/erts-6.0.1/bin && $BINDIR/erlexec -boot asn1/releases/1/asn1 -mode embedded -init_debug
{progress,preloaded}
{progress,kernel_load_completed}
{progress,modules_loaded}
{start,heart}
{start,error_logger}
{start,application_controller}
{progress,init_kernel_started}
{apply,{application,load,[{application,stdlib,[{description,"ERTS  CXC 138 10"},{vsn,"2.0"},{id,[]},{modules,[array,base64,beam_lib,binary,c,calendar,dets,dets_server,dets_sup,dets_utils,dets_v8,dets_v9,dict,digraph,digraph_utils,edlin,edlin_expand,epp,eval_bits,erl_bits,erl_compile,erl_eval,erl_expand_records,erl_internal,erl_lint,erl_parse,erl_posix_msg,erl_pp,erl_scan,erl_tar,error_logger_file_h,error_logger_tty_h,escript,ets,file_sorter,filelib,filename,gb_trees,gb_sets,gen,gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,io_lib_fread,io_lib_pretty,lib,lists,log_mf_h,maps,math,ms_transform,orddict,ordsets,otp_internal,pg,pool,proc_lib,proplists,qlc,qlc_pt,queue,random,re,sets,shell,shell_default,slave,sofs,string,supervisor,supervisor_bridge,sys,timer,unicode,win32reg,zip]},{registered,[timer_server,rsh_starter,take_over_monitor,pool_master,dets]},{applications,[kernel]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}}
{apply,{application,load,[{application,asn1,[{description,"The Erlang ASN1 compiler version 3.0"},{vsn,"3.0"},{id,[]},{modules,[asn1rt,asn1rt_nif]},{registered,[asn1_ns,asn1db]},{applications,[kernel,stdlib]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}}
{progress,applications_loaded}
{apply,{application,start_boot,[kernel,permanent]}}
Erlang/OTP 17 [erts-6.0.1] [source-57e57ad] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

{running_on_load_handler,asn1rt_nif}
{on_load_handler_returned_ok,asn1rt_nif}
{apply,{application,start_boot,[stdlib,permanent]}}
{apply,{c,erlangrc,[]}}
{progress,started}
Eshell V6.0.1  (abort with ^G)
1> 
1> 
1> init:restart().
ok
2> {progress,preloaded}
{progress,kernel_load_completed}
{"init terminating in do_boot",{'cannot load',asn1rt_nif,get_files}}

Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Written with StackEdit.