Opened 5 years ago
Closed 5 years ago
#13283 closed Bugs (invalid)
boost log crash during thread termination
Reported by: | Owned by: | Andrey Semashev | |
---|---|---|---|
Milestone: | To Be Determined | Component: | log |
Version: | Boost 1.66.0 | Severity: | Problem |
Keywords: | Cc: |
Description
I'm using boost 1.64 on iOS and Android. Adding trivial log causes a crash during thread termination.
Removing more and more code produces the follwing example code for reproducing the issue. In the default settings in XCode it randomly crashed.
Enabling all memory related helper in the Scheme (memory guards, ...) helps because it crashes more reproducible and procudes better callstacks.
My result of debugging is:
- the first executed log line -> creates a thread local storage object with the severity - this severity is stored in severity_level_holder which creates a thread local storage with an automatic deleter (boost::this_thread::at_thread_exit(boost::bind(checked_deleter< uintmax_t >(), p));) - saw this in severity_level.cpp
- normal Code -> logging works
- thread terminates
- the destructor of the TestClass is called and adds a result to the internal promise -> logging works
- the automatic deleter is called and frees the memory block for the severity.
- the TestClass of the internal promise is destroyed -> logging crashes because it tries to read the destroyed severity memory block.
#include <memory> #ifndef BOOST_THREAD_PROVIDES_FUTURE #define BOOST_THREAD_PROVIDES_FUTURE #endif #ifndef BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #endif #include <boost/thread/thread.hpp> #include <boost/thread/future.hpp> #include <boost/log/trivial.hpp> class TestClass { public: TestClass(bool shouldSet) : shouldSet_(shouldSet) {} virtual ~TestClass() { if (shouldSet_) { BOOST_LOG_TRIVIAL(warning) << "crashes here"; promise_.set_value(std::unique_ptr<TestClass>(new TestClass(false))); } } bool shouldSet_; boost::promise<std::unique_ptr<TestClass>> promise_; }; int main(int, char **) { #define N 1 auto wait_future = boost::async([](){ auto task = []() { for (int i=0;i<1000;i++) { BOOST_LOG_TRIVIAL(warning) << "Create event #" << i; std::shared_ptr<TestClass> event = std::shared_ptr<TestClass>(new TestClass(false)); boost::future<std::unique_ptr<TestClass>> future = event->promise_.get_future(); future.then([i](boost::future<std::unique_ptr<TestClass>>){BOOST_LOG_TRIVIAL(warning) << "event handled #" << i; }); boost::async([event, i](){ event->promise_.set_value(std::unique_ptr<TestClass>(new TestClass(true))); BOOST_LOG_TRIVIAL(warning) << "Destroy event #" << i; }).wait(); } }; boost::future<void> f[N]; for(int j=0; j<N; j++){ f[j] = boost::async(task); } for(int j=0; j<N; j++){ f[j].wait(); } }); wait_future.wait_for(boost::chrono::seconds(10)); return 0; }
Change History (3)
comment:1 by , 5 years ago
Version: | Boost 1.64.0 → Boost 1.66.0 |
---|
comment:2 by , 5 years ago
Component: | None → log |
---|---|
Owner: | set to |
comment:3 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
So the problem seems to be that the code attempts to write a log record after the thread-local data has been destroyed, am I correct? If so, I'm sorry, but this use case is not going to be supported. Like with global destructors on program termination, there's generally no way to make sure that a thread-local object is still alive while the thread terminates and attempting to log at this point will always be unreliable. My advice is to avoid this practice and finish logging before the thread terminates.
I've tested it again with boost 1.66.0 and the behaviour is the same. I also tried to compile boost with BOOST_LOG_USE_COMPILER_TLS but the behaviour is still the same.
My results after more debugging: