15
Practical Erlang Programming Process Error Handling

Practical Erlang Programming Process Error Handling

Embed Size (px)

Citation preview

Page 1: Practical Erlang Programming Process Error Handling

Practical Erlang Programming

Process Error Handling

Page 2: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 2Process Error Handling

Erlang By Example

Page 3: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 3Process Error Handling

Overview: Process Error Handling I

• Process Error Handling I Links Exit Signals Definitions Propagation Semantics

Page 4: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 4Process Error Handling

Links

• link/1 will create a bi-directional link between the process calling the BIF and the process Pid

• spawn_link/3 will yield the same result as calling spawn/3 followed bylink/1, only that it will do it atomically

link(PidA)

PidA PidB

Page 5: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 5Process Error Handling

• Exit Signals are Sent when processes terminate abnormally

• They are sent to all processes to which the failing process is currently linked to

• The process receiving the signal will exit, propagate a new signal to the processes it is linked to

Exit Signals

{‘EXIT’, A, Reason}

Page 6: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 6Process Error Handling

• When process A fails, the exit signals propagate to process B

• From B, it propagates to C

Exit Signals

{‘EXIT’, A, Reason}

{‘EXIT’, B, Reason}

C

A

B

Page 7: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 7Process Error Handling

• Processes can trap exit signals by executing the BIF process_flag(trap_exit, true)

• Exit signals will be converted to messages of the format{‘EXIT’, Pid, Reason}

• They are saved in the process mailbox

• If an exit signal is trapped, it does not propagate further

Exit Signals

{‘EXIT’, A, Reason}A

Page 8: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 8Process Error Handling

• Process B marked with a double ring is trapping EXITs

• If an error occurs in A, or C then they will terminate.

• Process B will receive the {‘EXIT’,Pid,Reason} message

• The process that did not terminate will not be affected

Exit Signals

{‘EXIT’, A, Reason}

{‘EXIT’, B, Reason}

C

B

A

Page 9: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 9Process Error Handling

Definitions: Terminology

Link• A bi-directional propagation path for exit signals set up between

processes

Exit Signal• A signal transmitted by a process upon exiting. It contains

termination information

Error trapping• The ability of a process to handle exit signals as if they were

messages

Page 10: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 10Process Error Handling

Definitions: Built In Functions

link(Pid)Set a link between the calling process and Pid

unlink(Pid)Removes a link to Pid

spawn_link(Mod, Fun, Args)Atomically spawns and sets link between the calling and the spawned processprocess_flag(trap_exit, true |

false)Set the current process to convert exit signals to exit messages

Page 11: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 11Process Error Handling

Definitions: Built In Functions

• The BIF exit/1 terminates the process who calls it

• Generates an exit signal sent to linked processes

• The BIF exit/1 can be caught in a catch

{‘EXIT’, Pid, Reason}

Page 12: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 12Process Error Handling

Definitions: Built In Functions

• exit(Pid, Reason) - Sends an exit signal containing Reason to the process Pid

• If trapping exits, the signal is converted to an exit message

{‘EXIT’, Pid, Reason}

exit(Pid, Reason)

Page 13: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 13Process Error Handling

Propagation Semantics

• When a process terminates it sends an exit signal to the processes in its link set

• Exit signals can be normal or non-normal

• A process not trapping exit signals dies if it receives a non-normal exit signal. Normal signals are ignored

• A process which is trapping exit signals converts all incoming exit signals to conventional messages handled in a receive statement

• If Reason is kill, the process is terminated unconditionally

Page 14: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 14Process Error Handling

Trapping Exits

Not Trapping Exits

Normalreceives

{‘EXIT’, Pid, normal}nothing happens

Killterminates with reason

killedterminates with reason

killed

Otherreceives

{‘EXIT’, Pid, Other}terminates with reason

Other

Propagation Semantics

Trap_exitFlag

ExitReason

Page 15: Practical Erlang Programming Process Error Handling

© 2001 - 2009 Erlang Training and Consulting Ltd 15Process Error Handling

Summary: Process Error Handling I

• Process Error Handling I Links Exit Signals Definitions Propagation Semantics