Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  Help with supervisor behaviour

daws
Posted: Tue Aug 28, 2007 8:41 pm Reply with quote
Joined: 28 Aug 2007 Posts: 6
Hi, I want to get into OTP and I followed along with Joe's book and made a gen_server and supervisor. The server just has one [non-callback] function (divide) which takes two numbers and divides the first by the second. In Joe's example we have the area_server with a supervisor, and when it comes across a bad_match, the server is restarted by the supervisor. My server, when divide is called with the second argument being 0, crashes, restarts, crashes and then the supervisor terminates. Can anyone enlighten me to what I'm doing wrong? Attached are the two pertinent files.

P.S., I used the emacs skeletons to write them.

Thanks a lot!



divide_sup.erl
 Description:

Download
 Filename:  divide_sup.erl
 Filesize:  2.16 KB
 Downloaded:  1060 Time(s)


divide_server.erl
 Description:

Download
 Filename:  divide_server.erl
 Filesize:  4.34 KB
 Downloaded:  1094 Time(s)

View user's profile Send private message
khigia
Posted: Wed Aug 29, 2007 4:09 am Reply with quote
User Joined: 27 Mar 2007 Posts: 52
Haven't read The Book ... so not sure what I'm talking about Smile

My first assumption was that the trap_exit in supervisor cause the supervision tree to crash on first gen_server crash ... I was wrong, but not yet sure why ...

Second assumption gave better result:
I guess the divide_sup is started (linked!) from erlang shell, as well as the divide_server:divide/2 call ...
when the divide call fail, it causes the erlang process of the shell to crash (normal); but then the (linked) supervisor also crashes, and stop its gen_server child at the same time!
... I'm not clear, but try to make a bad call from another process, and you'll got the expected behaviour:
Code:
spawn(fun() -> divide_server:divide(10,0) end).


hope this help
View user's profile Send private message
daws
Posted: Wed Aug 29, 2007 12:32 pm Reply with quote
Joined: 28 Aug 2007 Posts: 6
That does work! Is there any way, however to keep he shell from crashing while calling from the shell so I have an easier time debugging?
View user's profile Send private message
khigia
Posted: Wed Aug 29, 2007 12:54 pm Reply with quote
User Joined: 27 Mar 2007 Posts: 52
Starting the supervisor without link to the shell (using start instead of start_link) should do what you want.

It may also be possible to start the supervisor in a special process (following code is not tested):
Code:
spawn(fun() -> divide_sup:start_link(), receive X -> ok end end)

(the supervisor is started, and the process wait)
View user's profile Send private message
daws
Posted: Wed Aug 29, 2007 1:55 pm Reply with quote
Joined: 28 Aug 2007 Posts: 6
I don't see a function called supervisor:start, though.
View user's profile Send private message
daws
Posted: Wed Aug 29, 2007 2:03 pm Reply with quote
Joined: 28 Aug 2007 Posts: 6
However the code you wrote does work. Thank you very much for your time!
View user's profile Send private message
khigia
Posted: Wed Aug 29, 2007 2:12 pm Reply with quote
User Joined: 27 Mar 2007 Posts: 52
oh, sorry, my bad ... didn't checked supervisor has start function.

Maybe a cleaner solution is to start the supervisor in one shell, and start another shell (connected to the same erlang node) for testing (or another job in the same shell, see CTRL-G h).

Hope this help.
View user's profile Send private message
daws
Posted: Fri Aug 31, 2007 1:44 pm Reply with quote
Joined: 28 Aug 2007 Posts: 6
I'll just have to do that or wrap it in a spawn function with the receive loop. Thanks for identifying the problem for me though. It was a great help.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum