Skip to content

Commit 050fb00

Browse files
committed
ThreadedGzip classes cannot prevent a program from exiting anymore.
1 parent df45eaf commit 050fb00

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ 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
11+
-----------------
12+
+ Fix a bug where ``gzip_ng_threaded.open`` could
13+
cause a hang when the program exited and the program was not used with a
14+
context manager.
15+
1016
version 0.5.0
1117
-----------------
1218
+ Wheels are now build for MacOS arm64 architectures.

src/zlib_ng/gzip_ng_threaded.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def __init__(self, filename, queue_size=2, block_size=1024 * 1024):
9898
self.exception = None
9999
self.buffer = io.BytesIO()
100100
self.block_size = block_size
101-
self.worker = threading.Thread(target=self._decompress)
101+
# Using a daemon thread prevents programs freezing on error.
102+
self.worker = threading.Thread(target=self._decompress, daemon=True)
102103
self._closed = False
103104
self.running = True
104105
self.worker.start()
@@ -231,17 +232,18 @@ def __init__(self,
231232
queue.Queue(queue_size) for _ in range(threads)]
232233
self.output_queues: List[queue.Queue[Tuple[bytes, int, int]]] = [
233234
queue.Queue(queue_size) for _ in range(threads)]
234-
self.output_worker = threading.Thread(target=self._write)
235+
# Using daemon threads prevents a program freezing on error.
236+
self.output_worker = threading.Thread(target=self._write, daemon=True)
235237
self.compression_workers = [
236-
threading.Thread(target=self._compress, args=(i,))
238+
threading.Thread(target=self._compress, args=(i,), daemon=True)
237239
for i in range(threads)
238240
]
239241
elif threads == 1:
240242
self.input_queues = [queue.Queue(queue_size)]
241243
self.output_queues = []
242244
self.compression_workers = []
243245
self.output_worker = threading.Thread(
244-
target=self._compress_and_write)
246+
target=self._compress_and_write, daemon=True)
245247
else:
246248
raise ValueError(f"threads should be at least 1, got {threads}")
247249
self.threads = threads

0 commit comments

Comments
 (0)