{"id":4494,"date":"2010-12-23T10:58:27","date_gmt":"2010-12-23T16:58:27","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=4494"},"modified":"2010-12-23T13:59:33","modified_gmt":"2010-12-23T19:59:33","slug":"tracking-down-an-annoying-boost-asio-problem","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/4494","title":{"rendered":"Tracking Down an Annoying Boost ASIO Problem"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2010\/07\/boost_logo.png\" alt=\"Boost C++ Libraries\" title=\"boost_logo.png\" border=\"0\" width=\"115\" height=\"109\" style=\"float:right;\" \/><\/p>\n<p>In the midst of working on the changes necessitated by the Broker's re-write, I found myself in a very nasty little problem. I am trying to do things quickly, and my test cases are often far worse than any real-world use is going to be, but they have served me well, and they were pointing out a problem I was having this morning.<\/p>\n<p>If I created new updater instances for each request, and deleted them after they were no longer needed, I ended up with a very fast create-use-delete lifecycle. This lead to a segmentation fault in boost's <tt>io_service<\/tt> - specifically, in it's <tt>run()<\/tt> method. The core dumps were of little to no help whatsoever, and I was left trying to diagnose the problem from my end.<\/p>\n<p>If I didn't delete them right away, but threw them into a pool, and still created new ones, only clearing out the pool at the end of the application, then everything was fine. It seemed like it was just the short lifecycle connections that was the problem. Very nasty.<\/p>\n<p>The seg faults weren't on anything related to boost asio, either. They were on the line right after the context switch <strong><em>after<\/em><\/strong> the closing of the socket connection. I spent hours debugging the code to find that guy.<\/p>\n<p>I came to the conclusion that there was something in the <tt>io_service<\/tt> that wasn't getting a \"chance\" to handle the socket closing before I deleted it. So I changed my code ever so slightly. Originally I had:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>si<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>second.<span style=\"color: #007788;\">pool<\/span>.<span style=\"color: #007788;\">size<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #000080;\">&gt;=<\/span> MAX_UPDATERS_IN_POOL<span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">&#123;<\/span>\n    cLog.<span style=\"color: #007788;\">debug<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #FF0000;\">&quot;[recycle] the pool is full - deleting the updater&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #0000dd;\">delete<\/span> anUpdater<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><\/pre>\n<p>to:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>si<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>second.<span style=\"color: #007788;\">pool<\/span>.<span style=\"color: #007788;\">size<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #000080;\">&gt;=<\/span> MAX_UPDATERS_IN_POOL<span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">&#123;<\/span>\n    cLog.<span style=\"color: #007788;\">debug<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #FF0000;\">&quot;[recycle] the pool is full - deleting the updater&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    anUpdater<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>mChannelID.<span style=\"color: #007788;\">clear<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    anUpdater<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>disconnect<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ we need to let the io_service have a go at it<\/span>\n    boost<span style=\"color: #008080;\">::<\/span><span style=\"color: #0000dd;\">system<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">error_code<\/span>  err<span style=\"color: #008080;\">;<\/span>\n    mIOService.<span style=\"color: #007788;\">poll_one<\/span><span style=\"color: #008000;\">&#40;<\/span>err<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ ...and now we can successfully delete the updater<\/span>\n    <span style=\"color: #0000dd;\">delete<\/span> anUpdater<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><\/pre>\n<p>The difference was <strong><em>stunning<\/em><\/strong>. No more crashes, and the code was rock solid every time I ran it. Amazing. I'm going to have to remember this. It's like a little context switch for the <tt>io_service<\/tt> so it can detect the close of the socket before it's deleted.<\/p>\n<p>Several things to finish on the re-write, but it's getting close now. Nice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the midst of working on the changes necessitated by the Broker&#8217;s re-write, I found myself in a very nasty little problem. I am trying to do things quickly, and my test cases are often far worse than any real-world use is going to be, but they have served me well, and they were pointing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-4494","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=4494"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4494\/revisions"}],"predecessor-version":[{"id":4495,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4494\/revisions\/4495"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=4494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=4494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=4494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}