From 4b69622d72046f26e17a9e3500a924219685032e Mon Sep 17 00:00:00 2001 From: vans163 Date: Thu, 1 Sep 2016 12:06:10 -0400 Subject: Update rebar_agent.erl https://github.com/erlang/rebar3/pull/1317 In reference to with support to load erlang code atomically but load nifs non-atomically. --- src/rebar_agent.erl | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl index 95818d8..5d6b050 100644 --- a/src/rebar_agent.erl +++ b/src/rebar_agent.erl @@ -109,8 +109,7 @@ refresh_paths(RState) -> {X,X} -> ?DEBUG("reloading ~p from ~s", [Modules, Path]), code:replace_path(App, Path), - [begin code:purge(M), code:delete(M), code:load_file(M) end - || M <- Modules]; + reload_modules(Modules); {_,_} -> ?DEBUG("skipping app ~p, stable copy required", [App]) end @@ -123,3 +122,31 @@ refresh_state(RState, _Dir) -> rebar3:init_config(), [fun(S) -> rebar_state:apply_profiles(S, rebar_state:current_profiles(RState)) end] ). + +reload_modules([]) -> noop; +reload_modules(Modules) -> + reload_modules(Modules, erlang:function_exported(code, prepare_loading, 1)). + +%% OTP 19 and later -- use atomic loading and ignore unloadable mods +reload_modules(Modules, true) -> + case code:prepare_loading(Modules) of + {ok, Prepared} -> + [code:purge(M) || M <- Modules], + code:finish_loading(Prepared); + + {error, ModRsns} -> + Blacklist = + (fun Error([], Acc) -> Acc; + Error([ {ModError, on_load_not_allowed} |T], Acc) -> + reload_modules([ModError], false), + Error(T, Acc); + Error([ {ModError, _} |T], Acc) -> + Error(T, [ModError|Acc]) + end)(ModRsns, []), + reload_modules(Modules -- Blacklist, true) + end; + +%% Older versions, use a more ad-hoc mechanism. Specifically has +%% a harder time dealing with NIFs. +reload_modules(Modules, false) -> + [begin code:purge(M), code:load_file(M) end || M <- Modules]. -- cgit v1.1 From d74cd14065bacf786bf4a463aa47715e80b412ec Mon Sep 17 00:00:00 2001 From: vans163 Date: Thu, 1 Sep 2016 13:31:23 -0400 Subject: Update rebar_agent.erl opps. Infinite loop fixed. --- src/rebar_agent.erl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl index 5d6b050..72bbe44 100644 --- a/src/rebar_agent.erl +++ b/src/rebar_agent.erl @@ -137,9 +137,9 @@ reload_modules(Modules, true) -> {error, ModRsns} -> Blacklist = (fun Error([], Acc) -> Acc; - Error([ {ModError, on_load_not_allowed} |T], Acc) -> - reload_modules([ModError], false), - Error(T, Acc); + Error([ {ModNif, on_load_not_allowed} |T], Acc) -> + reload_modules([ModNif], false), + Error(T, [ModNif|Acc]); Error([ {ModError, _} |T], Acc) -> Error(T, [ModError|Acc]) end)(ModRsns, []), -- cgit v1.1 From 9b64c60f8bbbac7686d0f8d1f56219c847d2c811 Mon Sep 17 00:00:00 2001 From: vans163 Date: Thu, 1 Sep 2016 14:20:48 -0400 Subject: comment and spacing fixup --- src/rebar_agent.erl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl index 72bbe44..818401d 100644 --- a/src/rebar_agent.erl +++ b/src/rebar_agent.erl @@ -125,7 +125,7 @@ refresh_state(RState, _Dir) -> reload_modules([]) -> noop; reload_modules(Modules) -> - reload_modules(Modules, erlang:function_exported(code, prepare_loading, 1)). + reload_modules(Modules, erlang:function_exported(code, prepare_loading, 1)). %% OTP 19 and later -- use atomic loading and ignore unloadable mods reload_modules(Modules, true) -> @@ -137,16 +137,15 @@ reload_modules(Modules, true) -> {error, ModRsns} -> Blacklist = (fun Error([], Acc) -> Acc; - Error([ {ModNif, on_load_not_allowed} |T], Acc) -> + Error([ {ModNif, on_load_not_allowed} |T], Acc) -> reload_modules([ModNif], false), Error(T, [ModNif|Acc]); - Error([ {ModError, _} |T], Acc) -> + Error([ {ModError, _} |T], Acc) -> Error(T, [ModError|Acc]) end)(ModRsns, []), reload_modules(Modules -- Blacklist, true) end; -%% Older versions, use a more ad-hoc mechanism. Specifically has -%% a harder time dealing with NIFs. +%% Older versions, use a more ad-hoc mechanism. reload_modules(Modules, false) -> [begin code:purge(M), code:load_file(M) end || M <- Modules]. -- cgit v1.1 From 6180ce0c64041e58feab600aa71ca31978b5d131 Mon Sep 17 00:00:00 2001 From: vans163 Date: Thu, 1 Sep 2016 14:44:38 -0400 Subject: Make less than R17 compatible --- src/rebar_agent.erl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl index 818401d..6b4526e 100644 --- a/src/rebar_agent.erl +++ b/src/rebar_agent.erl @@ -136,13 +136,17 @@ reload_modules(Modules, true) -> {error, ModRsns} -> Blacklist = - (fun Error([], Acc) -> Acc; - Error([ {ModNif, on_load_not_allowed} |T], Acc) -> - reload_modules([ModNif], false), - Error(T, [ModNif|Acc]); - Error([ {ModError, _} |T], Acc) -> - Error(T, [ModError|Acc]) - end)(ModRsns, []), + lists:foldr(fun({ModError, Error}, Acc) -> + case Error of + %perhaps cover other cases of failure? + on_load_not_allowed -> + reload_modules([ModError], false), + [ModError|Acc]; + _ -> [ModError|Acc] + end + end, + [], ModRsns + ), reload_modules(Modules -- Blacklist, true) end; -- cgit v1.1 From 0f524bb6c2efc687956af85150b322b3ed883b03 Mon Sep 17 00:00:00 2001 From: vans163 Date: Fri, 2 Sep 2016 10:12:39 -0400 Subject: delete purge load_file specific order --- src/rebar_agent.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_agent.erl b/src/rebar_agent.erl index 6b4526e..8e1c8f6 100644 --- a/src/rebar_agent.erl +++ b/src/rebar_agent.erl @@ -152,4 +152,4 @@ reload_modules(Modules, true) -> %% Older versions, use a more ad-hoc mechanism. reload_modules(Modules, false) -> - [begin code:purge(M), code:load_file(M) end || M <- Modules]. + [begin code:delete(M), code:purge(M), code:load_file(M) end || M <- Modules]. -- cgit v1.1