Crash when session being destroyed in one thread while UpdateLock is being acquired in another thread
Added by Anonymous over 13 years ago
Hi Guys,
I got a boost::bad_weak_ptr exception thrown in the following scenario. Wt is trying to destroy a WebSession - the stack trace is shown below:
tbuWtGui.dll!Tbu::WApplication::finalize() Line 123 C++
tbuWtGui.dll!Wt::WebSession::notify(const Wt::WEvent & event={...}) Line 1081 C++
tbuWtGui.dll!Wt::WApplication::notify(const Wt::WEvent & e={...}) Line 1177 C++
tbuWtGui.dll!Wt::WebSession::~WebSession() Line 132 C++
tbuWtGui.dll!Wt::WebSession::`scalar deleting destructor'() + 0x31 bytes C++
tbuWtGui.dll!boost::checked_delete<Wt::WebSession>(Wt::WebSession * x=0x000000000949b5b0) Line 34 + 0x2b bytes C++
tbuWtGui.dll!boost::detail::sp_counted_impl_p<Wt::WebSession>::dispose() Line 79 C++
tbuWtGui.dll!boost::detail::sp_counted_base::release() Line 103 C++
tbuWtGui.dll!boost::detail::shared_count::~shared_count() Line 221 C++
tbuWtGui.dll!boost::shared_ptr<Wt::WebSession>::~shared_ptr<Wt::WebSession>() + 0x31 bytes C++
tbuWtGui.dll!boost::shared_ptr<Wt::WebSession>::reset() Line 387 C++
tbuWtGui.dll!Wt::WebController::handleAsyncRequest(Wt::WebRequest * request=0x0000000009466b70) Line 518 C++
tbuWtGui.dll!Wt::WebController::handleRequest(Wt::WebRequest * request=0x0000000009466b70) Line 385 C++
tbuWtGui.dll!Wt::WServer::handleRequest(Wt::WebRequest * request=0x0000000009466b70) Line 298 C++
tbuWtGui.dll!http::server::WtReply::consumeRequestBody(const char * begin=0x000000001c4f3919, const char * end=0x000000001c4f3990, bool endOfRequest=true) Line 79 C++
tbuWtGui.dll!http::server::RequestParser::parseBody(http::server::Request & req={...}, boost::shared_ptr<http::server::Reply> * reply=0x00000000038df388, const char * & begin=0x000000001c4f3754, const char * end=0x000000001c4f3990) Line 119 C++
tbuWtGui.dll!http::server::Connection::handleReadBody() Line 135 + 0x8a bytes C++
The above call to Tbu::WApplication::finalize() has blocked while trying to acquire a lock that is shared with my thread that performs "server push" functions. Clearly I need to synchronise these two threads so I don't make changes to applications that are being destroyed. A call to WApplication::finalise() is the first notification I receive that the application and associated session are to be destroyed.
Meanwhile, my "server push" thread is busy making changes to widgets, calling getUpdateLock() and triggerChanged() as required - it does this while holding the lock that the thread noted above is waiting on. The following call stack from the "server push" thread shows where things go wrong:
tbuWtGui.dll!boost::throw_exception<boost::bad_weak_ptr>(const boost::bad_weak_ptr & e={...}) Line 81 + 0x4d bytes C++
tbuWtGui.dll!boost::detail::shared_count::shared_count(const boost::detail::weak_count & r={...}) Line 421 + 0x23 bytes C++
tbuWtGui.dll!boost::shared_ptr<Wt::WebSession>::shared_ptr<Wt::WebSession><Wt::WebSession>(const boost::weak_ptr<Wt::WebSession> & r={...}) Line 216 C++
tbuWtGui.dll!boost::enable_shared_from_this<Wt::WebSession>::shared_from_this() Line 49 + 0xf bytes C++
tbuWtGui.dll!Wt::UpdateLockImpl::UpdateLockImpl(Wt::WApplication & app={...}) Line 1050 + 0x49 bytes C++
tbuWtGui.dll!Wt::WApplication::UpdateLock::UpdateLock(Wt::WApplication & app={...}) Line 1080 + 0x26 bytes C++
tbuWtGui.dll!Wt::WApplication::getUpdateLock() Line 1041 + 0xf bytes C++
When my "Server push" thread tries to acquire the Update Lock, it creates a shared_ptr to the WebSession. However, the WebSession is already being destructed in the other thread! At the point that the exception was thrown, the use count of the shared_ptr was zero and the weak_count was 2.
It seems to me that either WApplication::finalize() needs to be called before the WebSession destructs or WApplication::getUpdateLock() should not involve the WebSession.
This has occurred on the git version of Wt.
Cheers,
Dan
Replies (1)
RE: Crash when session being destroyed in one thread while UpdateLock is being acquired in another thread - Added by Anonymous over 13 years ago
Oops - meant to add this as an "Issue" - can it be moved?
Dan