From e572e123b55b29527e54ce5f0807f115481d78b9 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sat, 23 Mar 2013 21:51:38 +0200 Subject: [PATCH] liblzma: Fix another deadlock in the threaded encoder. This race condition could cause a deadlock if lzma_end() was called before finishing the encoding. This can happen with xz with debugging enabled (non-debugging version doesn't call lzma_end() before exiting). --- src/liblzma/common/stream_encoder_mt.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c index 3199cf80..e30e9dca 100644 --- a/src/liblzma/common/stream_encoder_mt.c +++ b/src/liblzma/common/stream_encoder_mt.c @@ -341,11 +341,14 @@ worker_start(void *thr_ptr) if (state == THR_EXIT) break; - // Mark the thread as idle. Signal is needed for the case + // Mark the thread as idle unless the main thread has + // told us to exit. Signal is needed for the case // where the main thread is waiting for the threads to stop. mythread_sync(thr->mutex) { - thr->state = THR_IDLE; - pthread_cond_signal(&thr->cond); + if (thr->state != THR_EXIT) { + thr->state = THR_IDLE; + pthread_cond_signal(&thr->cond); + } } mythread_sync(thr->coder->mutex) {