[ejabberd] Concurrency issue inside custom Module

Abhishek Ranjan abhishek at blacklightsw.com
Wed Aug 9 15:33:04 MSK 2017


Actually the call some_other_module:some_func/1 when executes generates a state and stores it into the mnesia table from which I am reading the state using the function call mnesia_module:read/1 i.e. mnesia_module:read/1 will only give the correct state once all the operations of some_other_module:some_func/1 are performed in its entirety.

After posting the question I was repeatedly checking the code using timer:sleep/1 and I am getting the correct state each time but as soon as I get rid of timer:sleep/1 I am getting an empty state but by the logs on ejabberd I can see that some_other_module:some_func/1 has executed correctly thus droping me to the same conclusion that mnesia_module:read/1 is reading the mnesia table before some_other_module:some_func/1 writes the state into it.

If it would help the code basic code in some_other_module:some_func/1 looks something like the code on this link
https://github.com/bekkopen/erlang-tictactoe/tree/master/src

and in the module tictactoe.erl in the above link you will find a "NewGameState" something like which I am storing in mnesia at the end of some_other_module:some_func/1 and then retrieving it using mnesia_module:read/1 .

Please Share your thoughts.

Best Regards
Abhishek Ranjan

> On August 9, 2017 at 5:25 PM Evgeny Khramtsov <xramtsov at gmail.com> wrote:
>
>
> Wed, 9 Aug 2017 16:21:19 +0530 (IST)
> Abhishek Ranjan <abhishek at blacklightsw.com> wrote:
>
> > 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.
>
> It's hard to say anything from such abstract code, even though you
> somehow described it. I can only say that if there is no race between
> some_other_module:some_func/1 and mnesia_module:read/1, then there
> shouldn't be any races at all, because the hook (which myMessage is
> associated with) is called inside a single process *synchronously*.
>
> Regarding Mnesia decision: it's indeed pointless to use Mnesia here if
> you just need to keep values on the c2s state (without storing them on
> disc).
> _______________________________________________
> ejabberd mailing list
> ejabberd at jabber.ru
> http://lists.jabber.ru/mailman/listinfo/ejabberd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.jabber.ru/pipermail/ejabberd/attachments/20170809/5242e867/attachment.html>


More information about the ejabberd mailing list