Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7406 UnicodeDecodeError in on_tracker_error_alert #7468

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ def on_tracker_reply_alert(self, alert: lt.tracker_reply_alert):
self.tracker_status[alert.url] = [alert.num_peers, 'Working']

def on_tracker_error_alert(self, alert: lt.tracker_error_alert):
self._logger.error(f'On tracker error alert: {alert}')

# try-except block here is a workaround and has been added to solve
# the issue: "UnicodeDecodeError: invalid continuation byte"
# The try-except block is added as a workaround to suppress UnicodeDecodeError in `repr(alert)`,
# `alert.url` and `alert.msg`. See https://github.com/arvidn/libtorrent/issues/143
try:
self._logger.error(f'On tracker error alert: {alert}')
url = alert.url

peers = self.tracker_status[url][0] if url in self.tracker_status else 0
if alert.msg:
status = 'Error: ' + alert.msg
Expand All @@ -330,8 +330,7 @@ def on_tracker_error_alert(self, alert: lt.tracker_error_alert):

self.tracker_status[url] = [peers, status]
except UnicodeDecodeError as e:
self._logger.exception(e)
return
self._logger.warning(f'UnicodeDecodeError in on_tracker_error_alert: {e}')

def on_tracker_warning_alert(self, alert: lt.tracker_warning_alert):
self._logger.warning(f'On tracker warning alert: {alert}')
Expand Down
9 changes: 9 additions & 0 deletions src/tribler/core/components/libtorrent/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import libtorrent as lt
import pytest
from _pytest.logging import LogCaptureFixture
from ipv8.util import succeed
from libtorrent import bencode

Expand Down Expand Up @@ -238,6 +239,14 @@ def test_process_error_alert(test_download):
assert test_download.tracker_status[url][1] == 'Timeout'


def test_tracker_error_alert_unicode_decode_error(test_download: Download, caplog: LogCaptureFixture):
exception = UnicodeDecodeError('utf-8', b'\xc3\x28', 0, 1, 'invalid continuation byte')
mock_alert = MagicMock(__repr__=Mock(side_effect=exception))
test_download.process_alert(mock_alert, 'tracker_error_alert')
assert caplog.messages == ["UnicodeDecodeError in on_tracker_error_alert: "
"'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte"]


def test_tracker_warning_alert(test_download):
"""
Test whether a tracking warning alert is processed correctly
Expand Down