Boost C++ Libraries: Ticket #7961: handle_connect called before the socket was actually connected https://svn.boost.org/trac10/ticket/7961 <p> RedHat5.8, kernel 2.6.32.26-17.el5, glibc 2.5-81.el5_8.7 The problem was detected with boost 1.49, but was confirmed with 1.53b1 also. </p> <p> Problem: handle_connect (connect completion handler) can be called before TCP open handshake completes. </p> <p> Unfortunately I could not strip my application to reasonable size, so I prefer not to post it. There is mail thread related to this bug at </p> <ul><li><a class="ext-link" href="http://sourceforge.net/mailarchive/forum.php?thread_name=kegjks%24c82%241%40ger.gmane.org&amp;forum_name=asio-users"><span class="icon">​</span>Asio Users Mail List</a> </li></ul><p> I believe there is a bug in epoll_reactor, the way it handles EPOLLHUP event on yet-not-connected sockets. Below I explain the details and symptoms. </p> <p> Here is strace output of such connects: </p> <pre class="wiki">[pid 25441] socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 9 [pid 25441] epoll_ctl(5, EPOLL_CTL_ADD, 9, {EPOLLIN|EPOLLPRI|EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLET, {u32=1811961744, u64=140515962017680}} &lt;unfinished ...&gt; [pid 25442] epoll_wait(5, &lt;unfinished ...&gt; [pid 25441] &lt;... epoll_ctl resumed&gt; ) = 0 [pid 25442] &lt;... epoll_wait resumed&gt; {{EPOLLOUT, {u32=1811958752, u64=140515962014688}}, {EPOLLIN, {u32=1811943784, u64=140515961999720}}, {EPOLLOUT|EPOLLHUP, {u32=1811961744, u64=140515962017680}}}, 128, 0) = 3 [pid 25441] ioctl(9, FIONBIO, [1]) = 0 [pid 25441] connect(9, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("xxx.xxx.193.11")}, 16) = -1 EINPROGRESS (Operation now in progress) *********** no epoll_wait after connect ********** [pid 25441] epoll_ctl(5, EPOLL_CTL_MOD, 9, {EPOLLIN|EPOLLPRI|EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLET, {u32=1811961744, u64=140515962017680}}) = 0 *********** calling handle_connect [pid 25442] getsockopt(9, SOL_SOCKET, SO_ERROR, [7782250667543887872], [4]) = 0 [pid 25442] getpeername(9, 0x40450360, [140514150055964]) = -1 ENOTCONN (Transport endpoint is not connected) [pid 25442] write(2, "connect error: ", 15connect error: ) = 15 [pid 25442] write(2, "Transport endpoint is not connec"..., 35Transport endpoint is not connected) = 35 [pid 25442] write(2, "\n", 1 ) = 1 </pre><p> As one can see, there is no epoll_wait after ::connect call, but connect handler was called. </p> <p> So, asio calls "::connect" and then immediately calls user handle_connect handler without calling (and waiting for) epoll_wait between ::connect and handle_connect. Thus handle_connect is called before the socket was actually connected. </p> <p> What may happen is: </p> <ol><li>main thread calls do_open and adds the socket to epoll queue. </li><li>service thread calls epoll_wait and it returns several events INCLUDING that socket. </li><li>main thread calls async_connect (and modifies the socket in epoll queue, but it does not matter at this point) </li><li>service thread processes the events it got form epoll_wait at step <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2" title="#2: Bugs: list::size should be const (closed: fixed)">#2</a> in a loop, and when it process that socket, the completion connect handler is called. </li></ol><p> The possible workaround is to ignore EPOLLHUP in epoll_reactor::descriptor_state::perform_io() until the socket got 'connected' state. </p> <p> I'm attaching the patch that works for me. It need to be carefully reviewed, because of possible unwanted side effects (e.g. lost socket disconnect error notifications). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7961 Trac 1.4.3 Nikki Chumakov <nikkikom@…> Fri, 01 Feb 2013 16:05:22 GMT attachment set https://svn.boost.org/trac10/ticket/7961 https://svn.boost.org/trac10/ticket/7961 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-1.53.0b1-asio-epoll-reactor.patch</span> </li> </ul> <p> Suggested patch for boost-1.53b1 </p> Ticket anonymous Mon, 04 Mar 2013 01:53:13 GMT <link>https://svn.boost.org/trac10/ticket/7961#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7961#comment:1</guid> <description> <p> We're seeing this happen with Boost 1.50.0 in our newly-ported-to-Linux code, and it's having some other effects as well. </p> <p> When the erroneous connect occurs, we start sending data and getting reads for the same data we're sending out. It looks like the send and receive buffers are getting confused, although it'll be a little while before we have time to dig in and created a minimal test case. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Fri, 24 May 2013 03:54:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7961#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7961#comment:2</guid> <description> <p> Fixed on trunk in <a class="changeset" href="https://svn.boost.org/trac10/changeset/84349" title="Fix implementation of asynchronous connect operation so that it can ...">[84349]</a>. Merged to release in <a class="changeset" href="https://svn.boost.org/trac10/changeset/84388" title="Merge from trunk. ...">[84388]</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Fri, 24 May 2013 03:54:56 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7961#comment:3 https://svn.boost.org/trac10/ticket/7961#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket