[erlang-questions] demonitor

Ladislav Lenart lenartlad@REDACTED
Fri Aug 22 14:53:10 CEST 2008


Claes Wikstrom wrote:
> I just recently discovered some bugs in the ssh application.
> basically it's a code like in gen.erl
> 
>              erlang:demonitor(Mref),
>              receive
>                  {'DOWN', Mref, _, _, _} ->
>                      {ok, Reply}
>              after 0 ->
>                      {ok, Reply}
>              end;
> 
> 
> 
> The flushing wasn't there in the ssh app, and thus the DOWN messages
> got accumulated and there was a memory leak. (I posted to erlang-patches)
> 
> Anyway, here is my question.
> 
> Don't you _always_ want to do that flushing after demonitor. And if
> so, shouldn't the BIF do that. I bet there is whole lot of code
> out there which doesn't properly do the flush.

Hello,

I might be wrong but isn't this a "classical" race condition example?

1. Monitored process terminates.
2. Down message is sent.
3. Monitoring process calls demonitor.
4. Monitoring process flushes its mailbox for a potential down message
    which is not there yet.
5. Down message arrives and remains in the mailbox "forever".

Adding of the receive clause does not solve the original problem,
it just makes it happen less often.

The proper solution would be to always consume the down message
in handle_info/X callback (or such) and do something useful only
when the monitoring process is still interested in the event, no?

Ladislav Lenart




More information about the erlang-questions mailing list