Commit 61bc5a62 for xz

commit 61bc5a621ca7db2616970b9d999ddc789d4aa2d9
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Sat Oct 17 18:37:39 2026 +0300

    liblzma: mt enc: Clarify worker thread error reporting

    It worked correctly, but it was a bit confusing that on errors
    a worker thread returned itself to the stack of free threads,
    and the main thread still accessed that worker_thread struct.
    Since the access was always to the most recently started thread,
    there was no risk that the worker could have been reused, but
    with this commit it's more obvious.

    Thanks-to: Mar <mardgez13@gmail.com>

diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
index 570cdd5e..a2e6f02c 100644
--- a/src/liblzma/common/stream_encoder_mt.c
+++ b/src/liblzma/common/stream_encoder_mt.c
@@ -49,6 +49,10 @@ typedef struct worker_thread_s worker_thread;
 struct worker_thread_s {
 	worker_state state;

+	/// The worker sets this to true if an error occurs.
+	/// See worker_error().
+	bool error_occurred;
+
 	/// Input buffer of coder->block_size bytes. The main thread will
 	/// put new input into this and update in_size accordingly. Once
 	/// no more input is coming, state will be set to THR_FINISH.
@@ -207,6 +211,8 @@ worker_error(worker_thread *thr, lzma_ret ret)
 	assert(ret != LZMA_OK);
 	assert(ret != LZMA_STREAM_END);

+	// Pass the error code to the main thread.
+	// Don't replace an earlier error code though.
 	mythread_sync(thr->coder->mutex) {
 		if (thr->coder->thread_error == LZMA_OK)
 			thr->coder->thread_error = ret;
@@ -214,6 +220,20 @@ worker_error(worker_thread *thr, lzma_ret ret)
 		mythread_cond_signal(&thr->coder->cond);
 	}

+	mythread_sync(thr->mutex) {
+		// This flag matters only if the error occurred in the
+		// most recently started worker to which the main thread
+		// is still passing more input data.
+		thr->error_occurred = true;
+
+		// Wait until the main thread tells us to stop or exit.
+		// This isn't strictly necessary, but it's less confusing
+		// that we don't put ourselves to the stack of free threads
+		// until the error has been handled in the main thread.
+		while (thr->state < THR_STOP)
+			mythread_cond_wait(&thr->cond, &thr->mutex);
+	}
+
 	return;
 }

@@ -574,6 +594,7 @@ get_thread(lzma_stream_coder *coder, const lzma_allocator *allocator)
 	// in the main thread.
 	mythread_sync(coder->thr->mutex) {
 		coder->thr->state = THR_RUN;
+		coder->thr->error_occurred = false;
 		coder->thr->in_size = 0;
 		coder->thr->outbuf = lzma_outq_get_buf(&coder->outq, NULL);

@@ -623,7 +644,7 @@ stream_encode_in(lzma_stream_coder *coder, const lzma_allocator *allocator,
 		bool block_error = false;

 		mythread_sync(coder->thr->mutex) {
-			if (coder->thr->state == THR_IDLE) {
+			if (coder->thr->error_occurred) {
 				// Something has gone wrong with the Block
 				// encoder. It has set coder->thread_error
 				// which we will read a few lines later.