Skip to content

Commit 4a25e32

Browse files
committed
Fix infinite while loops with a thread is alive check
1 parent 9f46857 commit 4a25e32

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Changelog
77
.. This document is user facing. Please word the changes in such a way
88
.. that users understand how the changes affect the new version.
99
10+
version 0.5.1-dev
11+
-----------------
12+
+ Threaded reading and writing do no longer block exiting when an exception
13+
occurs in the main thread.
14+
1015
version 0.5.0
1116
-----------------
1217
+ Wheels are now build for MacOS arm64 architectures.

src/zlib_ng/gzip_ng_threaded.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(self, filename, queue_size=2, block_size=1024 * 1024):
101101
self.worker = threading.Thread(target=self._decompress)
102102
self._closed = False
103103
self.running = True
104+
self._calling_thread = threading.current_thread()
104105
self.worker.start()
105106

106107
def _check_closed(self, msg=None):
@@ -110,15 +111,15 @@ def _check_closed(self, msg=None):
110111
def _decompress(self):
111112
block_size = self.block_size
112113
block_queue = self.queue
113-
while self.running:
114+
while self.running and self._calling_thread.is_alive():
114115
try:
115116
data = self.fileobj.read(block_size)
116117
except Exception as e:
117118
self.exception = e
118119
return
119120
if not data:
120121
return
121-
while self.running:
122+
while self.running and self._calling_thread.is_alive():
122123
try:
123124
block_queue.put(data, timeout=0.05)
124125
break
@@ -215,6 +216,7 @@ def __init__(self,
215216
if "b" not in mode:
216217
mode += "b"
217218
self.lock = threading.Lock()
219+
self._calling_thread = threading.current_thread()
218220
self.exception: Optional[Exception] = None
219221
self.level = level
220222
self.previous_block = b""
@@ -349,7 +351,7 @@ def _compress(self, index: int):
349351
in_queue = self.input_queues[index]
350352
out_queue = self.output_queues[index]
351353
compressor: zlib_ng._ParallelCompress = self.compressors[index]
352-
while True:
354+
while self._calling_thread.is_alive():
353355
try:
354356
data, zdict = in_queue.get(timeout=0.05)
355357
except queue.Empty:
@@ -372,7 +374,7 @@ def _write(self):
372374
fp = self.raw
373375
total_crc = 0
374376
size = 0
375-
while True:
377+
while self._calling_thread.is_alive():
376378
out_index = index % self.threads
377379
output_queue = output_queues[out_index]
378380
try:
@@ -397,7 +399,7 @@ def _compress_and_write(self):
397399
size = 0
398400
in_queue = self.input_queues[0]
399401
compressor = self.compressors[0]
400-
while True:
402+
while self._calling_thread.is_alive():
401403
try:
402404
data, zdict = in_queue.get(timeout=0.05)
403405
except queue.Empty:

0 commit comments

Comments
 (0)