Skip to content

Commit

Permalink
Fix for websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
coletdjnz committed Feb 17, 2024
1 parent e27dafc commit d121ac7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
29 changes: 19 additions & 10 deletions test/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,25 @@ def test_certificate_nocombined_pass(self, handler):
})


class TestRequestHandlerMisc:
"""Misc generic tests for request handlers, not related to request or validation testing"""
@pytest.mark.parametrize('handler,logger_name', [
('Requests', 'urllib3'),
('Websockets', 'websockets.client'),
('Websockets', 'websockets.server')
], indirect=['handler'])
def test_remove_logging_handler(self, handler, logger_name):
# Ensure any logging handlers, which may contain a YoutubeDL instance,
# are removed when we close the request handler
# See: https://github.com/yt-dlp/yt-dlp/issues/8922
logging_handlers = logging.getLogger(logger_name).handlers
before_count = len(logging_handlers)
rh = handler()
assert len(logging_handlers) == before_count + 1
rh.close()
assert len(logging_handlers) == before_count


class TestUrllibRequestHandler(TestRequestHandlerBase):
@pytest.mark.parametrize('handler', ['Urllib'], indirect=True)
def test_file_urls(self, handler):
Expand Down Expand Up @@ -906,16 +925,6 @@ def mock_close(*args, **kwargs):

assert called

def test_remove_logging_handler(self, handler):
# Ensure logging handler, containing YoutubeDL instance, is removed when we close the request handler
# https://github.com/yt-dlp/yt-dlp/issues/8922
logging_handlers = logging.getLogger('urllib3').handlers
before_count = len(logging_handlers)
rh = handler()
assert len(logging_handlers) == before_count + 1
rh.close()
assert len(logging_handlers) == before_count


def run_validation(handler, error, req, **handler_kwargs):
with handler(**handler_kwargs) as rh:
Expand Down
13 changes: 12 additions & 1 deletion yt_dlp/networking/_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import websockets.sync.client
from websockets.uri import parse_uri

WEBSOCKETS_LOGGERS = ('websockets.client', 'websockets.server')


class WebsocketsResponseAdapter(WebSocketResponse):

Expand Down Expand Up @@ -90,10 +92,12 @@ class WebsocketsRH(WebSocketRequestHandler):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name in ('websockets.client', 'websockets.server'):
self.__logging_handlers = []
for name in WEBSOCKETS_LOGGERS:
logger = logging.getLogger(name)
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(logging.Formatter(f'{self.RH_NAME}: %(message)s'))
self.__logging_handlers.append(handler)
logger.addHandler(handler)
if self.verbose:
logger.setLevel(logging.DEBUG)
Expand All @@ -103,6 +107,13 @@ def _check_extensions(self, extensions):
extensions.pop('timeout', None)
extensions.pop('cookiejar', None)

def close(self):
# Remove the logging handler that contains a reference to our logger
# See: https://github.com/yt-dlp/yt-dlp/issues/8922
for name in WEBSOCKETS_LOGGERS:
for handler in self.__logging_handlers:
logging.getLogger(name).removeHandler(handler)

def _send(self, request):
timeout = float(request.extensions.get('timeout') or self.timeout)
headers = self._merge_headers(request.headers)
Expand Down

0 comments on commit d121ac7

Please sign in to comment.