Boost C++ Libraries: Ticket #7923: Multiple consecutive named_mutex::remove https://svn.boost.org/trac10/ticket/7923 <p> I have three named_mutex that need to be removed from system in the same inizialization function to ensure the unique existence of the named resources. </p> <p> The problem appear with multiple consecutive calls to named_mutex::remove with the three named_mutex, obtaining "false" return value randomly. </p> <p> I can see that named_mutex::remove use a temporal file and rename it with a name generated randomly, using process/system info data, however I obtain the same generated name in two consecutive calls and consequently remove method fails </p> <p> Maybe a precision issue with time used to construct the randomly generated name. </p> <p> I make a simple program to reproduce the problem. With this example you can obtain "false" sometimes in the first iteration, sometimes at iteration 11000, 23000, 90000... </p> <pre class="wiki">#include &lt;boost/interprocess/sync/named_mutex.hpp&gt; #include &lt;boost/interprocess/exceptions.hpp&gt; #include &lt;boost/scoped_ptr.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;boost/thread.hpp&gt; using namespace std; void sharedMemoryTest() { boost::scoped_ptr&lt;boost::interprocess::named_mutex&gt; mutex; boost::scoped_ptr&lt;boost::interprocess::named_mutex&gt; mutex2; std::string mutexName = "ASYSTEM_00000_mutex"; std::string mutexName2 = "ASYSTEM_00001_mutex"; bool error = false; int count = 0; while(!error) { try { mutex.reset(new boost::interprocess::named_mutex(boost::interprocess::create_only, mutexName.c_str())); error = error || !boost::interprocess::named_mutex::remove(mutexName.c_str()); mutex2.reset(new boost::interprocess::named_mutex(boost::interprocess::create_only, mutexName2.c_str())); error = error || !boost::interprocess::named_mutex::remove(mutexName2.c_str()); ++count; } catch(boost::interprocess::interprocess_exception&amp; ie) { cout&lt;&lt;"Error "&lt;&lt;ie.what() &lt;&lt;" on iteration: "&lt;&lt;count&lt;&lt;std::endl; error = true; } } cout&lt;&lt;"Error on iteration: "&lt;&lt;count&lt;&lt;std::endl; } int _tmain(int argc, _TCHAR* argv[]) { sharedMemoryTest(); return 0; } </pre><p> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7923 Trac 1.4.3 cbarrioy <cbarrioy@…> Fri, 25 Jan 2013 08:13:46 GMT component changed; owner set https://svn.boost.org/trac10/ticket/7923#comment:1 https://svn.boost.org/trac10/ticket/7923#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Ion Gaztañaga</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">interprocess</span> </li> </ul> Ticket kamesh <kameshdeepakbetablet@…> Sun, 27 Jan 2013 13:30:02 GMT <link>https://svn.boost.org/trac10/ticket/7923#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7923#comment:2</guid> <description> <p> I was able to re-produce the issue on cygwin. The "0" iteration fails with message "Error File exists on iteration: 0". Now the problem is there is no directory called /tmp/boost_interprocess , but still it fails and $TMP=/tmp </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Sun, 27 Jan 2013 16:43:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7923#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7923#comment:3</guid> <description> <p> Thanks for the report, the test case and the time precission problem hint. I expecto to have some time to investigate this the following days. </p> <p> @kamesh:I don't think the cygwin issue is the same as the one pointed by the reporter. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Mon, 28 Jan 2013 18:21:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7923#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7923#comment:4</guid> <description> <p> Please, can you try with the attached modified file? It includes a new unlink implementation with the following changes: </p> <ul><li>A static atomically incremented count is added to each rename operation. </li><li>Memory allocation is minimized querying the win kernel instead of allocatin a 32K buffer. </li><li>Delete on close flag is applied only if renaming was successful. </li></ul> </description> <category>Ticket</category> </item> <item> <author>cbarrioy <cbarrioy@…></author> <pubDate>Wed, 30 Jan 2013 00:09:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7923#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7923#comment:5</guid> <description> <p> I have tested the attached file for many hours and everything seems to work fine, however I think that it could have some problem if two parallel processes(both started at the same time) try to remove two different named resources at the same time, the static counter will have the same value for both processes. Maybe some type of process identifier is needed for this case too, like PID. </p> <p> what do you think? Anyway, nice work! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 07 Feb 2013 19:54:42 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/7923 https://svn.boost.org/trac10/ticket/7923 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">win32_api.hpp</span> </li> </ul> <p> New try adding process id to the random name </p> Ticket Ion Gaztañaga Thu, 07 Feb 2013 19:55:37 GMT <link>https://svn.boost.org/trac10/ticket/7923#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7923#comment:6</guid> <description> <p> I guess adding the current process id to the name could do the trick. See attached file for proposed solution. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Sat, 09 Feb 2013 17:48:32 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7923#comment:7 https://svn.boost.org/trac10/ticket/7923#comment:7 <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> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/82797" title="Fixes #7923">[82797]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7923" title="#7923: Bugs: Multiple consecutive named_mutex::remove (closed: fixed)">#7923</a> </p> Ticket