Boost C++ Libraries: Ticket #5311: io_service::post/dispatch, io_service::stop, deadline_timer crashes https://svn.boost.org/trac10/ticket/5311 <p> I've used programs using boost::asio before, but never used it directly myself. I made a test program using boost::asio that models communications from a synchronous TCP server to an asynchronous TCP client. </p> <p> &lt;Long version&gt; The program launches one thread for the server, which creates an acceptor and calls async_accept; then runs the io_service. Upon accepting a new connection with success, it immediately begins an async read on the new connection and another async_accept. On the original thread, I create a TCP socket (using a separate io_service), connect it to the server, and send a packet saying "Hello, Server". When the server thread receives this "Hello, Server" packet, it responds with a "Hello, Client" packet and begins another async_read with a newly-allocated buffer. When the client receives this "Hello, Client" packet, since the test program has nothing else to do, I begin cleaning up by calling io_service::stop on the server's io_service. That crashed. I guessed that maybe it was my fault, so instead I called service.post(bind(&amp;io_service::stop, &amp;service));. This crashed, so I tried the same with service.dispatch(). Now I was confused, so I tried a third time with a deadline_timer that expired after 1 millisecond. This also crashes. </p> <p> &lt;Short version&gt; io_service::post, io_service::dispatch, and io_service::stop are all crashing when called from a thread other than the one running the io_service. An async_read and async_accept are in progress, but no reading or connecting is being done. deadline_timer::async_wait() crashes when called from a thread other than the one calling io_service::run </p> <p> Also, none of these crashes are caught by try{}catch(){}. </p> <p> &lt;Compiler and System Information&gt; Compiler: TDM-MinGW GCC 4.4.1 32-bit Compiler Flags: -Wall -s -O2 Boost Version: 1.46.0 System: Windows 7 Home Premium 64-bit CPU: AMD Turion II P540 (dual-core, 2.40ghz) </p> <p> &lt;Information from the crash pop-up&gt; Problem signature: </p> <blockquote> <p> Problem Event Name: APPCRASH Application Name: nettest.exe Application Version: 0.0.0.0 Application Timestamp: 4d7fc897 Fault Module Name: nettest.exe Fault Module Version: 0.0.0.0 Fault Module Timestamp: 4d7fc897 Exception Code: c0000005 Exception Offset: 0002c702 OS Version: 6.1.7600.2.0.0.768.3 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789 </p> </blockquote> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5311 Trac 1.4.3 chris_kohlhoff Wed, 16 Mar 2011 01:13:46 GMT <link>https://svn.boost.org/trac10/ticket/5311#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5311#comment:1</guid> <description> <p> Please supply some minimal sample code exhibiting the problem. Unfortunately, your description is insufficient to be able say where the problem lies. </p> </description> <category>Ticket</category> </item> <item> <author>nfries88@…</author> <pubDate>Wed, 16 Mar 2011 02:49:57 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5311 https://svn.boost.org/trac10/ticket/5311 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">net.zip</span> </li> </ul> <p> full test program source code </p> Ticket nfries88@… Wed, 16 Mar 2011 02:51:09 GMT <link>https://svn.boost.org/trac10/ticket/5311#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5311#comment:2</guid> <description> <p> I attached the full source code for the test program, define <span class="underline">NETTEST</span> to have the server send the response. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Wed, 16 Mar 2011 03:51:32 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5311#comment:3 https://svn.boost.org/trac10/ticket/5311#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">invalid</span> </li> </ul> <p> If you use a debugger you will probably see that the myservice pointer is always null. Specifically, your Service class's constructor calls doWork() which in turn calls io_service.run(). This means the constructor never returns, and the myservice global is never set in runServer(). </p> <p> N.B. Even if you move the doWork() call out of the constructor, you will still have a race condition in the initialisation of the myservice global. </p> <p> Unrelated: if you stop your service in this way you will also leak the Connection object that is created in Service::accept(). </p> Ticket anonymous Wed, 16 Mar 2011 19:31:40 GMT <link>https://svn.boost.org/trac10/ticket/5311#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5311#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5311#comment:3" title="Comment 3">chris_kohlhoff</a>: </p> <blockquote class="citation"> <p> N.B. Even if you move the doWork() call out of the constructor, you will still have a race condition in the initialisation of the myservice global. <br /> </p> </blockquote> <p> why is that? I'm not having any issues.<br /> <br /> And sorry to waste your time with an invalid bug. But thanks to your input and a little more reading of the documentation, it seems to be stable now. </p> </description> <category>Ticket</category> </item> <item> <author>kyaw.tech@…</author> <pubDate>Tue, 22 Oct 2013 06:32:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5311#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5311#comment:5</guid> <description> <p> Subject : C++ Dealing with Concurrency using Boost.Asio (Visual Studio 2008 SP1) </p> <p> Dear Chris, </p> <p> I am looking for the concrete advice or solution for the C++ Dealing with Concurrency using Boost.Asio. </p> <p> Since my APIs need to communicate between multiple(lots of) clients (workstations) and servers. </p> <p> There are four ways of handling concurrency in the document named using Asio with C++ 11. </p> <p> 1) Single Thread </p> <p> 2) Use Threads for long running tasks </p> <p> 3) Multiple io_sevices, one thread each </p> <p> 4) one io_service, multiple threads </p> <p> I am very concern about the throughput and latency for network traffic because of heavy data transactions. </p> <p> I hope some one will help me to close the case asap since I need to proceed further. </p> <p> Thanks and best regards </p> </description> <category>Ticket</category> </item> </channel> </rss>