Skip to content

Fix thread cleanup and memory leaks during partial initialization#3959

Open
sprangerik wants to merge 1 commit into
cisco:masterfrom
sprangerik:thread_pool_tear_down
Open

Fix thread cleanup and memory leaks during partial initialization#3959
sprangerik wants to merge 1 commit into
cisco:masterfrom
sprangerik:thread_pool_tear_down

Conversation

@sprangerik

Copy link
Copy Markdown
Contributor

If CWelsThreadPool::Init() fails halfway through creating worker threads, StopAllRunning() returns an error code because the number of idle threads does not match m_iMaxThreadNum. Previously, Uninit() would return early on this error code without terminating or destroying the worker threads that were already successfully started, leaving them running in the background after the pool was destroyed.

This PR updates Uninit() to systematically terminate, join, and clean up all worker threads across both idle and busy lists regardless of StopAllRunning()'s return value. Additionally, if pThread->Start() fails in CreateIdleThread(), the allocated thread object is now freed before returning.

If CWelsThreadPool::Init() fails halfway through creating worker threads, StopAllRunning() returns an error code because the number of idle threads does not match m_iMaxThreadNum. Previously, Uninit() would return early on this error code without terminating or destroying the worker threads that were already successfully started, leaving them running after the pool was destroyed.

This commit updates Uninit() to systematically terminate, join, and clean up all worker threads across both idle and busy lists regardless of StopAllRunning()'s return value. Additionally, if pThread->Start() fails in CreateIdleThread(), the allocated thread object is now freed before returning.
@sprangerik sprangerik force-pushed the thread_pool_tear_down branch 2 times, most recently from f9870ab to aa0c791 Compare July 9, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant