Possible Elixir 1.8.1 bug when redefining module under load?

I just upgraded to Elixir 1.8.1 after running 1.5 for a few years (rock solid, fyi–THANKS!!!)

I’m posting this because I use Elixir on a daily basis and have never come across this issue.

Basically, I modified an existing function Polo.Sim.Model.Ids.insert_start_end_date/2, then reloaded it in IEx. (The function was not itself running in a GenServer, but rather a function being called by another Genserver)

iex> r Polo.Sim.Model.Ids  
{:reloaded, Polo.Sim.Model.Ids, [Polo.Sim.Model.Ids]}
iex> 

Great, working, fine, as you can see below.

Then, the system tried to auto-recompile (I guess it detected changes I made), but then the app crashed with an UndefinedFunctionError.

I also am running a Phoenix 1.4/Ecto app, but since this is really backend stuff, I thought I’d post here.

[info] Updating holdings in Polo.Sim.Model.Holdings for [100675]...
[info] [100675] start_date = 2014-03-19 (oem = 2014-03-31), end_date = 2019-10-31
[info] Updating holdings in Polo.Sim.Model.Holdings for [100676]...
[info] [100676] start_date = 2019-04-11 (oem = 2019-04-30), end_date = 2019-10-31
[info] Loading model_id [100677] Parsley Energy Inc into Polo.Sim.Server...

warning: redefining module Polo.Sim.Model.Ids (current version loaded from _build/dev/lib/polo/ebin/Elixir.Polo.Sim.Model.Ids.beam)
  lib/polo/sim/model/ids.ex:1

[info] Updating holdings in Polo.Sim.Model.Holdings for [100677]...
[info] [100677] start_date = 2014-05-23 (oem = 2014-05-31), end_date = 2019-10-31

.... (works for a while...)

[info] [100717] start_date = 2007-01-03 (first_end_date = 2007-01-31) end_date = 2019-10-31
[info] Loading model_id [100718] Pure Storage Inc into Polo.Sim.Server...
[info] Updating holdings in Polo.Sim.Model.Holdings for [100718]...
[info] [100718] start_date = 2015-10-07 (first_end_date = 2015-10-31) end_date = 2019-10-31
[info] Loading model_id [100719] Phillips 66 into Polo.Sim.Server...
[info] Updating holdings in Polo.Sim.Model.Holdings for [100719]...
[info] [100719] start_date = 2012-04-12 (first_end_date = 2012-04-30) end_date = 2019-10-31
[info] Loading model_id [100720] PTC Inc into Polo.Sim.Server...
[info] Updating holdings in Polo.Sim.Model.Holdings for [100720]...
[info] [100720] start_date = 2007-01-03 (first_end_date = 2007-01-31) end_date = 2019-10-31
[info] Loading model_id [100721] PVH Corp into Polo.Sim.Server...
Compiling 34 files (.ex)
[info] Updating holdings in Polo.Sim.Model.Holdings for [100721]...
[error] GenServer Polo.Sim.Server terminating
** (UndefinedFunctionError) function Polo.Sim.Model.Ids.insert_start_end_date/2 is undefined (module Polo.Sim.Model.Ids is not available)
    (elixir) lib/enum.ex:769: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:769: Enum.each/2
    (polo) lib/polo/sim/server/server.ex:539: Polo.Sim.Server.update_data/0
    (polo) lib/polo/sim/server/server.ex:518: Polo.Sim.Server.handle_cast/2
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", :update}
Compiling lib/polo/web/views/signing_view.ex (it's taking more than 15s)
Compiling lib/polo/web/views/portfolio_view.ex (it's taking more than 15s)
Etc...

Its happened a few time today already (e.g. modifying a function, reloading it in IEx, working for a while, then blowing up on auto-recompile)

Thoughts?

Thanks in advance!!!

I know I’m late to the party, since it has been a while since you posted this question, but I experience something similar in a livebook and here are my thoughts related to this. Let’s start what my issue was:

I defined a GenServer in my livebook which executes some user defined functions. Thus, the code look something like this:

def myfunc(arg) do
  # do something in here 
end
GenServer.call(server, {:func, &myfunc/1})

The GenServerrepeatedly called the function in certain time intervals.

This all worked just fine, until I redefined the function to do something else, because suddenly I received the UndefinedFunctionError from the GenServer (Note: not from defining my function).

Instead of using named functions I used anonymous functions and suddenly the problem disappeared.

I suspect that the issue is with redefining the function (in one process) and accessing that function (through a reference) in another process. The first process has dropped the function (still referenced by the other process) and the other process hasn’t caught up with the change (yet!?). I’m not sure if the other process would catch up at all… I never investigated this.

Your description sounds very similar to this and therefore I would suspect that you have a similar issue.

With my assumptions and my (limited) understanding of Elixir/Erlang, I don’t think this is a bug in Elixir. I"m not even sure it’s a bug in Erlang, even though it would be nice if the function does not get dropped until all processes have caught up with the redefinition.

Maybe the above also helps for some more wise Elixir/Erlang people :slight_smile: to make that judgement call