Disadvantages Of Adding Modules Without Using Genserver

Phoenix is already an OTP application. I added some modules without using genserver. My question what happens if that module crashes different than a module using genserver?

A crash while executing code in your controller might “crash” a cowboy process handling the request, but probably not cowboy itself.

And if my unreleated module crashes whole phoenix will start again?

It depends on what exactly you crash. By default, I think, OTP applications are not restarted. And an OTP application terminates when its supervisors “give up” restarting their children.

1 Like

Yes you are right supervisors doesnt crash when child process crashes. Supervisors responsible to restart child process. Then how we compare non OTP ibrary and OTP library when they crash?

Then how we compare non OTP ibrary and OTP library when they crash?

What do you mean? If you have code that doesn’t use any of OTP modules and it “crashes”, and you don’t have anything resembling supervisors from OTP, then recovering from the crash depends totally on you. If you don’t do anything, your program just stops, probably.

Yes that happens with a completely non OTP app. But Phoenix already OTP app. So our non OTP library lives in an OTP app. With OTP library we can revover state when it crash but we cant recover state with non OTP library?.

What do you mean by state? By default, the restarted process is given its initial state. The state that it had before the crash does not get recovered automatically.

So our non OTP library lives in an OTP app.

What do you mean by non OTP library?

Something is “OTP”, in my understanding, if it uses modules from OTP. So if your code gets executed under supervision (which is what happens in your phoenix controllers), it automatically becomes “OTP”.

let me explain from begining

suppose we decided to code 2 different time library for phoenix as timex.
1- Modules with using OTP
2- Just a some modules codes without OTP

Both of these libraries will live in a OTP app (phoenix). And imagine our code crashed while parsing a date. With OTP library we can recover last state and reparse same date. And with non OTP library we lose that date right?

Can we though? I don’t know.

Probably not, unless you persist the date somewhere, like an ets table managed by some supervisor (probably the one that supervises the process that parses the date).

I am really confused . Can you tell why we build a OTP time library and why not non OTP time library ?

Can you tell why we build a OTP time library and why not non OTP time library ?

Probably because we don’t want bugs/whatever in our time library crash the rest of our app.

There are many reasons to use OTP …

OTP behaviors like genserver are very well thought out and handle many of the edge-cases that I would most likely miss if I were to rewrite it.

I think you are confused about what something being OTP means. It just means that you use the behaviors provided by OTP instead of using primitives like spawn and send that are baked into the language itself. That’s it. At least that’s my understanding of it.

I understand difference beetwen a non OTP and OTP app. Problem is a mix app with OTP modules and non OTP modules :smile:

I understand difference beetwen a non OTP and OTP app.

Then I am confused.

Problem is a mix app with OTP modules and non OTP modules :smile:

Can you provide an example of a non OTP module?

Maybe whole my perspective is wrong.

these libraries written with OTP right?

def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger, :runtime_tools, :coherence, :timex, :httpoison, :httpotion, :tesla, :scrivener_ecto, :scrivener_html, :arc_ecto]
    ]
  end

I also have another libraries no need to add in extra applications. I call them non OTP libraries

Most of those don’t need to be added as “extra applications”. You only need to add erlang applications, which come bundled with erlang there, if they should be started with your project. Your application as well as all your dependencies are still OTP applications, even if they do not start any supervision tree or processes.

2 Likes

Yes i think so. problem is then why we use GenServer? A library writtten with genserver and without genserver. I think llike that with genserver we can recover state that is the benefit ?

defmodule Stack do
  use GenServer
end

defmodule Stack do
end

Any function you call that can crash or raise will only ever crash the current BRAM process, and depending on the design any linked processes or notify monitors.

Well this assumes pure BEAM functions.

Things are different when dealing with NIFs.

1 Like

There is no such thing as OTP library and Non-OTP library.

There are only libraries that use OTP components or not. The question becomes: Should I use OTP components like gen_server or not? Or: Should I run the function in a separate process or not?
If you use Phoenix your controller code gets executed inside a gen_server (edit: proc_lib not gen_server; thanx for pointing this out @idi527) process if you or the library you use do not start a separate process.

1 Like

now i see my perspective is really wrong. So what happens when a library crashes with built otp components and without otp component? when we should use otp components?

More like a proc_lib process.

Ok, thanks for clarifying this. I wasn’t sure about that, actually.