Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compilation is non-blocking #25

Open
brigadier opened this issue Feb 5, 2024 · 5 comments
Open

compilation is non-blocking #25

brigadier opened this issue Feb 5, 2024 · 5 comments

Comments

@brigadier
Copy link
Contributor

Sometimes it takes several seconds to compile changed modules. This compilation is asynchronous so it won't lock console prompt thus it is possible to call a function which still would use code from the old version of a module, opaque to the user. There's even no indication whether the process of compilation and changing of code has been completed or not. Is it possible to make compilation synchronous and lock the console until the compilation process is completed?

@vans163
Copy link
Owner

vans163 commented Feb 6, 2024

Recompiling this way BTW is considered debug behaviour and should not be used in production, does this happen so often it throws your debug/local environment out of sync each time? Because if it happens 1/3 or 1/10 times, thats OK for something not meant to be used in production. The correct way to hotload in production so everything stays in sync is to use releases.

Adding this would be complex but something using https://www.erlang.org/doc/man/erlang.html#suspend_process-2 could work. Figure out which processes are critical to not suspend, spawn a "suspender" process, suspend all, recompile, resume. I think you could even code this yourself in minimal time

:erlang.spawn(fn()->
  me = self()
  pids = :erlang.processes() -- [me]
  
  pids |> Enum.each(& :erlang.suspend_process(&1))
  recompile
  pids |> Enum.each(& :erlang.resume_process(&1))
end)

@brigadier
Copy link
Contributor Author

Yes, it's debug so it is not a big problem, but it happens often as I run some code over a big chunk of semistructured data using different rules and to do this I run the same function with just [Arrow up - Enter] in the console, so it is almost always faster than compilation.
Maybe not suspend but at least some indication that compilation process is completed would help a lot.

@vans163
Copy link
Owner

vans163 commented Feb 7, 2024

If you issue a r3:do(compile) in the console (instead of just saving the file), does it return only once the project is fully recompiled?

Also what about rebar_agent:do(compile) instead as well?

@brigadier
Copy link
Contributor Author

r3:do returns only after the project is recompiled, tested it with r3:do(compile), mymodule:run(). after changing the run function. rebar_agent:do(compile) same, it locks the console prompt until everything is compiled.

@brigadier
Copy link
Contributor Author

brigadier commented Feb 7, 2024

looks like something like this does the job

Me = self(),
 F = fun() -> 
  erlang:display(start), 
  Pid = shell:whereis(), 
  erlang:suspend_process(Pid), 
  try
    rebar_agent:do(compile)
  after
    erlang:resume_process(Pid)
  end,
  erlang:display('end'),
  Me ! done 
end,
spawn(F),
receive
  done -> ok
end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants