3
votes

As far as I understand, a supervisor process can supervise a number of processes in Erlang/OTP behavioral model.

That processes can be gen_server, gen_fsm, and gen_event. My question is that can an ordinary process become supervised by a supervisor process in Erlang/OTP? By ordinary process I mean a process that is not gen_server, gen_fsm, or gen_event.

If so, how process specification will look like? I tried below but does not work:

Spec = {Id, {my_module, my_function, [Par1, ..., ParN]}, permanent, 5000, worker, [my_module]}

I also tried this:

Spec = {Id, {my_module, my_function, [Par1, ..., ParN]}, permanent, 5000, worker, []}

But when I replace the module and function with a gen_server module and a function from inside it, this works well.

Can I conclude that a child process for a supervisor can not be an ordinary process?

Error that I get is:

Error is follow: application: myapp exited: {bad_return, {{myapp_app,start,[normal,[]]}, {'EXIT', {{badmatch, {error, {shutdown, {failed_to_start_child,worker_1, {ok,state}}}}}, [{myapp_app,start,2, [{file,"src/myapp_app.erl"},{line,27}]}, {application_master,start_it_old,4, [{file,"application_master.erl"},{line,272}]}]}}}}

1
How do you know it doesn't work?legoscia
Obviously, I ran it, and got error.user3389168
What error did you get?legoscia
My question is: Can an ordinary process become supervised by a supervisor process?user3389168
Processes that fit into a supervised tree without using a standard behaviour are called "special processes", and should in principle follow the guidelines described in the Sys and Proc_Lib chapter of the OTP Design Principles User Guide - but in practice you can usually get away with ignoring those guidelines. That's why I asked about the error message: there's nothing in your question that gives any clue as to what's gone wrong.legoscia

1 Answers

4
votes

The most likely reason, without seeing your source, that this is failing is that ordinary spawn calls return just a pid, as opposed to OTP start_link calls which return {ok, Pid}, and this is what the supervisor expects.

As for whether or not an ordinary process can be supervised...

Short answer: Yes, but you should start it with proc_lib.

Long answer: Yes, but you should start it with proc_lib, and a few other things wouldn't hurt. Basically, your process should be OTP-compliant if you intend for it to work in an OTP supervision tree. Please refer to the OTP Design Principles for special/custom processes, as suggested by legoscia in the comments to your question.

If you supervise a process that is not started with proc_lib, you lose certain "guarantees" (for lack of a better term) that supervisors give you. For instance, a supervisor will not start the second process in its process list until the first one has completed its init function (in the case of gen_server/fsm/event). This "guarantee" is lost with generic spawn calls.