Boost C++ Libraries: Ticket #4639: boost thread library leaks pthread_key https://svn.boost.org/trac10/ticket/4639 <p> implementation on Linux of boost::thread_specific_ptr calls pthread_key_create, but never calls pthread_key_delete. </p> <p> On RHEL 4.6 system pthread_key_create succeeds 1024 times in a row without calling pthread_key_delete. After that point pthread_key_create fails. </p> <p> Basically there are 2 issues: </p> <ol><li>resource leak - boost thread should release acquired resource </li><li>When/if pthread_key_create fails - current (1.44.0) implementation completely ignores return code in release version. </li></ol><p> I am attaching test case to illustrate/reproduce problem. </p> <p> Debug version yields: <em>attempt 512 boost_thread_test_deb.exe: libs/thread/src/pthread/once.cpp:32: void boost::detail::&lt;unnamed&gt;::create_epoch_tss_key(): Assertion `!pthread_key_create(&amp;epoch_tss_key,delete_epoch_tss_data)' failed. Aborted </em> While release version results in following: <em>attempt 512 Segmentation fault</em> </p> <p> Here is a brief explanation of why I need to dlopen/dlclose same shared object repeatedly: </p> <p> I am working on apache's module and I am using boost::thread_specific_ptr in my module. I am linking with boost thread library statically. </p> <p> In our organization apache is being gracefully restarted (apachectl graceful) every hour. During every restart my module is being unloaded (along with boost thread) and loaded back again. This usage scenario gives me 512 restarts/reloads, which translates to 21 days before it segfaults. </p> <p> After closer examination I found out that even if do not boost::thread_specific_ptr use directly - I am still subject to this problem as boost::thread also uses it. All of the above prevents me from using boost thread library in my project. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4639 Trac 1.4.3 Leonid Gershanovich <gleonid@…> Wed, 08 Sep 2010 21:14:17 GMT attachment set https://svn.boost.org/trac10/ticket/4639 https://svn.boost.org/trac10/ticket/4639 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">tss_test_case.tar.gz</span> </li> </ul> Ticket viboes Sun, 04 Dec 2011 17:08:49 GMT owner, status changed; cc set https://svn.boost.org/trac10/ticket/4639#comment:1 https://svn.boost.org/trac10/ticket/4639#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> added </li> <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> <p> When the pthread_key_delete should be called? At exit? </p> Ticket anonymous Sun, 04 Dec 2011 20:05:59 GMT <link>https://svn.boost.org/trac10/ticket/4639#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4639#comment:2</guid> <description> <p> If I am not mistaken, pthread_key_create is called from 2 places: </p> <ol><li>pthread_key_create(&amp;epoch_tss_key... </li><li>pthread_key_create(&amp;current_thread_tls_key, ... ) </li></ol><p> (at the moment I am looking at code from 1.48.0 release) </p> <p> I guess that for both keys appropriate point to call pthread_key_delete would be at moment when boost thread library is being unloaded (or process is being terminated). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 09 Dec 2011 17:50:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4639#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4639#comment:3</guid> <description> <p> I'm not aware of library load/unload interfaces. </p> <p> In another ticket, someone has proposed to use <span class="underline">attribute</span>(destructor) or something like that when using gcc. How can a function be called when the library is unloaded with other compilers? Could you give me a pointer or better yet, could you prepare a patch? Which compiler are you using? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 09 Dec 2011 19:07:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4639#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4639#comment:4</guid> <description> <p> if you have a global object, which has a dtor. that dtor will get called when module is unloaded, regardless of a compiler. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 11 Dec 2011 01:47:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4639#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4639#comment:5</guid> <description> <p> Do you mean something like adding this to once.cpp </p> <pre class="wiki"> namespace { const pthread_once_t pthread_once_init_value=PTHREAD_ONCE_INIT; struct BOOST_THREAD_DECL delete_epoch_tss_key_on_dlclose_t { ~delete_epoch_tss_key_on_dlclose_t() { if(memcmp(&amp;epoch_tss_key_flag, &amp;pthread_once_init_value, sizeof(pthread_once_t))) { pthread_key_delete(epoch_tss_key); } } }; delete_epoch_tss_key_on_dlclose_t delete_epoch_tss_key_on_dlclose; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 11 Dec 2011 02:06:45 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4639#comment:6 https://svn.boost.org/trac10/ticket/4639#comment:6 <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">duplicate</span> </li> </ul> <p> Even if the problem doesn't manifest in the same way, it seems the solution should be be same. Duplicate of <a class="reopened ticket" href="https://svn.boost.org/trac10/ticket/3926" title="#3926: Bugs: thread_specific_ptr + dlopen library causes a SIGSEGV. (reopened)">#3926</a> thread_specific_ptr + dlopen library causes a SIGSEGV. </p> Ticket