stream.segmented: join worker+writer on close#4517
Conversation
|
Another approach: In def close(self):
self.worker.close()
self.writer.close()
self.buffer.close()
+ try:
+ self.writer.join()
+ except RuntimeError:
+ pass
+ try:
+ self.worker.join()
+ except RuntimeError:
+ pass
Note about the try-catch: Since |
bastimeyer
left a comment
There was a problem hiding this comment.
Thanks for the PR.
The following diff should be enough to fix the issue with the SegmentedStreamWorker and SegmentedStreamWriter deamon threads. No need to add a special method to SegmentedStreamReader, which is just one of the stream (base) classes, and catching an AttributeError when trying to call this method from the CLI on other stream implementations that don't have this method.
diff --git a/src/streamlink/stream/segmented.py b/src/streamlink/stream/segmented.py
index d097be24..26fbdac1 100644
--- a/src/streamlink/stream/segmented.py
+++ b/src/streamlink/stream/segmented.py
@@ -3,7 +3,7 @@ import queue
from concurrent import futures
from concurrent.futures import Future, ThreadPoolExecutor
from sys import version_info
-from threading import Event, Thread
+from threading import Event, Thread, current_thread
from typing import Any, Optional
from streamlink.buffers import RingBuffer
@@ -228,6 +228,12 @@ class SegmentedStreamReader(StreamIO):
self.writer.close()
self.buffer.close()
+ current = current_thread()
+ if current is not self.worker: # pragma: no branch
+ self.worker.join(timeout=self.timeout)
+ if current is not self.writer: # pragma: no branch
+ self.writer.join(timeout=self.timeout)
+
def read(self, size):
return self.buffer.read(
size,…SegmentedStreamWriter to finish. Thanks @bastimeyer
Co-authored-by: bastimeyer <[email protected]>
A simple, maybe ulgy, attempt to fix #4516 .
There may be more beautiful approaches.