Boost C++ Libraries: Ticket #9787: [windows] Small duration value passed down to basic_timed_mutex::try_lock_until and condition_variable::wait_until can cause infinite or near infinite wait for win32 https://svn.boost.org/trac10/ticket/9787 <p> Creating a unique_lock using a timed_mutex which is passed a small chrono::milliseconds duration value (10 milliseconds in our case) can cause a very large value to be passed to <a class="missing wiki">WaitForSingleObject</a> for the dwMilliseconds parameter. The code path creates an initial chrono::time_point where the duration value is added to the current time. Once it gets to try_lock_until it calculates a new rel_time duration to feed to <a class="missing wiki">WaitForSingleObject</a>. If the small duration has elapsed due to thread switching then the subtraction results in a very large positive value to be passed to <a class="missing wiki">WaitForSingleObject</a>. Similarly, if we calculate our own boost::chrono::system_clock::time_point and pass it to boost::condition_variable::wait_until and the time_point has passed then the subtraction to compute a relative time value can result in a very large positive value. Another possibility is if the caller inadvertently passes an elapsed time_point to condition_variable::wait_until. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9787 Trac 1.4.3 viboes Tue, 18 Mar 2014 21:46:44 GMT <link>https://svn.boost.org/trac10/ticket/9787#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:1</guid> <description> <p> Is this a duplicate of <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9618" title="#9618: Bugs: try_join_for problem: program is not terminate. (closed: fixed)">#9618</a>? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 18 Mar 2014 21:47:50 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/9787#comment:2 https://svn.boost.org/trac10/ticket/9787#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Bryan Laird <bryan_laird@…> Tue, 18 Mar 2014 22:15:31 GMT <link>https://svn.boost.org/trac10/ticket/9787#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:3</guid> <description> <p> This ticket does not describe an issue with thread::try_join_for or thread::try_join_until so it is not a duplicate of <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9618" title="#9618: Bugs: try_join_for problem: program is not terminate. (closed: fixed)">#9618</a>. However what's described in comment 9 of that ticket sounds similar in nature. The patch in comment 10 of that ticket would not fix this problem. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 19 Apr 2014 11:12:36 GMT</pubDate> <title>summary changed https://svn.boost.org/trac10/ticket/9787#comment:4 https://svn.boost.org/trac10/ticket/9787#comment:4 <ul> <li><strong>summary</strong> <span class="trac-field-old">Small duration value passed down to basic_timed_mutex::try_lock_until and condition_variable::wait_until can cause infinite or near infinite wait for win32</span> → <span class="trac-field-new">[windows] Small duration value passed down to basic_timed_mutex::try_lock_until and condition_variable::wait_until can cause infinite or near infinite wait for win32</span> </li> </ul> Ticket viboes Sat, 19 Apr 2014 11:13:01 GMT <link>https://svn.boost.org/trac10/ticket/9787#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:5</guid> <description> <p> Have you tried it? </p> </description> <category>Ticket</category> </item> <item> <author>Bryan Laird <bryan_laird@…></author> <pubDate>Wed, 23 Apr 2014 15:24:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:6</guid> <description> <p> No because we are not calling try_join_for and basic_timed_mutex::try_lock_until and condition_variable::wait_until don't call try_join_for. </p> </description> <category>Ticket</category> </item> <item> <author>Bryan Laird <bryan_laird@…></author> <pubDate>Fri, 25 Apr 2014 21:06:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:7</guid> <description> <p> It appears that this problem is fixed for the condition_variable::wait_until method in version 1.55. </p> <p> The problem still exists for basic_timed_mutex::try_lock_until in 1.55. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 14 Sep 2014 20:21:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:8</guid> <description> <p> Could someone try this patch </p> <pre class="wiki">git diff include/boost/thread/win32/basic_timed_mutex.hpp diff --git a/include/boost/thread/win32/basic_timed_mutex.hpp b/include/boost/thread/win32/basic_timed_mutex.hpp index b55affd..d20c658 100644 --- a/include/boost/thread/win32/basic_timed_mutex.hpp +++ b/include/boost/thread/win32/basic_timed_mutex.hpp @@ -203,7 +203,12 @@ namespace boost do { - chrono::milliseconds rel_time= chrono::ceil&lt;chrono::milliseconds&gt;(tp-chrono::system_clock::now()); + chrono::time_point&lt;chrono::system_clock, chrono::system_clock::duration&gt; now = chrono::system_clock::now(); + if (tp&lt;=now) { + BOOST_INTERLOCKED_DECREMENT(&amp;active_count); + return false; + } + chrono::milliseconds rel_time= chrono::ceil&lt;chrono::milliseconds&gt;(tp-now); if(win32::WaitForSingleObjectEx(sem,static_cast&lt;unsigned long&gt;(rel_time.count()),0)!=0) { </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Wed, 17 Sep 2014 11:09:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:9</guid> <description> <p> I have not been able to replicate this issue with Boost 1.56 release. See the attached test case - it tests both of the issues reported by the OP and soak tests the timed_mutex for thirty seconds across 128 threads. Runs flawlessly without any fixes. </p> <p> OP - please supply a version of the attached test case test_9787 which demonstrates the problem you report. Else we shall close this issue as invalid. </p> <p> Niall </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Wed, 17 Sep 2014 11:09:51 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9787 https://svn.boost.org/trac10/ticket/9787 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_9787.cpp</span> </li> </ul> Ticket Niall Douglas Wed, 17 Sep 2014 11:11:57 GMT <link>https://svn.boost.org/trac10/ticket/9787#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:10</guid> <description> <p> FYI I tested on Win8.1 x86 using VS2013 Update 1. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 18 Sep 2014 05:40:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:11</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9787#comment:10" title="Comment 10">ned14</a>: </p> <blockquote class="citation"> <p> FYI I tested on Win8.1 x86 using VS2013 Update 1. </p> </blockquote> <p> Thanks Niall. </p> <p> Even if the issue is not reproducible in your configuration, could you try the patch to see if there is no regressions on the Thread tests? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Thu, 18 Sep 2014 13:24:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:12</guid> <description> <p> With the patch applied, all unit tests pass including test_9787 on Win8.1 x64 VS2013 Update 1. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 19 Sep 2014 17:03:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:13</guid> <description> <p> Briand please, could you try the patch? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 19 Sep 2014 17:04:11 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:14</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9787#comment:12" title="Comment 12">ned14</a>: </p> <blockquote class="citation"> <p> With the patch applied, all unit tests pass including test_9787 on Win8.1 x64 VS2013 Update 1. </p> </blockquote> <p> Thanks Niall. </p> </description> <category>Ticket</category> </item> <item> <author>Bryan Laird <bryan_laird@…></author> <pubDate>Fri, 19 Sep 2014 17:44:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:15</guid> <description> <p> I'm not sure if "Briand" is me, but I will test this the week of September 22nd. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 23 Sep 2014 00:15:50 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/9787#comment:16 https://svn.boost.org/trac10/ticket/9787#comment:16 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.57.0</span> </li> </ul> Ticket Bryan Laird <bryan_laird@…> Tue, 23 Sep 2014 11:59:19 GMT <link>https://svn.boost.org/trac10/ticket/9787#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:17</guid> <description> <p> The fix looks good. </p> <p> I would prefer to call <a class="missing wiki">WaitForSingleObjectEx</a>() with a 0ms timeout in the case of tp&lt;=now but that's up to you. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 23 Sep 2014 21:15:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9787#comment:18 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9787#comment:18</guid> <description> <p> Thanks for trying it. </p> <p> I will commit this patch. If you have any patch that can improve the current code, please post it here on another ticket. As I said, I need some help on the windows platform. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 26 Sep 2014 06:08:17 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9787#comment:19 https://svn.boost.org/trac10/ticket/9787#comment:19 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket