<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div>Hi Serge,</div><div><br></div><div>Thank you very much for clarifying this! At least I know where everything stands now.</div><div><br></div><div>Does the scenario I'm talking about make sense? Could a heartbeat in the current design end up interleaved with other data? I suspect it would be pretty rare, but we pump 10 million or more messages through this app per day, so even if the chance was a million to one, we'd end up seeing it ten times a day on average...</div><div><br></div><div>I'm thinking for now that I'll give Robert's suggestion a try. If I call ei_xreceive() with a timeout and wrap a mutex around it, would that then protect the socket? I'm assuming (probably a bad thing) that it doesn't reply in a different thread or something...</div><div><br></div><div>I guess it would be a bit wasteful to effectively poll the socket every millisecond or so (I can't really block the other threads from writing longer than that) but it seems to be a potential way to resolve this issue in the short term while I come up with a better fix.</div><div><br></div><div>Does that sound like a reasonable plan?</div><div><br></div><div>Thanks again!</div><div><br></div><div>Kind Regards,</div><div><br></div><div>Peter Membrey</div><div><br></div><hr id="zwchr"><div style="color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;" data-mce-style="color: #000; font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Serge Aleynikov" <serge@aleynikov.org><br><b>To: </b>"Peter Membrey" <peter@membrey.hk><br><b>Cc: </b>"Erlang Questions" <erlang-questions@erlang.org><br><b>Sent: </b>Tuesday, August 27, 2013 10:59:27 PM<br><b>Subject: </b>Re: [erlang-questions] Is ei_xreceive_msg() thread safe?<br><div><br></div><div dir="ltr">e<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;">i_send/receive family of functions are not thread safe, and receive functions do handle heartbeats internally. The way the ei_{connect,send,receive} functions are written it's not possible to do any non-blocking I/O with them or use them in multi-threaded code in the manner you described without modifying the functions.</div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;"><br></div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;">You can take a look at the alternative C++ library that doesn't have such limitations: </div><a href="https://github.com/saleyn/eixx" target="_blank" data-mce-href="https://github.com/saleyn/eixx">https://github.com/saleyn/eixx</a><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;">, and offers almost all functionality that ei has.</div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;"><br></div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;">Regards,</div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;"><br></div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline" data-mce-style="font-family: arial,helvetica,sans-serif; display: inline;">Serge</div></div></div><div class="gmail_extra"><br><div><br></div><div class="gmail_quote">On Tue, Aug 27, 2013 at 3:30 AM, Peter Membrey <span dir="ltr"><<a href="mailto:peter@membrey.hk" target="_blank" data-mce-href="mailto:peter@membrey.hk">peter@membrey.hk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex" data-mce-style="margin: 0 0 0 .8ex; border-left: 1px #ccc solid; padding-left: 1ex;">Hi guys,<br> <br> I've got a fairly basic C Node set up where I have the main thread running in a loop with ei_xreceive_msg() and a number of "callback" threads that execute functions and write data using ei_send() to the shared socket (connecting to the Erlang node).<br> <br> Originally I had a lot of data corruption (the Erlang node crashing due to corrupt data) because of incorrect locking on socket writes. I added mutexes to the ei_send() calls and this problem seemed to go away.<br> <br> However I've had a couple of occasions where the system has been quiet and then suddenly become busy where corrupt data has still been sent to the Erlang node. I'm positive all the places where I do ei_send() are protected, but that got me wondering about ei_xreceive_msg().<br> <br> From what I can find, ei_xreceive_msg() automatically handles heartbeats for you and I guess that means it will send some sort of reply on that socket. If the heart beat is being sent at the same time as some other process tries to write to the socket, is it possible that the two could get interleaved or something? I would honestly have thought it unlikely but I'm running out of ideas.<br> <br> Assuming it's possible, how could I add a mutex in this case? The call itself blocks, so I can't wrap the whole call in the mutex else nothing else will be able to send data, and there's no way to pass a mutex into the call itself. So as far as I can tell, there's no way to protect these writes and prevent them from getting mixed up with other writes on that socket.<br> <br> Does anyone have any ideas? I'm quite willing to accept I could be doing something pretty stupid, but I'm really out of ideas as to what that might be...<br> <br> Thanks in advance!<br> <br> Kind Regards,<br> <br> Peter Membrey<br> _______________________________________________<br> erlang-questions mailing list<br> <a href="mailto:erlang-questions@erlang.org" target="_blank" data-mce-href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank" data-mce-href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br></div></div><div><br></div></div></body></html>