ErlangInside

23

Nov

Gen_event: Unlinking Processes When Unsubscribing from a Publisher?

As Erlang Inside has been quiet lately we’ll publish a technical question.

Should gen_event unlink processes when they unsubscribe from a publisher? Fernando Benavides, our Erlang development lead at Inaka Networks, found this issue with gen_event. Is this behavior by design? The output of running this code on R14B is below, and the source is below that:

The question is should they be linked in the first place? Or should they just be monitored?  Either way, the behavior should match the documentation… UPDATE: Robert Virding points out that the documentation does say it links but there’s no matching delete_sup_handler.

1> event_test:run().
We start a publisher and three subscribers...
Publisher running on <0.33.0>
sub1 subscriber running on <0.34.0>
sub2 subscriber running on <0.35.0>
sub3 subscriber running on <0.36.0>
 
Now you should see three event receptions...
sub3: Event received: {local_time,{{2010,11,23},{11,25,40}}}
sub2: Event received: {local_time,{{2010,11,23},{11,25,40}}}
sub1: Event received: {local_time,{{2010,11,23},{11,25,40}}}
 
Now we shut down sub3, so according to the docs ?MODULE:terminate/2 will be called for sub3 with {stop,normal} as argument...
sub3: Terminate: {stop,normal}
sub2: Info received: {'EXIT',<0.36.0>,normal}
sub1: Info received: {'EXIT',<0.36.0>,normal}
 
What happened? Each other subscriber received an info message? That's because they're linked to the publisher since they were added using gen_event:add_sup_handler/3. That's not on the erlang docs.
 
Now we'll unsubscribe sub2 without actually stopping it, that should result in sub2 process receiving a gen_event_EXIT message...
sub2: Terminate: {unsubscribed,sub2}
sub2: Handler removed: normal
 
We would expect the publisher to be unlinked from sub2, so we stop sub2 hoping that nothing happens...
sub1: Info received: {'EXIT',<0.35.0>,normal}
 
But something actually happened! sub1 received an info message telling him that sub2 exited
That means that even when sub2's handler was deleted, sub2 process is still linked to the publisher.
 
Cleaning up...
sub1: Terminate: stop
sub1: Handler removed: shutdown
https://gist.github.com/cbd/711844

Comentarios recientes