erofs-utils: fix thread join loop in erofs_destroy_workqueue
PATCH 1/1 erofs-utils: fix thread join loop in erofs_destroy_workqueue
Currently, erofs_destroy_workqueue returns immediately if a single pthread_join fails. This halts the teardown process, potentially leaving orphaned threads and leaking the workqueue's mutexes and worker array.
Refactor the joining logic to unconditionally join all worker threads. Capture the first error encountered, continue joining the remaining threads, and ensure all workqueue resources are properly freed before returning the captured error.
--- lib/workqueue.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
lib/workqueue.c +7 -3
@@ -42,6 +42,8 @@ static void *worker_thread(void *arg)int erofs_destroy_workqueue (struct erofs_workqueue *wq) { +int err =0 ; +if (!wq)return -EINVAL ;
@@ -53,15 +55,17 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)while (wq->nworker) {int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) -return ret; +if (ret && !err) + err = ret; + --wq->nworker; }free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);pthread_cond_destroy (&wq->cond_full); -return 0 ; + +return err; } int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
Signature · 1 line
2.51.0
Subject: Re: [PATCH] erofs-utils: fix thread join loop in erofs_destroy_workqueue
Hi,
I have submitted a patch to fix the thread joining logic within erofs_destroy_workqueue. To ensure the robustness of the teardown process and guarantee no resources are leaked, I performed the following verification and testing:
1) I built mkfs.erofs and successfully compressed a test directory containing various text files using lz4 compression. The image build completed successfully without any hangs or crashes.
2) I ran the same mkfs.erofs compression workload through Valgrind with --leak-check=full and --show-leak-kinds=all. Valgrind reported that "All heap blocks were freed -- no leaks are possible," confirming that the modified teardown loop correctly frees the worker array and other resources.
3) To ensure no orphaned threads or synchronization issues occur during the destruction phase, I ran the workload through Helgrind. The workqueue teardown sequence executed cleanly. (Note:
Helgrind did catch a pre-existing benign data race in compressor_lz4_init unrelated to this patch which I am happy to send a patch for).
4) Finally, I ran the built-in ./contrib/mkstress.sh script to verify behavior under heavier loads, which also completed without issues.
Please let me know if you need any additional testing logs or further modifications to the patch.
Thanks,
Nithurshen
Quoted 11 lines · Nithurshen
Currently,
erofs_destroy_workqueuereturns immediately if a singlepthread_joinfails. This halts the teardown process, potentially leaving orphaned threads and leaking the workqueue's mutexes and worker array.Refactor the joining logic to unconditionally join all worker threads. Capture the first error encountered, continue joining the remaining threads, and ensure all workqueue resources are properly freed before returning the captured error.
Signed-off-by Nithurshen <[email protected]>
Hi Nithurshe,
I'm afraid I cannot agree with this change. If pthread_join() fails, it implies that the worker thread is still alive.
Cleaning up the synchronization primitives and the workers array at this point would lead to a Use-After-Free issue, which is far more severe than the current resource leak.
I believe pthread_join() seldom fails, otherwise it indicates bugs in our codebase.
How about just leaving an error log print for this scenario? cc @hsiangkao
Thanks,
Yifan
Quoted 38 lines
--- lib/workqueue.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)lib/workqueue.c +7
-3@@ -42,6 +42,8 @@ static void *worker_thread(void *arg)int erofs_destroy_workqueue (struct erofs_workqueue *wq) { +int err =0 ; +if (!wq)return -EINVAL ;@@ -53,15 +55,17 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)while (wq->nworker) {int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) -return ret; +if (ret && !err) + err = ret; + --wq->nworker; }free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);pthread_cond_destroy (&wq->cond_full); -return 0 ; + +return err; } int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
Quoted 19 lines · 赵逸凡
Quoted 11 lines · Nithurshen
Currently,
erofs_destroy_workqueuereturns immediately if a singlepthread_joinfails. This halts the teardown process, potentially leaving orphaned threads and leaking the workqueue's mutexes and worker array.Refactor the joining logic to unconditionally join all worker threads. Capture the first error encountered, continue joining the remaining threads, and ensure all workqueue resources are properly freed before returning the captured error.
Signed-off-by Nithurshen <[email protected]>Hi Nithurshe,
I'm afraid I cannot agree with this change. If
pthread_join()fails, it implies that the worker thread is still alive.
Quoted 6 lines
Cleaning up the synchronization primitives and the workers array at this point would lead to a Use-After-Free issue, which is far more severe than the current resource leak.
I believe
pthread_join()seldom fails, otherwise it indicates bugs in our codebase.
Yes, I wonder the real error number here, it seems there is another bug somewhere.
Quoted 2 lines
How about just leaving an error log print for this scenario? cc @hsiangkao
Agreed, we should print the error message for each failure instead.
Thanks,
Gao Xiang
Quoted 47 lines
Thanks,
Yifan
Quoted 38 lines
--- lib/workqueue.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)lib/workqueue.c +7
-3@@ -42,6 +42,8 @@ static void *worker_thread(void *arg)int erofs_destroy_workqueue (struct erofs_workqueue *wq) { +int err =0 ; +if (!wq)return -EINVAL ;@@ -53,15 +55,17 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)while (wq->nworker) {int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) -return ret; +if (ret && !err) + err = ret; + --wq->nworker; }free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);pthread_cond_destroy (&wq->cond_full); -return 0 ; + +return err; } int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
PATCH v2 1/1 erofs-utils: fix thread join loop in erofs_destroy_workqueue
In reply to NithurshenCurrently, erofs_destroy_workqueue returns immediately if a single pthread_join fails. This halts the teardown process, potentially leaving orphanedd threads and leaking the workqueue's mutexes and worker array.
Refactor the joining logic to unconditionally join all worker threads. If a thread fails to join, print an error message with the return code. Crucially, if any join fails, skip cleaning up the synchronization primitives and worker array to prevent a severe UAF vulnerability caused by still-living threads.
--- lib/workqueue.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
lib/workqueue.c +14 -3
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 #include <pthread.h> #include <stdlib.h> +#include "erofs/print.h" #include "erofs/workqueue.h" static void *worker_thread (void *arg)
@@ -42,6 +43,8 @@ static void *worker_thread(void *arg)int erofs_destroy_workqueue (struct erofs_workqueue *wq) { +int err =0 ; +if (!wq)return -EINVAL ;
@@ -51,12 +54,20 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)pthread_mutex_unlock (&wq->lock);while (wq->nworker) { -int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); +int ret =pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) -return ret; +if (ret) { +erofs_err ("failed to join worker thread %u: %d" , + wq->nworker -1 , ret); +if (!err) + err = -ret; + } --wq->nworker; } + +if (err) +return err; +free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);
Signature · 1 line
2.43.0
On 3/18/2026 12:03 PM, Nithurshen wrote:
Currently, erofs_destroy_workqueue returns immediately if a single pthread_join fails. This halts the teardown process, potentially leaving orphanedd threads and leaking the workqueue's mutexes and worker array.
Refactor the joining logic to unconditionally join all worker threads. If a thread fails to join, print an error message with the return code. Crucially, if any join fails, skip cleaning up the synchronization primitives and worker array to prevent a severe UAF vulnerability caused by still-living threads.
--- lib/workqueue.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
lib/workqueue.c +4 -2
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 #include <pthread.h> #include <stdlib.h> +#include "erofs/print.h" #include "erofs/workqueue.h" static void *worker_thread (void *arg)
@@ -42,6 +43,8 @@ static void *worker_thread(void *arg)int erofs_destroy_workqueue (struct erofs_workqueue *wq) { +int err =0 ; +if (!wq)return -EINVAL ;
@@ -51,12 +54,20 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)pthread_mutex_unlock (&wq->lock);while (wq->nworker) { -int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); +int ret =pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret)
I think just a erofs_err() here is enough.., as joining other workers brings very little benefit here.
Yifan
- return ret;
+ if (ret) {
+ erofs_err("failed to join worker thread %u: %d",
+ wq->nworker - 1, ret);
+ if (!err)
+ err = -ret;
+ }
--wq->nworker;
}
+
+ if (err)
+ return err;
+
free(wq->workers);
pthread_mutex_destroy(&wq->lock);
pthread_cond_destroy(&wq->cond_empty);
PATCH v3 1/1 erofs-utils: fix thread join loop in erofs_destroy_workqueue
In reply to NithurshenCurrently, erofs_destroy_workqueue returns immediately if a single pthread_join fails. However, it does not log the failure, making it difficult to debug if a worker thread gets stuck.
Add an error log when pthread_join fails. Retain the early return behavior to safely abort the teardown process, ensuring we do not free the synchronization primitives and worker array while threads are potentially still alive (avoiding a use after free).
--- lib/workqueue.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/workqueue.c +6 -1
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 #include <pthread.h> #include <stdlib.h> +#include "erofs/print.h" #include "erofs/workqueue.h" static void *worker_thread (void *arg)
@@ -53,10 +54,14 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)while (wq->nworker) {int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) +if (ret) { +erofs_err ("failed to join worker thread %u: %d" , + wq->nworker -1 , ret);return ret; + } --wq->nworker; } +free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);
Signature · 1 line
2.43.0
A message that is not in the archive (sent off-list or not yet received): CABra5+2gf8+T4ker-7rdABiy25VVPjPUSLjzarttxajYkjU2xg@mail.gmail.com
Quoted 46 lines · Yifan Zhao
Quoted 46 lines · Nithurshen
Currently,
erofs_destroy_workqueuereturns immediately if a singlepthread_joinfails. However, it does not log the failure, making it difficult to debug if a worker thread gets stuck.Add an error log when
pthread_joinfails. Retain the early return behavior to safely abort the teardown process, ensuring we do not free the synchronization primitives and worker array while threads are potentially still alive (avoiding a use after free).Signed-off-by Nithurshen <[email protected]>--- lib/workqueue.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/workqueue.c +6
-1@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 #include <pthread.h> #include <stdlib.h> +#include "erofs/print.h" #include "erofs/workqueue.h" static void *worker_thread (void *arg)@@ -53,10 +54,14 @@ int erofs_destroy_workqueue(struct erofs_workqueue *wq)while (wq->nworker) {int ret = -pthread_join (wq->workers[wq->nworker -1 ],NULL ); -if (ret) +if (ret) { +erofs_err ("failed to join worker thread %u: %d" , + wq->nworker -1 , ret);return ret; + } --wq->nworker; } +free (wq->workers);pthread_mutex_destroy (&wq->lock);pthread_cond_destroy (&wq->cond_empty);
Quoted 1 line
Quoted 1 line
Reviewed-by Yifan Zhao <[email protected]>
Thanks for the review,
It looks like the CC list was dropped on your last reply, so I'm adding the mailing list back.
Quoted 2 lines · Nithurshen Karthikeyan
Quoted 1 line · Yifan Zhao
Quoted 1 line
...
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Reviewed-by Yifan Zhao <[email protected]>Thanks for the review,
It looks like the CC list was dropped on your last reply, so I'm adding the mailing list back.
Will apply, busy now.
Thanks,
Gao Xiang