[erlang-questions] Why does this crash?

Vimal Kumar <>
Sun Sep 27 10:52:59 CEST 2015


Hi Robert,

Going to add supervisors into this system and learn more about it.

Thanks!

On Sun, Sep 27, 2015 at 1:30 PM, Robert Raschke <>
wrote:

> Hi Vinal,
>
> the process that starts the server is linked to it. In your example that
> is the shell process. The exception due to the pattern mismatch kills your
> shell and therefore also your linked server.
>
> The "real" way to run a server is in a supervision tree. There it us the
> supervision process that starts your server and can react to it crashing.
> (Or if you don't care about error propagation, you could start your server
> without linking.)
>
> If you need to handle a termination of your server, for example to do some
> cleanup, then your server needs to "trap exits".
>
> I realise that all of this can be a bit overwhelming when first starting
> out on the OTP behaviours. Reading and re-reading the behaviour docs (
> http://www.erlang.org/doc/design_principles/users_guide.html) and playing
> around with small systems like your example will get you there.
>
> Hth,
> Robby
> On Sep 27, 2015 9:30 AM, "Vimal Kumar" <> wrote:
>
>> Hi all,
>>
>> Erlang newbie here. This is my first attempt to write an OTP module
>> myself - a simple bank server. API functions allow users to register,
>> login, deposit/withdraw money to/from their own account.
>>
>> The bank_server:login/2 function returns a secret key (sort of unique
>> session id, lets assume) which is required for further meaningful
>> transactions (like bank_server:deposit/3 or bank_server:withdraw/3).
>>
>> 1> bank_server:start_link().
>> {ok,<0.549.0>}
>>
>> 2> whereis(bank_server).
>> <0.549.0>
>>
>> 3> bank_server:new_user(user1, "mysecretpass1").
>> {ok}
>>
>> 4> {_, SecretKey1} = bank_server:login(user1, "mysecretpass1").
>> {ok,"lYWrBFIi7CoeYN8KSK3QgA=="}
>>
>> 5> bank_server:deposit(user1, SecretKey1, 10.00).
>> {ok,success}
>>
>> 6> bank_server:check_balance(user1, SecretKey1).
>> {ok,10.0}
>>
>> 7> bank_server:new_user(user2, "mysecretpass2").
>> {ok}
>>
>> 8> {_, SecretKey1} = bank_server:login(user2, "mysecretpass2").
>> ** exception error: no match of right hand side value {ok,
>> "vYaWQbNQvROqXybVf424lQ=="}
>>
>> 9> whereis(bank_server).
>> undefined
>>
>> In line 8, I understand that I should have used a different variable,
>> since SecretKey1 already has a value. But I am curious why a simple mistake
>> *** from client side *** crashed the server itself? Is there any way to
>> avoid it?
>>
>> I am yet to learn OTP supervisors, but I believe it might have restarted
>> the server without any delay in such a crash.
>>
>> The complete code for bank_server.erl is available at
>> http://pastebin.com/4bMWjNG7 to check.
>>
>> Thank you!
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150927/36bb1503/attachment.html>


More information about the erlang-questions mailing list