[ejabberd] Concurrency issue inside custom Module

Abhishek Ranjan abhishek at blacklightsw.com
Wed Aug 9 13:51:19 MSK 2017


I have a custom module running in ejabberd with the following function 'myMessage' that is triggered whenever a chat message is sent by the client.

I call some other module's functions from this function myMessage as follows: 

myMessage({#message = Msg, C2SState})->


    some_other_module:some_func(Args),
    State=mnesia_module:read(key),
   
    {Msg, C2SState};
myMessage(Acc) ->
    Acc.

In the call

> 
>      some_other_module:some_func(Args),
> 

some_func() contains code written in Erlang's Message passing construct, and while its operation stores a state State in a mnesia table. But the next line

> 
>      State=mnesia_module:read(key),
> 

always shows a empty value of table as {atomic,[]},

And when I insert a timer:sleep/1 between these two lines as 


some_other_module:some_func(Args),

timer:sleep(20),
 State=mnesia_module:read(key),

I sometimes get the correct and current value of State and sometimes get a value which was the value of the previous state.

Thus it gives me a feeling that in the first case with no timer:sleep/1 present and in the second case where the timer:sleep/1 is present but is giving me the value of the previous state, the second line 

 State=mnesia_module:read(key),

is getting executed before the first line is able to execute completely and write into the mnesia table.


Can anyone suggest a way out of it?

Also the variable 'State' will get updated on each call, hence I am not sure if my choice of mnesia is a correct one or not. So any kind of suggestion will be very helpful.


Best Regards, 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.jabber.ru/pipermail/ejabberd/attachments/20170809/ecca51c5/attachment.html>


More information about the ejabberd mailing list