<div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;">From: <span style="color:rgb(32,33,36);font-size:0.875rem;font-weight:bold;letter-spacing:0.2px;font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;white-space:nowrap">Valentin Micic</span><br><br><div>I’ve run a couple of tests on my own, and managed to reproduce your “second! and only second!!” packet problem.</div><div><br></div><div>It happens under following circumstances:</div><div><br></div><div>1) Socket (Rx) is instructed to provide a single packet as it arrives ( as per {active, once} );</div><div><br></div><div><br></div><div>2) After receiving this packet, a call to gen_udp:recv( Rx, 0, 0 ) is executed before another packet has ARRIVED to the kernel buffer;</div><div><br></div><div>Then the following diagram illustrates the problem:</div><div><br></div><div>Note:</div><div>    - Tx is sending socket;</div><div>    - Rx is receiving socket;</div><div><br></div><div style="font-size:14px"><b>Please note that sequence diagrams below are not completely accurate, but it is reasonably illustrative of what may have happened.</b></div><div><br></div><div><img id="gmail-m_6101839135358663616A9063F1E-9499-47B4-86A1-68E2C918F23E" width="705" height="534" src="cid:1701fe498065b3c9df81"></div><div><br></div><div><br></div><div style="font-size:14px">The next sequence diagram depicts a situation when a non-zero value for gen_udp:recv/3, e.g. gen_udp:recv( Rx, 0, <b>10</b> ):</div><div><br></div><div><br></div><div><img id="gmail-m_61018391353586636168A1A9CAC-4F02-4483-BF1F-B5E4F02A7F7F" width="705" height="534" src="cid:1701fe498075b58ccfa2"></div><div><br></div><div><br></div><div>Thus, it appears that gen_udp:recv( <i>Socket, Length</i>, <i>Timeout</i> ) is broken when <i>Timeout</i> is set to <b>0</b>.</div><div>For all positive non-zero values for Timeout, it behaves properly (or as intuitively expected).</div><div><br></div><div>Therefore, this should either be fixed, or it should be documented in order to indicate that 0 must *not* be used as a value for the Timeout in gen_udp:recv/3 call.</div><div><br></div><div>Aside, I fail to see a benefit of using Timeout of value 0 when combined with {active, once}. The only time when zero value may appear to be useful is when server does need to process other messages, e.g. messages not originated by the socket, which would be the case when gen_server is used to support the socket. In my view (which is supported by substantial empirical evidence), gen_server is not the most optimal behaviour to use when implementing support for sockets.</div><div><br></div><div>Kind regards</div><div><br></div><div>V/</div><div><br></div></div></blockquote></div><div><br></div>So, seems like there is some buggy behavior in the erlang VM for reading UDP messages between switching from {active, once} to gen_udp:recv(socket, 0, 0)? Lukas, Raimo could you help with that?<div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr">Петровский Александр / Alexander Petrovsky,<br><br>Skype: askjuise<br><div>Phone: +7 931 9877991<div><br></div></div></div></div></div></div></div>