Skip to content

Instantly share code, notes, and snippets.

@Lesmiscore
Created December 15, 2021 15:02
Show Gist options
  • Save Lesmiscore/ad089792632b1799660f12175231a485 to your computer and use it in GitHub Desktop.
Save Lesmiscore/ad089792632b1799660f12175231a485 to your computer and use it in GitHub Desktop.
trying to handle Ctrl+C on windows
diff --git a/yt_dlp/downloader/fragment.py b/yt_dlp/downloader/fragment.py
index 3d66ae19d..b5e50496b 100644
--- a/yt_dlp/downloader/fragment.py
+++ b/yt_dlp/downloader/fragment.py
@@ -4,6 +4,8 @@ import os
import time
import json
import http.client
+import threading
+import signal
from math import ceil
try:
@@ -400,15 +402,28 @@ class FragmentFD(FileDownloader):
job = tpe.submit(thread_func, idx, ctx, fragments, info_dict, tpe)
spins.append((tpe, job))
+ event = threading.Event()
+ def _signal_handler(signo, _frame):
+ event.set()
+
+ signal.signal(signal.SIGINT, _signal_handler)
+ signal.signal(signal.SIGTERM, _signal_handler)
+
result, kint = True, None
for tpe, job in spins:
- try:
- result = result and job.result()
- except KeyboardInterrupt as ex:
- interrupt_trigger[0] = False
- kint = ex
- finally:
- tpe.shutdown(wait=True)
+ while job.running() and not event.is_set():
+ try:
+ event.wait(10)
+ result = result and job.result(0.1)
+ break
+ except concurrent.futures.TimeoutError:
+ continue
+ except KeyboardInterrupt as ex:
+ interrupt_trigger[0] = False
+ kint = ex
+ break
+ finally:
+ tpe.shutdown(wait=True)
if not interrupt_trigger[0] and not any(arg[0].get('live') for arg in args):
# rethrow if it's not live
raise kint or KeyboardInterrupt()
diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.py
index 0db49ace8..984fe57e5 100644
--- a/yt_dlp/extractor/youtube.py
+++ b/yt_dlp/extractor/youtube.py
@@ -1729,7 +1729,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
try:
fmts, _ = self._extract_mpd_formats_and_subtitles(
mpd_url, None, note=False, errnote=False, fatal=False)
- except BaseException:
+ except ExtractorError:
fmts = None
if not fmts:
no_fragment_score += 1
@@ -1745,7 +1745,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
try:
urlh = self._request_webpage(
last_segment_url, None, note=False, errnote=False, fatal=False)
- except BaseException:
+ except ExtractorError:
urlh = None
if not urlh:
no_fragment_score += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment