Skip to content

Crash in ThreadPool during shutdown #1462

@hgroenenboom

Description

@hgroenenboom

Description

When exiting my unit test application that uses libdatachannel, the ThreadPool crashes inside a mutex lock call. This only happens if there are pending tasks during shutdown. I am running in Debug mode, so this mutex seems to be a mutex around the debug structure of the deque iterator.

Currently it seems the threads are still running after/during static deinitialization, which causes a crash when the threads are accessing members members of ThreadPool.

Also the destructor of ThreadPool is never ran, because it is never deleted unless cleanup() is actively called

Stacktrace

_Mtxlock(_RTL_CRITICAL_SECTION * _Mtx) Line 25	C++
std::_Lockit::_Lockit(int kind) Line 76	C++
std::_Iterator_base12::_Adopt_locked(const std::_Container_base12 * _Parent) Line 1355	C++
std::_Iterator_base12::_Adopt(const std::_Container_base12 * _Parent) Line 1295	C++
std::_Deque_const_iterator<std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>>>::_Setcont(const std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>> * _Pdeque) Line 393	C++
std::_Deque_const_iterator<std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>>>::_Deque_const_iterator<std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>>>(unsigned __int64 _Off, const std::_Container_base12 * _Pdeque) Line 253	C++
std::_Deque_iterator<std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>>>::_Deque_iterator<std::_Deque_val<std::_Deque_simple_types<rtc::impl::ThreadPool::Task>>>(unsigned __int64)	C++
std::deque<rtc::impl::ThreadPool::Task,std::allocator<rtc::impl::ThreadPool::Task>>::end() Line 911	C++
std::priority_queue<rtc::impl::ThreadPool::Task,std::deque<rtc::impl::ThreadPool::Task,std::allocator<rtc::impl::ThreadPool::Task>>,std::greater<rtc::impl::ThreadPool::Task>>::pop() Line 424	C++
rtc::impl::ThreadPool::dequeue() Line 82	C++
rtc::impl::ThreadPool::runOne() Line 66	C++
rtc::impl::ThreadPool::run() Line 61	C++

Environment

OS: Windows 11
IDE: Visual Studio 2022
libdatachannel: 0.23.2
Build flags: OPENSSL_USE_STATIC_LIBS, NO_MEDIA

Workarounds

Explicitly join the threads in the ThreadPool::~ThreadPool to avoid std::terminate and ensure ThreadPool destructor is called (missing delete):

ThreadPool &ThreadPool::Instance() {
	static ThreadPool instance;
	return instance;
}

ThreadPool::ThreadPool() {}

ThreadPool::~ThreadPool() 
{ 
	join();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions