Boost C++ Libraries: Ticket #8290: Python GIL not acquired before freeing shared_ptr, causes crash https://svn.boost.org/trac10/ticket/8290 <p> We're successfully using boost::python with Python on multiple threads that lock and release the GIL as appropriate, and correctly set the Python thread state, etc. </p> <p> One thing we don't have control over, however, is the freeing of shared_ptr objects which were created by boost::python. This may happen on a different thread than the previous Python execution and/or without the GIL being locked. To fix this, the following patch is required: </p> <pre class="wiki">--- builtin_converters.cpp 2011-06-07 06:15:33.000000000 +0200 +++ builtin_converters.cpp.fixed 2013-03-13 12:57:29.346638173 +0100 @@ -32,7 +32,9 @@ void shared_ptr_deleter::operator()(void const*) { + PyGILState_STATE gil = PyGILState_Ensure(); owner.reset(); + PyGILState_Release(gil); } namespace </pre><p> This fix can be applied externally to boost::python, but requires a few header files that re-implement shared_ptr_to_python.hpp and shared_ptr_from_python.hpp. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8290 Trac 1.4.3 eu@… Tue, 28 Jan 2014 13:30:18 GMT <link>https://svn.boost.org/trac10/ticket/8290#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:1</guid> <description> <p> I am bumping into this issue, having crash with this traceback </p> <pre class="wiki"> boost::python::converter::shared_ptr_deleter::operator()(void const*) () // CRASH here boost::detail::sp_counted_base::release ~shared_count </pre><p> It is a python-instantiated object which is deleted in c++ because its refcount drops to zero. I also confirm that locking the GIL solves the issue thanks for the tip). Would you mind sharing your workaround? </p> <p> Cheers! </p> </description> <category>Ticket</category> </item> <item> <author>eu@…</author> <pubDate>Tue, 28 Jan 2014 13:45:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:2</guid> <description> <p> (The workaround I mean is how to apply this to boost::python externally -- I need to support linking against officially compiled libs.) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dane Springmeyer</dc:creator> <pubDate>Wed, 02 Apr 2014 00:21:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:3</guid> <description> <p> Any chance this will be applied? I'm also seeing this crash in Mapnik. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 24 Jun 2014 04:18:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:4</guid> <description> <p> Please, can someone apply this patch? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 12 Aug 2014 20:58:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:5</guid> <description> <p> Any change someone can look at and apply this patch? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 13 Aug 2014 06:37:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:6</guid> <description> <p> Could you please try the Python C++-SIG to get help? I don' know if this change is safe. Sorry. </p> </description> <category>Ticket</category> </item> <item> <author>leaf.nunes@…</author> <pubDate>Tue, 05 May 2015 23:02:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:7</guid> <description> <p> This seems to be a bigger problem with python 2.7; is there an objection to this patch? My site is having to re-apply a different patch to work around the same issue (every time we install a new version of boost). </p> </description> <category>Ticket</category> </item> <item> <author>luke.titley@…</author> <pubDate>Tue, 29 Sep 2015 04:17:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:8</guid> <description> <p> We're having to hack around this too. It's a problem with boost::python not python. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 29 Sep 2015 07:23:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:9</guid> <description> <p> I posted to c++-sig in May, those folks are afraid that it might cause deadlocks <a class="ext-link" href="http://code.activestate.com/lists/python-cplusplus-sig/17196/"><span class="icon">​</span>http://code.activestate.com/lists/python-cplusplus-sig/17196/</a> . Please someone else post a follow-up there, this Trac is abandoned (we can move to github if someone opens an issue in <a class="ext-link" href="https://github.com/boostorg/python/issues"><span class="icon">​</span>https://github.com/boostorg/python/issues</a>). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 29 Sep 2015 07:24:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:10</guid> <description> <p> PS there is a pull request to fix it (with an interesting discussion) at <a class="ext-link" href="https://github.com/boostorg/python/pull/11"><span class="icon">​</span>https://github.com/boostorg/python/pull/11</a> . </p> </description> <category>Ticket</category> </item> <item> <author>luke.titley@…</author> <pubDate>Wed, 30 Sep 2015 04:44:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8290#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8290#comment:11</guid> <description> <p> Thanks for this. For now we've patched boost python. </p> </description> <category>Ticket</category> </item> </channel> </rss>